[NumPy-Tickets] [NumPy] #1681: arange function give inconsistence result

NumPy Trac numpy-tickets@scipy....
Fri Nov 19 13:59:34 CST 2010


#1681: arange function give inconsistence result
--------------------------+-------------------------------------------------
 Reporter:  hongdog       |       Owner:  somebody
     Type:  defect        |      Status:  new     
 Priority:  normal        |   Milestone:  2.0.0   
Component:  Other         |     Version:  1.3.0   
 Keywords:  arange 1.3.0  |  
--------------------------+-------------------------------------------------

Comment(by m-paradox):

 This is the kind of thing normally expected with binary floating-point
 math.  In this case, what's curious is that the 12.9 in both arrays is the
 same:

 {{{
 >>> import numpy as np
 >>> a = np.arange(12.5,13,0.1); a
 array([ 12.5,  12.6,  12.7,  12.8,  12.9])
 >>> b = np.arange(12.6,13,0.1); b
 array([ 12.6,  12.7,  12.8,  12.9,  13. ])
 >>> a[-1] == b[-2]
 True
 }}}

 Examining the code (PyArray_Arange in ctors.c), we see the length is
 calculated as follows:

 {{{
 _safe_ceil_to_intp((stop - start)/step, &length)
 }}}

 Which in this case both become 5 because of differing round-off:

 {{{
 >>> (13.-12.5)/.1
 5.0
 >>> (13.-12.6)/.1
 4.0000000000000036
 }}}

 It's normal that it would truncate in one direction when doing the divide,
 but the other way when adding/multiplying in the code that later computes
 the values, so I don't think that one could expect consistent results from
 this interface.  It's better to use linspace for consistent results.  See:

 http://projects.scipy.org/numpy/ticket/1564

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1681#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list