[Numpy-discussion] [SciPy-user] Arithmetic Errors

Travis Oliphant oliphant.travis at ieee.org
Mon Jul 17 19:10:36 CDT 2006


John Lawless wrote:
> Travis,
>
>      Thanks!
>
> 1).  I haven't found any documentation on dtype='O'.  (I purchased your
>      trelgol book but it hasn't arrived yet.)  Does 'O' guarantee no
>      wrong answers?
>   
The object data-type uses Python objects instead of low-level C-types 
for the calculations.  So, it gives the same calculations that Python 
would do (but of course it's much slower).
> 2).  My actual code was more complex than the example I posted.  It was
>      giving correct answers until I increased the dataset size.  Then,
>      luckily, the result became obviously wrong.  I can go through a
>      code and try to coerce everything to double but, when debugging a
>      large code, how can one ever be sure that all types are coerced
>      correctly if no errors are generated?
>   
NumPy uses c data-types for calculations.  It is therefore, *much* 
faster, but you have to take precautions about overflowing on integer 
operations.

> 3).  AFAIK, checking for overflows should take no CPU time whatsoever
>      unless an exception is actually generated. 
This is true for floating point operations, but you were doing integer 
multiplication.    There is no support for hardware multiply overflow in 
NumPy (is there even such a thing?). 

Python checks for overflow on integer arithmetic by doing some 
additional calculations.    It would be possible to add slower, integer 
over-flow checking ufuncs to NumPy if this was desired and you could 
replace the standard non-checking functions pretty easily.


-Travis







More information about the Numpy-discussion mailing list