[Numpy-discussion] float16/32: wrong number of digits?
peridot.faceted at gmail.com
Thu Mar 9 05:57:19 EST 2017
On Thu, Mar 9, 2017, 11:27 Nico Schlömer <nico.schloemer at gmail.com> wrote:
> 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)) - b
> Out: -1.9531250000093259e-06
> Out: array([ 0.33325195], dtype=float16)
> Out: '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?
Even more troubling, the high precision numpy types - np.longdouble and its
complex version - lose intimation when used with repr.
The basic problem is (roughly) that all floating-point numbers are
converted to python floats before printing. I put some effort into cleaning
this up, but the code is messy (actually there are several independent code
paths for converting numbers to strings) and the algorithms python uses to
make repr work out nicely are nontrivial.
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion