[Numpy-discussion] Crash on "import numpy" [including fix]

Thomas Schreiner tmp-numpy@tschreiner....
Thu Sep 20 09:55:23 CDT 2007


I just posted a mail via gmane - I didn't expect it to hit the list, so 
here's the full context:

Using NumPy in an embedded scenario sometimes leads to crashes (floating 
point exception) depending on the compiler used. If you compile your C++ 
application with some non-g++ compilers (e.g. Borland C++ Builder), it 
will crash as soon as you try to run "import numpy".

The reason is the way in which numpy initializes the double value for 
infinity in umathmodule.c:

static double
     double mul = 1e10;
     double tmp = 0.0;
     double pinf;

     pinf = mul;
     for (;;) {
         pinf *= mul;
         if (pinf == tmp) break;
         tmp = pinf;
     return pinf;

Whereas g++ compilers will interpret this intentional overflow as 
1.#INF, applications built with Borland C++ Builder will crash, throwing 
an overflow exception.

My proposal is to replace this method by creating the correct double 
value for infinity according to the IEEE 754 specification:

static double
   char inf_string[9] = "\x00\x00\x00\x00\x00\x00\xF0\x7F";
   double pinf = ((double*)inf_string)[0];
   return pinf;

or, for big ending machines:
   char inf_string[9] = "\x7F\xF0\x00\x00\x00\x00\x00\x00";

This will always lead to the correct value for infinity, no matter what 
compiler is being used.

I already opened a ticket for this issue on

Can you imagine incorporating this fix into trunk? I'm having some 
trouble recompiling numpy myself, so I would really appreciate this fix 
to be applied upstream so that the windows installer includes it.



Thomas Schreiner, AGBS
Max Planck Institute for Biological Cybernetics
72076 Tuebingen / Germany
+49 7071 601 536

More information about the Numpy-discussion mailing list