Home > database >  Unexpected round behaviour of Numpy float32
Unexpected round behaviour of Numpy float32

Time:11-29

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

CodePudding user response:

You can force the number of digits to be printed using f-strings

print(f"{np.float32(0.2581568658351898):.25f}")

where :.25f tells the interpreter to print the value as a floating point number with 25 decimal digits.

CodePudding user response:

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 
  • Related