C Module's '1.#INF' changes to 'inf' at Python

Robert Kern robert.kern at gmail.com
Fri Jan 8 10:36:44 EST 2010


On 2010-01-08 07:48 AM, CELEN Erman wrote:
> Hi All,
>
> My problem is that I’ve noticed a strange behavior in Python while
> handling FPEs on Windows after switching compilers (msvc8 to msvc9) and
> I am trying to find out how Python handles INF values to figure out
> where the problem might be.
>
> The problem appeared when we noticed that behavior of Numeric (older
> version of NumPy) library has changed when dealing with Floating-Point
> Exceptions. We are building our own slightly modified version of Python
> in-house (including Numeric/NumPy) and when we switched from the msvc8
> to msvc9 compiler, Numeric started to return ‘-inf’ as a result of
> ‘Numeric.log10(0.0)’ instead of rasing an OverflowError. I know that
> Numeric is using umath instead of the math library and since
> math.log10(0.0) is still raising an Error (which is ValueError in 2.6
> and OverflowError in 2.4, but still an error) I thought that it might
> have something to do with how umath or Numeric handles the input or
> return values of log functions.

Numeric.log10() will check to see if the errno was set to ERANGE. It does not 
check if a floating point exception flag was set, which is tricky to do across 
platforms. The newer numpy can do it because we've finally managed to implement 
all of that platform-specific code, but the earlier Numeric does not. 
Presumably, the msvc8 C runtime's implementation of log10() sets errno and the 
msvc9 runtime's version does not.

> Before hunting blindly I wanted to see how Python interprets INF
> results. I built a simple module with only one function that causes an
> FPE with log10(0.0) using C’s math library and prints the results with
> printf. Then I imported that module from python, called the function and
> printed the result in python too to see if there are any differences
> between two values (which I found out that there are)

Python 2.6 changed the string representations of the special floating point 
values inf and nan. Previously, the string representation was left up to the C 
runtime, so they differed between platforms. Python 2.6 normalized the string 
representation across all platforms. The underlying values are the same. What 
version of Python are you using?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list