Search code examples
pythonnumpyfloating-pointieee-754

Unexpected round behaviour of Numpy float32


I am trying to understand how numpy handles the float32 datatype.

The following code produces 0.25815687

print(np.float32(0.2581568658351898).astype(str)) # 0.25815687

But an online float converter https://www.h-schmidt.net/FloatConverter/IEEE754.html gives 0.2581568658351898193359375, Is Numpy doing something special when printing the single-precision float or there is something I missed? Online converter result


Solution

  • Is Numpy doing something special when printing the single-precision float or there is something I missed?

    0.2581568658351898 is not exactly encodable as a 32-bit float.
    The closest is 0.2581568658351898193359375 or 0x1.085a46p-2

    When 0.2581568658351898193359375 is printed with reduced precision, the result is 0.25815687


    0.2581568 658351898            Source code
    0.2581568 658351898193359375   True value
    0.2581568 7                    Output