[Numpy-tickets] [NumPy] #924: problem with summing large array of float32

NumPy numpy-tickets@scipy....
Fri Oct 3 00:40:24 CDT 2008


#924: problem with summing large array of float32
-------------------------------+--------------------------------------------
 Reporter:  emil               |        Owner:  somebody
     Type:  defect             |       Status:  closed  
 Priority:  high               |    Milestone:          
Component:  numpy.core         |      Version:  1.0.1   
 Severity:  critical           |   Resolution:  wontfix 
 Keywords:  sum, dot, float32  |  
-------------------------------+--------------------------------------------
Changes (by rkern):

  * status:  new => closed
  * resolution:  => wontfix

Comment:

 Yup, that's floating point arithmetic for you. At 32-bit precision, adding
 1 to 16777216 gives you 16777216 back again because 16777217 cannot be
 represented at 32-bit precision.

 {{{

 In [1]: from numpy import *

 In [2]: x = float32(16777216)

 In [3]: one = float32(1.0)

 In [4]: x
 Out[4]: 16777216.0

 In [5]: x + one
 Out[5]: 16777216.0

 }}}

 With the multiple sums across each axis, you are accumulating separate
 larger numbers *then* adding them together. Fortunately, the intermediates
 and the final result are multiples of reasonable powers of 2, so you can
 afford to drop off some of the low bits. If the final result were
 something like 110880003, that number cannot even be represented in 32-bit
 precision.

 You can use {{{sum(a, dtype=float64)}}} to use a double-precision
 accumulator for the sum and thus be able to be more accurate for larger
 inputs.

-- 
Ticket URL: <http://scipy.org/scipy/numpy/ticket/924#comment:1>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list