[C++-sig] double vs float in Py_BuildValue

David Abrahams dave at boost-consulting.com
Mon Jun 27 01:31:24 CEST 2005

Gary Robinson <grobinson at goombah.com> writes:

> If the float, gfSumChiSquare, is defined as a float, as above, it works 
> fine.
> But  if I turn it into a double:
>     static double         gfSumChiSquare = 0.0; 
>  ...
>     static PyObject *    
>     getSumChiSquare(PyObject *self, PyObject *args){
>         return Py_BuildValue("d", gfSumChiSquare);
>         }
>  ...
> I get a bus error when I call it from Python.
> I'm extremely rusty in C, not having used it in about 7 years, so maybe 
> that's what's responsible. But I would appreciate some insight here!

What I remember (without looking it up) is that floats and doubles are
both passed as doubles in C.  So the format character for a double
should also be "f."  I'm guessing "d" indicates an int or something.

Oh, well, according to http://docs.python.org/api/arg-parsing.html:

"d" (float) [double]
    Convert a C double to a Python floating point number.

"f" (float) [float]
    Same as "d".

So I don't know what to tell ya.  I'm guessing you have a bug
elsewhere in your code.  Throw a debugger at it? Purify?

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list