[Numpy-discussion] round / set_printoptions discrepancy

Andras Deak deak.andris at gmail.com
Fri Sep 13 07:23:57 EDT 2019


On Fri, Sep 13, 2019 at 12:58 PM Irvin Probst
<irvin.probst at ensta-bretagne.fr> wrote:
>
> Hi,
> Is it expected/documented that np.round and np.set_printoptions do not
> output the same result on screen ?
> I tumbled into this running this code:
>
> import numpy as np
> mes = np.array([
>      [16.06, 16.13, 16.06, 16.00, 16.06, 16.00, 16.13, 16.00]
> ])
>
> avg = np.mean(mes, axis=1)
> print(np.round(avg, 2))
> np.set_printoptions(precision=2)
> print(avg)
>
>
> Which outputs:
>
> [16.06]
> [16.05]
>
> Is that worth a bug report or did I miss something ? I've been able to
> reproduce this on many windows/linux PCs with python/numpy releases from
> 2017 up to last week.
>
> Thanks.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

Hi,

I just want to add that you can use literal 16.055 to reproduce this:
>>> import numpy as np
>>> np.set_printoptions(precision=2)
>>> np.array([16.055]).round(2)
array([16.06])
>>> np.array([16.055])
array([16.05])

I would think it has to do with "round to nearest even":
>>> np.array(16.055)
array(16.05)
>>> np.array(16.065)
array(16.07)
>>> np.array(16.065).round(2)
16.07

But it's as if `round` rounded decimal digits upwards (16.055 ->
16.06, 16.065 -> 16.07), whereas the `repr` rounded to the nearest
odd(!) digit (16.055 -> 16.05, 16.065 -> 16.07). Does this make any
sense? I'm on numpy 1.17.2.
(Scalars or 1-length 1d arrays don't seem to make a difference).
Regards,

András


More information about the NumPy-Discussion mailing list