[Numpy-discussion] math.exp underflow (was: exp behaviour: is it a bug ?)

Charles G Waldman cgw at fnal.gov
Fri Jun 2 12:36:55 EDT 2000


Czerminski, Ryszard writes:
 > 
 > Is the behaviour illustrated below a bug or the Python's feature ?

I don't know why you're posting this to the NumPy discussion list,
since the
"math" module is part of the standard Python distribution, not NumPy.

Anyhow, the differing behaviors people are seeing are just coming from
differences in the system math libraries:

On Linux (Intel):

Python 1.6a2 (#9, May 22 2000, 12:34:51)  [GCC 2.95.2 19991024 (release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
>>> from math import exp
>>> x = -706.
>>> while x > -900:
...    x = x - 1
...    print 'x, exp(x) = %g %g' % (x, exp(x))
... 
x, exp(x) = -707 8.99086e-308
x, exp(x) = -708 3.30755e-308
<snip>
x, exp(x) = -745 4.94066e-324
x, exp(x) = -746 0
<snip>
x, exp(x) = -899 0
x, exp(x) = -900 0
>>> 


Wheras on Irix (MIPS):
Python 1.6a2 (#8, Jun  1 2000, 20:01:55) [C] on irix646
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
>>> from math import exp
>>> x = -706.
>>> while x > -900:
...    x = x - 1
...    print 'x, exp(x) = %g %g' % (x, exp(x))
... 
x, exp(x) = -707 8.99086e-308
x, exp(x) = -708 3.30755e-308
x, exp(x) = -709 0
x, exp(x) = -710 0
<snip>
x, exp(x) = -745 0
Traceback (most recent call last):
  File "<stdin>", line 3, in ?
OverflowError: math range error


>From the Irix man page for exp(3M):

     The exp functions return HUGE_VAL when the correct value would overflow,
     and return zero if the correct value would underflow. The -lm and -lmx
     versions set the value of errno to ERANGE for both underflow and
     overflow.

Since the Python math module sees errno set after the call to exp, it
raises an exception.

Whereas on Linux, exp(-very big number) simply returns 0 and does not
set errno.

On the one hand, Python's behavior makes sense because it simply
reflects the behavior of the system math libraries.  On the other
hand, these kinds of differences make it hard to write portable code -
you could test on Linux and think everything is OK, then run on IRIX
and get exceptions.  Maybe that's just the way life is when you are
using floating-point math... Tim Peters may have more to say on this
topic <exp(-900)wink>












More information about the NumPy-Discussion mailing list