471,356 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Another C API Question

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

Jul 27 '07 #1
9 1600
beginner wrote:
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

Jul 27 '07 #2
beginner wrote:
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.
Jul 27 '07 #3
Hi Robert,

On Jul 26, 8:16 pm, Robert Kern <robert.k...@gmail.comwrote:
beginner wrote:
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.
Thanks a lot for your help.

Best regards,
beginner
Jul 27 '07 #4
Hi Farshid,

On Jul 26, 8:18 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
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.
This works with PyFloat only. It does not work with integers.

Thanks,
b

Jul 27 '07 #5
beginner wrote:
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.
Jul 27 '07 #6
On Jul 27, 11:37 am, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
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.
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 ...

Jul 27 '07 #7
beginner wrote:
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?
Jul 27 '07 #8
On Jul 27, 4:50 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
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?
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. :-)

Jul 27 '07 #9
On Jul 27, 4:50 pm, Farshid Lashkari <n...@spam.comwrote:
beginner wrote:
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?
I am using 2.5.1 and Windows XP. Thanks for your help.

Jul 27 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by terence.parker | last post: by
reply views Thread by Sebastian Hiller | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.