Can't exponentiate zero in NumPy

Tim Peters tim.one at comcast.net
Mon Sep 9 13:06:01 EDT 2002


[Rick Muller]
> I found out more about this problem, and I'm still hoping that someone
> can suggest a good fix.
>
> The problem comes when I take a power of a NumPy array that has very
> small values in it:
>
>  >>> from Numeric import *
>  >>> a = zeros(3,Float)
>  >>> a**2
> array([0., 0., 0.])
>  >>> a[1] = 1.e-310
>  >>> a**2
> Traceback (most recent call last):
>     File "<stdin>", line 1, in ?
> OverflowError: math range error
>
> This crashes on Linux, but not on Mac OS X.

Does this require wrapping in a Numeric array, or does it also happen in
*any* of these straight Python expressions?:

    1e-310 ** 2
    pow(1e-310, 2)
    import math
    math.pow(1e-310, 2)

    1e-310 ** 2.1
    pow(1e-310, 2.1)
    math.pow(1e-310, 2.1)

In addition, exactly which version of Python are you using (2.0, 2.1, 2.1.1,
2.1.2, 2.1.3, 2.2, 2.2.1, ...)?

> Is this a bug or a feature?

You first <wink>.  Python intends silent underflow to 0, but C99 changed the
rules for how errno works, and there was some class of related problem in
core Python right after glibc started exploiting the looser C99 rules.  As
far as we know that's been fixed.





More information about the Python-list mailing list