[Pyrex] Numerical bugs in pyrex

Mario Pernici Mario.Pernici at mi.infn.it
Tue Jun 29 01:25:38 CEST 2004


Hello.

I find the following numerical bugs in pyrex-0.9.2.
My system is Linux, Debian, i686, gcc 3.3.3.

# example.pyx
cdef extern from "math.h":
  double M_PI

cdef unsigned long int n1
n1 = 4293858116

cdef double pi
pi = 3.14159265358979323846

def main():
  print n1
  print "%.18f" % M_PI
  print "%.18f" % (<float> M_PI)
  print "%.18f" % pi
#-----------------------------------

The output is
-1109180
3.141592653589793116
3.141592741012573242
3.141592653590000062

Regarding the unsigned long number,
replacing in the pyrexc-generated example.c
 __pyx_1 = PyInt_FromLong(__pyx_v_7example_n1);
 with
 __pyx_1 = PyLong_FromUnsignedLong(__pyx_v_7example_n1);
 the unsigned long number 4293858116 is correctly obtained.

I get this with the following diff in PyrexTypes.py
< class CULongType(CIntType):
<     to_py_function = "PyLong_FromUnsignedLong"
<     from_py_function = "PyLong_AsUnsignedLong"
642,643c639
<
< c_ulong_type = CULongType(3,0)
---
> c_ulong_type =     CIntType(3, 0, "T_ULONG")

Regarding the double, observe that the loss of precision in the output for
pi cannot be due to the cast to a float:
3.14159265358979323846  M_PI
3.141592653589793116    output for M_PI  (16 significative digits correct)
3.141592741012573242    output for <float>M_PI  (8 significative digits 
correct)
3.141592653590000062    output for pi (12 significative digits correct)

Bye
  Mario








More information about the Pyrex mailing list