Another C API Question

B

beginner

Hi,

I run into another C API question. What is the simplest way to convert
an PyObject into a double?

For example, I have

PyObject *obj;

I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Thanks,
beginner
 
R

Robert Kern

beginner said:
Hi,

I run into another C API question. What is the simplest way to convert
an PyObject into a double?

For example, I have

PyObject *obj;

I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Convert it to a Python float using PyNumber_Float(), then use PyFloat_AsDouble()
to get the C double value from it.

--
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
 
F

Farshid Lashkari

beginner said:
I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Try using the PyFloat_AsDouble(...) function, it should be able to
convert an object to a double, as long as the object implements the
__float__ method.
 
B

beginner

Hi Robert,

Convert it to a Python float using PyNumber_Float(), then use PyFloat_AsDouble()
to get the C double value from it.

Thanks a lot for your help.

Best regards,
beginner
 
B

beginner

Hi Farshid,

Try using the PyFloat_AsDouble(...) function, it should be able to
convert an object to a double, as long as the object implements the
__float__ method.

This works with PyFloat only. It does not work with integers.

Thanks,
b
 
F

Farshid Lashkari

beginner said:
This works with PyFloat only. It does not work with integers.

Did you try it out? I have used it on ints, bools, and objects that
implement the __float__ method. It does not work on strings though.
 
B

beginner

Did you try it out? I have used it on ints, bools, and objects that
implement the __float__ method. It does not work on strings though.

I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.


if(oDiscount==Py_None)
{
}
else if(PyFloat_Check(oDiscount))
{
d=PyFloat_AsDouble(oDiscount);
}
else if(PyNumber_Check(oDiscount))
{
PyObject *pf=PyNumber_Float(oDiscount);
if(pfDiscount)
{
d=PyFloat_AsDouble(pfDiscount);
Py_DECREF(pfDiscount);
}
}
else ...
 
F

Farshid Lashkari

beginner said:
I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.

That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?
 
B

beginner

That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?

Interesting. Let me try it again. I definitely want to remove the
intermediate step that creates a temporary Py object. Well, Python is
probably doing it internally anyway, but at least I don't have to see
it. :)
 
B

beginner

That's strange. I just tried the following code:

fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));

And it printed the following:

True = 1.000000
False = 0.000000
5 = 5.000000

What version of Python are you using?

I am using 2.5.1 and Windows XP. Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top