Search code examples
floating-pointieee-754

It looks like half-precision 0.01825 is not exactly represented in single?


I suppose each half precision (16bit) number can be exactly represented in single precision (32bit). But the following experiment seems to show the half-precision 0.01825 is represented only approximately in single. Any idea what is wrong?

In [37]: x=np.float16(0.01825)

In [38]: x
Out[38]: 0.01825

In [39]: np.float32(x)
Out[39]: 0.018249512

Solution

  • 0.01825 cannot be represented exactly as binary floating point number (of any precision). When converting to strings, python rounds floating point numbers to a certain number of digits (enough to get the original value when converting the string back to the original format). You can get an exact decimal representation of the stored number, e.g. using:

    In [4]: format(x, '.99g') # N.B. For double precision you may need more digits
    Out[4]: '0.01824951171875'