[Numpy-discussion] arange and floating point arguments
Charles R Harris
charlesr.harris@gmail....
Fri Sep 14 13:12:43 CDT 2007
On 9/14/07, Ed Schofield <edschofield@gmail.com> 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().
Since none of the numbers are exactly represented in IEEE floating point,
this sort of oddity is expected. If you look at the exact values, (.4 +
.2)/.1 > 6 and .6/.1 < 6 . That said, I would expect something like
ceil(interval/delta - relatively_really_small_number) would generally return
the expected result. Matlab probably plays these sort of games. The downside
is encouraging bad programming habits. In this case, the programmer should
be using linspace..
Chuck.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070914/34f30d07/attachment.html
More information about the Numpy-discussion
mailing list