Why is the precision of numpy output different each time?
print(a)
prints the shortest numeric string that yields the same float64 value as a
.
Example:
a = 0.392820481778549002
b = 0.392820481778549
a_bits = np.asarray(a).view(np.int64).item()
b_bits = np.asarray(b).view(np.int64).item()
print(f"{a:.18f}", hex(a_bits))
print(f"{b:.18f}", hex(b_bits))
print(a == b)
Result:
0.392820481778549002 0x3fd923f8849c0570
0.392820481778549002 0x3fd923f8849c0570
True
You can use the f"{a:.18f}"
syntax to get fixed-width output. The equivalent for numpy arrays is np.set_printoptions(precision=18, floatmode="fixed")
.