[Numpy-discussion] arange and floating point arguments

Robert Kern robert.kern at gmail.com
Fri Sep 14 11:54:19 EDT 2007


Ed Schofield wrote:
> Hi everyone,
> 
> This was reported yesterday as a bug in Debian's numpy package:
> 
>>>> len(numpy.arange(0, 0.6, 0.1)) == len(numpy.arange(0, 0.4+0.2, 0.1))
> False
> 
> The cause is this:
> 
>>>> ceil((0.4+0.2)/0.1)
> 7.0
> 
>>>> ceil(0.6/0.1)
> 6.0
> 
> which holds for both numpy's and the standard library's ceil().

>>> 0.6 == (0.4+0.2)
False

Consequently, not a bug.

> Using arange in this way is a fundamentally unreliable thing to do,
> but is there anything we want to do about this?

Tell people to use linspace(). Yes, it does a slightly different thing; that's
why it works. Most uses of floating point arange() can be cast using linspace()
more reliably.

-- 
Robert Kern

"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