Re: [Numpy-discussion] round / set_printoptions discrepancy

13 Sep
2019
13 Sep
'19
3:59 p.m.
In my opinion the problem is that numpy floats break the Liskov substitution principle,
pyfloat = 16.055 npfloat = np.float64(pyfloat) isinstance(npfloat, float)
True
round(pyfloat, 2)
16.05
round(npfloat, 2)
16.06
Since numpy.float64 is a subclass of builtins.float I would expect that
round(x, j) == round(np.float64(x), j)
is an invariant, but unfortunately this is not the case.
Moreover the python3 semantics of the round function require that when the number of digits is None, the return value should be of integral type:
round(pyfloat)
16
round(pyfloat, None)
16
round(pyfloat, 0)
16.0
round(npfloat)
16.0
round(npfloat, None)
16.0
round(npfloat, 0)
16.0
see also https://github.com/numpy/numpy/issues/11810
Stefano
1287
Age (days ago)
1287
Last active (days ago)
0 comments
1 participants
participants (1)
-
Stefano Miccoli