[Numpy-discussion] round / set_printoptions discrepancy
Stefano Miccoli
stefano.miccoli at polimi.it
Fri Sep 13 11:59:43 EDT 2019
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
More information about the NumPy-Discussion
mailing list