Nico Schlömer
2017-03-09 10:26:44 UTC
Hi everyone,
I wondered how to express a numpy float exactly in terms of format, and
found `%r` quite useful: `float(repr(a)) == a` is guaranteed for Python
`float`s. When trying the same thing with lower-precision Python floats, I
found this identity not quite fulfilled:
```
import numpy
b = numpy.array([1.0 / 3.0], dtype=np.float16)
float(repr(b[0])) - b[0]
Out[12]: -1.9531250000093259e-06
```
Indeed,
```
b
Out[6]: array([ 0.33325195], dtype=float16)
```
```
repr(b[0])
Out[7]: '0.33325'
```
When counting the bits, a float16 should hold 4.8 decimal digits, so
`repr()` seems right. Where does the garbage tail -1.9531250000093259e-06
come from though?
Cheers,
Nico
I wondered how to express a numpy float exactly in terms of format, and
found `%r` quite useful: `float(repr(a)) == a` is guaranteed for Python
`float`s. When trying the same thing with lower-precision Python floats, I
found this identity not quite fulfilled:
```
import numpy
b = numpy.array([1.0 / 3.0], dtype=np.float16)
float(repr(b[0])) - b[0]
Out[12]: -1.9531250000093259e-06
```
Indeed,
```
b
Out[6]: array([ 0.33325195], dtype=float16)
```
```
repr(b[0])
Out[7]: '0.33325'
```
When counting the bits, a float16 should hold 4.8 decimal digits, so
`repr()` seems right. Where does the garbage tail -1.9531250000093259e-06
come from though?
Cheers,
Nico