You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can see this visually using [float exposed](https://float.exposed/0x3ff4000000000000)
266
266
267
-
```{important}
267
+
```{tip}
268
268
the URL of that link is to `float.exposed/0x3ff4000000000000`
269
269
270
270
`3ff4000000000000` is the number we want to view in hex. Since the first 12 bits of the 64bit nubme are the sign and expontend, the first 3 characters represent those then the remaining 13 characters represent the 52 bits of the fraction.
@@ -277,12 +277,14 @@ So `3ff=011111111` and then 4000000000000=01000000000000000000000000000000000000
277
277
To see more examples of numbers doing surprising and interesting things try [memory spy](https://memory-spy.wizardzines.com/game#)```
278
278
````
279
279
280
-
Python can give you the float representation of
280
+
Python can give you the hex representation of a float:
281
281
282
282
```{code-cell} python
283
283
float.hex(1.25)
284
284
```
285
285
286
+
This matches what we saw above, but it takes some re-arranging, this is a good practice.
287
+
286
288
287
289
288
290
@@ -357,23 +359,20 @@ We want to use exactly 53 bits to represent $J$ because it needs to be represent
357
359
358
360
We can use this to find what $N$ needs to be first.
359
361
360
-
we want $J \approx \frac{2^N }{10}$ to be greater than or equal to $2^{52}$ and less than $2^{53}$, or equivalently
361
-
362
-
362
+
we want $J \approx \frac{2^N }{10}$ to be greater than or equal to $2^{52}$ and less than $2^{53}$, or equivalently:
and then we can re-write the middle comparison like this:
374
374
375
375
376
-
377
376
$$ 2^{N-4} < \frac{2^N }{10} < 2^{N-3} $$
378
377
379
378
@@ -385,9 +384,9 @@ and then we can re-write the middle comparison like this:
385
384
386
385
387
386
388
-
then best $N$ is 56, but we can check it.
389
-
387
+
then best $N$ is 56, because that will make the upper inequality the same as the lower one.
390
388
389
+
We can also check tha this does what we want.
391
390
392
391
```{code-cell} python
393
392
2**52 <= 2**56 //10 < 2**53
@@ -399,22 +398,21 @@ Now we can get the number we will use for $J$. So far, mathematically, we would
399
398
400
399
$$J \approx \frac{2^{56}} {10}$$
401
400
402
-
but we need to find an integer value, so we divide that out and separate the `q`uotient and `r`emainder
401
+
but we need to find an integer value, so we divide and separate the `q`uotient and `r`emainder.
403
402
404
403
```{code-cell} python
405
404
q,r = divmod(2**56,10)
406
405
```
407
406
408
407
409
-
Then we check the remainder to decide if we should round up by 1 or not.
408
+
Next we check the remainder to decide if we should round up by 1 or not.
410
409
411
410
```{code-cell} python
412
411
r
413
412
```
414
413
415
414
416
-
$ 6 > 5 = \frac{10}{2}$ so we round up
417
-
415
+
$ 6 > 5 = \frac{10}{2}$ so we round up. Since the remainder is more than half of the divisor it is closer to the next number up, so we increment.
418
416
419
417
420
418
```{code-cell} python
@@ -424,7 +422,7 @@ J
424
422
425
423
426
424
427
-
then we chan check the length in bits of that number
425
+
then we can check the length in bits of that number
428
426
429
427
Python can print it for us:
430
428
```{code-cell} python
@@ -440,6 +438,20 @@ len(bin(J))-2
440
438
Which is 53 as expected! To get the actual number we represent in the fraction part we want to drop the left most bit.
441
439
442
440
441
+
To actually represent our final number, we want to drop the left most bit. Remember, we found the number that gets represented including the additional 1 outside of the sum of the bits in the fraction.
442
+
443
+
```{code-cell} python
444
+
significand = J-2**52
445
+
significand
446
+
```
447
+
448
+
or in binary:
449
+
```{code-cell} python
450
+
bin(significand)
451
+
```
452
+
453
+
454
+
443
455
Now, we go back to the $e$. We need $52-e+1023 = 56$ so we solve to find
444
456
$$e = 52+1023-56$$
445
457
@@ -449,7 +461,19 @@ e = 52-56+1023
449
461
e
450
462
```
451
463
464
+
So our representation of .1 would be:
465
+
466
+
```{code-cell} python
467
+
exp_bin = bin(e)[2:]
468
+
sig_bin = bin(significand)[2:]
469
+
'0' + exp_bin + sig_bin
470
+
```
471
+
472
+
dropping the first two characters is because those are the 0b syntax indicator, not the actual binary.
473
+
474
+
452
475
476
+
[see on float exposed](https://float.exposed/0x3fb999999999999a)
453
477
454
478
Python doesn't provide a binary reprsentation of floats, but it does provide a hex representation.
0 commit comments