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 9 1685
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
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.
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
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
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.
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 ...
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?
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. :-)
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fig000 |
last post by:
Hi,
I'm relatively new to Javascript so please bear with me on what
might sound like silly questions.
This is what I want to do:
I'm working in classic asp (I have to for this project). I...
|
by: anon |
last post by:
Post Forwarding question......
For this control below,
<asp:Button runat="server" PostTargetUrl="page2.aspx" />
The Attribute: PostTargetUrl="page2.aspx"
Is this PostTargetUrl Attribute...
|
by: terence.parker |
last post by:
I am often faced with the dilemma of whether to use a JOIN query across
three tables in order to grab a bunch of results - or whether to create
another table to represent what I want. The latter is...
|
by: Sebastian Hiller |
last post by:
Hello,
i'm new to .Net (i'm using VB as language and i'm working in the
code-behind mode) and i can't solve the following problem:
I have a WebForm and want to Add a UserControl...
|
by: Brad |
last post by:
I have another hopefully simple question. I am so used to writing VB .Net
windows apps that there are some things in ASP .Net that just does not easily
cross over. I know how to pass variables to...
|
by: Javier Martinez |
last post by:
Hi
I have asp application in a machine with a virtual directory referring a
shared directory in another machine
When I try to load any aspx page of my portal I get the following error:
...
|
by: Eric_Dexter |
last post by:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename + "orc"
for line in alllines:
if not...
|
by: David Mathog |
last post by:
On a Solaris 8 system if a user "joe" logs in, for instance via ssh,
cuserid() returns "joe". That's the expected behavior and so far so
good. However if that user then does:
% su - sally
...
|
by: shookim |
last post by:
I don't care how one suggests I do it, but I've been searching for days
on how to implement this concept. I'm trying to use some kind of grid
control (doesn't have to be a grid control, whatever...
|
by: MichaelK |
last post by:
Hello.
I have all data already collected on the current page?
I want to open another window with the form, fill the fields and submit that
form.
So basically the question is how can I fill all...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |