RiskyScientist RiskyScientist - 3 months ago 12
Python Question

np.cast in numpy results in incorrect result

This use of np.cast:

np.cast['f'](np.pi)


results in an incorrect value for pi:

array(3.1415927410125732, dtype=float32)


why did this happen?

Answer

Real pi value up to a few digits more than float32 precision (from here)

`3.14159265358979323846264338327950288...` 

float32 precision has an accuracy from 6 to 9 decimal places (from here)

 3.1 4 1 5 9 2 7 4 1  0125732 #  your value
 3.1 4 1 5 9 2 6 5 3  5897    # actual value
 --1 2 3 4 5 6 7 8 9   # (index of decimal place)

As you can see the deviation from the real value happens after the 7th decimal place. So that's the reason why it is not right after that