[Numpy-discussion] Re: range/arange

Robert Kern robert.kern at gmail.com
Thu Apr 13 05:17:05 CDT 2006


Pierre GM wrote:
> Folks,
> Could any of you explain me why the two following commands give different 
> results ? It's mere curiosity, for my personal edification.
> 
> [(m-5)/10 for m in arange(1,10)]
> [0, 0, 0, 0, 0, 0, 0, 0, 0]
> 
> [(m-5)/10 for m in range(1,10)]
> [-1, -1, -1, -1, 0, 0, 0, 0, 0]

Python's rule for integer division is to round towards negative infinity. C's
rule (if it has one; I think it may be platform dependent) is to round towards
0. When it comes to arithmetic, numpy tends to expose the C behavior because
it's fastest. As Lars pointed out, the type of the object that you get from
iterating over an array is a numpy int32scalar object, so the numpy behavior is
used.

-- 
Robert Kern
robert.kern at gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco





More information about the Numpy-discussion mailing list