[Numpy-discussion] round(numpy.float64(0.0)) is a numpy.float64

Nathaniel Smith njs at pobox.com
Mon Mar 26 21:28:39 EDT 2018


On Mon, Mar 26, 2018 at 6:24 PM, Nathaniel Smith <njs at pobox.com> wrote:
> Even knowing that, it's still confusing that round(np.float64(0.0))
> isn't the same as round(0.0). The reason is a Python 2 / Python 3
> thing: in Python 2, round returns a float, while on Python 3, it
> returns an integer – but numpy still uses the python 2 behavior
> everywhere.
>
> I'm not sure if it's possible or worthwhile to change this. If we'd
> changed it when we first added python 3 support then it would have
> been easy (and obviously a good idea), but at this point it might be
> tricky?

Oh right, and I forgot: part of the reason it's tricky is that it
really would have to return a Python 'int', *not* any of numpy's
integer types, because floats have a much larger range than numpy
integers, e.g.:

In [4]: round(1e50)
Out[4]: 100000000000000007629769841091887003294964970946560

In [5]: round(np.float64(1e50))
Out[5]: 1e+50

In [6]: np.uint64(round(np.float64(1e50)))
Out[6]: 0

(Actually that last case illustrates another weird inconsistency:
np.uint64(1e50) -> OverflowError, but np.uint64(np.float64(1e50)) ->
0. I have no idea what's going on there.)

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the NumPy-Discussion mailing list