[SciPy-user] Problem with arange

Lance Boyle lanceboyle at myrealbox.com
Wed Nov 5 23:39:58 EST 2003


On Tuesday, Nov 4, 2003, at 10:45 America/Phoenix, Santiago Erquicia 
wrote:

> I'm using Numeric.arange and I have different results depending on the 
> numbers I'm using.
>
> For example:
> arange(9, 12.1, 0.1, Float)
> array([  9. ,   9.1,   9.2,   9.3,   9.4,   9.5,   9.6,   9.7,   9.8, 
> 9.9,  10. ,
>              10.1,  10.2,  10.3,  10.4,  10.5,  10.6,  10.7,  10.8, 
> 10.9,  11. ,
>              11.1,  11.2,  11.3,  11.4,  11.5,  11.6,  11.7,  11.8, 
> 11.9,  12. ])
>
>
> arange(18, 21.1, 0.1, Float)
> array([ 18. ,  18.1,  18.2,  18.3,  18.4,  18.5,  18.6,  18.7,  18.8, 
> 18.9,  19. ,
>              19.1,  19.2,  19.3,  19.4,  19.5,  19.6,  19.7,  19.8, 
> 19.9,  20. ,
>              20.1,  20.2,  20.3,  20.4,  20.5,  20.6,  20.7,  20.8, 
> 20.9,  21. ,
>              21.1])
>
> The difference is that the first one doesn't include the max value, 
> but the second one do this.
>
> Do you know how to solve this?
>
> I'm using python enthought edition.

It's everywhere:

 >>> arange(1.0e-18, 2.0e-18, 1.0e-19, Float)
array([  1.00000000e-18,   1.10000000e-18,   1.20000000e-18,   
1.30000000e-18,
               1.40000000e-18,   1.50000000e-18,   1.60000000e-18,   
1.70000000e-18,
               1.80000000e-18,   1.90000000e-18,   2.00000000e-18])
 >>> arange(2.0e-18, 3.0e-18, 1.0e-19, Float)
array([  2.00000000e-18,   2.10000000e-18,   2.20000000e-18,   
2.30000000e-18,
               2.40000000e-18,   2.50000000e-18,   2.60000000e-18,   
2.70000000e-18,
               2.80000000e-18,   2.90000000e-18])

This kind of floating point issue is present in some form on any 
platform and language. However, Numeric.arange() makes it possible to 
"automate" the error. <Flamebait> A language with ambitions to be a 
numerical computing platform should not make it this easy. 
Numeric.arange() is a dangerous function and should be eliminated.

Bottom line: Don't use it unless you know without a doubt that your 
"epsilon" will always work.

Jerry




More information about the SciPy-User mailing list