I run some simulations whose result is a dict like this:
results = {
'this': 5,
'that': 6,
'those': [2.34, 5.67]
}
I save it in a human-readable format using code like this:
s = ''
for key, value in results.items():
s += f'{key}: {value}n'
One of my values happens to be a list of numpy.float32
values.
All was well until I upgraded to numpy 2.0.0. In this version, the code above produces strings like [np.float32(2.34), np.float32(5.67)]
instead of [2.34, 5.67]
(which I got in numpy 1.26.1).
Should I complicate my saving code and use different code for saving values of different types? Or is there some workaround which could produce human-readable output for all types, including those composed of float32
?
Additional examples (in an interactive python environment):
import numpy x = numpy.float32(2.3) x numpy 1 — 2.3 numpy 2 — np.float32(2.3) f'{x}' numpy 1 — '2.299999952316284' numpy 2 — '2.299999952316284' f'{[x, x]}' numpy 1 — '[2.3, 2.3]' numpy 2 — '[np.float32(2.3), np.float32(2.3)]' y={x:x*x} y numpy 1 — {2.3: 5.29} numpy 2 — {np.float32(2.3): np.float32(5.29)}
I can’t make sense of it. Is it a bug that I could expect to be fixed, or a feature which is unlikely to change?