[Numpy-discussion] bug in round with negative number of decimals

Sebastian Haase haase at msg.ucsf.edu
Mon Sep 4 00:42:17 CDT 2006


Charles R Harris wrote:
> 
> 
> On 9/3/06, *Sebastian Haase* <haase at msg.ucsf.edu 
> <mailto:haase at msg.ucsf.edu>> wrote:
> 
>     Hi,
>     I just learn about the existence of round().
>     Is the following exposing a bug?
>      >>> N.__version__
>     '1.0b4'
>      >>> N.array([65.0])
>     [ 65.]
>      >>> _.round(-1)
>     [ 60.]
>      >>> round(65, -1)
>     70.0
>      >>> N.array([65])
>     [65]
>      >>> _.round(-1)
>     [60]
>      >>> N.array([66])
>     [66]
>      >>> _.round(-1)
>     [60]
>      >>> N.array([66.2])
>     [ 66.2]
>      >>> _.round(-1)
>     [ 70.]
> 
>     65 should round *up* to 70. (when decimals is given as -1)
>     In numpy even 66 rounds down, but 66.2 rounds up ...
> 
> 
> Numpy rounds to even.
> 
>  >>> around(arange(10)*.5)
> array([ 0.,  0.,  1.,  2.,  2.,  2.,  3.,  4.,  4.,  4.])
> 
> i.e., when at those x.5 boundaries, round to the nearest even number. 
> Always rounding in the same direction biases the numbers when you have 
> gazillions of them. This is most likely of import when the fpu is 
> rounding intermediate results to register length, but numpy does it too. 
> Floats can be weird anyway, .15, for instance, can't be represented 
> exactly as an ieee float. It does tend to throw folks for a loop when 
> they don't keep this in mind.
> 
>     - Sebastian Haase
> 
> 
> Chuck


Thanks for the reply - but read what the doc says:
 >>> N.around.__doc__
'Round 'a' to the given number of decimal places.  Rounding
     behaviour is equivalent to Python.

     Return 'a' if the array is not floating point.  Round both the real
     and imaginary parts separately if the array is complex.
     '

it is *not* done this way in Python:
 >>> round(.5)
1.0
 >>> round(1.5)
2.0
 >>> round(2.5)
3.0

( the round obj. method is missing this doc string )

I really think we should stick to what the doc string say - everybody 
expects x.5  to round up !!

look even at this:
 >>> repr(2.05)
'2.0499999999999998'
 >>> round(2.05, 1)
2.1

(don't ask me how Python does this, but it's "nice" )

Thanks,
Sebastian




More information about the Numpy-discussion mailing list