[Numpy-discussion] exp

Fernando Perez Fernando.Perez at colorado.edu
Tue Aug 2 09:54:34 CDT 2005


Pieter Dumon wrote:
> Hi,
> 
> I'm having a problem with exp():
> 
>  >>>import Numeric
>  >>>a = Numeric.array([-800+0j])*Numeric.ones(10)
>  >>>Numeric.exp(a)
> 
> results in"OverflowError: math range error"
> 
> I had expected to get the cmath.exp() result:
> 
>  >>> import cmath
>  >>> cmath.exp(-800+0j)
> 0j
> 
> 
> anyone knows what I'm doing wrong ?

Numeric doesn't underflow to zero silently, nor does it offer a way to control 
the fpu's exception bits directly.  I believe that numarray does offer such 
facilities.

For exponentials of real arguments, I've overridden exp() with my own version:

def exp_safe(x):
     """Compute exponentials which safely underflow to zero.

     Slow but convenient to use. Note that NumArray will introduce proper
     floating point exception handling with access to the underlying
     hardware."""

     if type(x) is ArrayType:
         return exp(clip(x,exp_safe_MIN,exp_safe_MAX))
     else:
         return math.exp(x)

You can modify this to work with complex inputs.

Best,

f




More information about the Numpy-discussion mailing list