473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Return float problem.

Hi, all.
I have a problem of float return error in python c binding module.
Below is my c sources, compile commands and result of program.

========== wrap.c =========
#include <Python.h>

PyObject *wrap_fact(PyObject *self, PyObject *args)
{
double n=0.0,result=0.0;
if (!PyArg_ParseTuple (args,"d:fact",&n))
return NULL;
result = fact(n);
result = fact(result);
return Py_BuildValue("d",result);
}

PyObject *wrap_test(PyObject *self, PyObject *args)
{
double value = 0.124;
return Py_BuildValue ("d", value);
}

static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Simple return a float"},
{"test", wrap_test, METH_VARARGS, "Test"},
{NULL,NULL}
};

void initexample()
{
PyObject *m;
m = Py_InitModule ("example", exampleMethods);
}
========== end wrap.c =========

========== example.c =========
#include <stdio.h>

double fact(double n)
{
printf ("n=%f\n",n);
return n;
}

int main(int argc,char *argv[])
{
printf ("%f\n",fact(0.1234));
}
========== end example.c =========

complied with commands:
lyb@ubuntu:~$ gcc -fpic -c -I. -I.. -I/usr/include/python2.4/ example.c
wrap.c
lyb@ubuntu:~$ gcc -shared -o example.so example.o wrap.o

then i used the example.so with command:
lyb@ubuntu:~$ python -c "import example; print example.fact(0.444)"
n=0.444000
n=-103079215.000000 <-- Why not is 0.444?
-1140850688.0 <--- Why not is 0.444?

Nov 25 '06 #1
4 1500
YunBin Lee wrote:
Hi, all.
I have a problem of float return error in python c binding module.
Below is my c sources, compile commands and result of program.
There seems to be a missing file: example.h. If that hint isn't
sufficient, please continue reading.
>
========== wrap.c =========
#include <Python.h>

PyObject *wrap_fact(PyObject *self, PyObject *args)
{
double n=0.0,result=0.0;
if (!PyArg_ParseTuple (args,"d:fact",&n))
return NULL;
result = fact(n);
result = fact(result);
return Py_BuildValue("d",result);
}

PyObject *wrap_test(PyObject *self, PyObject *args)
{
double value = 0.124;
return Py_BuildValue ("d", value);
}

static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Simple return a float"},
{"test", wrap_test, METH_VARARGS, "Test"},
{NULL,NULL}
};

void initexample()
{
PyObject *m;
m = Py_InitModule ("example", exampleMethods);
}
========== end wrap.c =========

========== example.c =========
#include <stdio.h>

double fact(double n)
{
printf ("n=%f\n",n);
return n;
}

int main(int argc,char *argv[])
{
printf ("%f\n",fact(0.1234));
}
========== end example.c =========

complied with commands:
lyb@ubuntu:~$ gcc -fpic -c -I. -I.. -I/usr/include/python2.4/ example.c
wrap.c
Didn't this produce any warning messages, like:

wrap.c:8: warning: implicit declaration of function `fact'

?
lyb@ubuntu:~$ gcc -shared -o example.so example.o wrap.o

then i used the example.so with command:
lyb@ubuntu:~$ python -c "import example; print example.fact(0.444)"
n=0.444000
n=-103079215.000000 <-- Why not is 0.444?
-1140850688.0 <--- Why not is 0.444?
Did you notice that the garbage is a whole number? Why? Because you
have been (more or less) plucking ints out of some arbitrary place on
the stack, floating them, and printing them.

HTH,
John

Nov 25 '06 #2
John, Thanks for your help!
Did you notice that the garbage is a whole number? Why? Because you
have been (more or less) plucking ints out of some arbitrary place on
the stack, floating them, and printing them.
I don't know how to solve it (or the right way), could you give me some
examples?

Nov 26 '06 #3
YunBin Lee wrote:
John, Thanks for your help!
Did you notice that the garbage is a whole number? Why? Because you
have been (more or less) plucking ints out of some arbitrary place on
the stack, floating them, and printing them.

I don't know how to solve it (or the right way), could you give me some
examples?
Is this homework?

Examples of what? Do you want me to write your code for you?

Examples? The Internet is loaded with *working* example C code split
over several .c and .h files.

How much C have you done? In other words, what is your level of
expertise? I would have thought that function prototypes and include
files would be something you would have already mastered before you
tried to write a Python extension wrapper for some C code.

You have been given 3 big fat hints. If you didn't understand any of
them, after reference to your C textbook and Googling, please feel free
to ask again.

Nov 26 '06 #4
Hello john.
I solve it by add '#include "example.h"' into wrap.c, recompiled all
c-sources, and it works on the right way!

Content of example.h:
========== example.h =========
#ifndef __EXAMPLE_H__
#define __EXAMPLE_H__

double fact(double n);

#endif
========== end example.h =========

Anyway, Thanks for you answer again!

On 11ÔÂ26ÈÕ, ÉÏÎç11ʱ12·Ö, "YunBin Lee" <li.yun....@gmail.com>
wrote:
John, Thanks for your help!
Did you notice that the garbage is a whole number? Why? Because you
have been (more or less) plucking ints out of some arbitrary place on
the stack, floating them, and printing them.I don't know how to solve it (or the right way), could you give me some
examples?
Nov 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ahmed Ossman | last post by:
hi, I want to save the return value from the signal() function, what is the type of the variable. i.e. I want to do the following: <variable type?> ret_sig = signal(......); I need it soo...
7
by: Victor Hannak | last post by:
I have a class with the following public function: float FuncClass::GetResult() { try { return(Result); } catch (...) { WriteString("FuncClass::GetResult ERROR: Exception encountered");...
2
by: Aryeh M. Friedman | last post by:
If I have something like this: class NumberException { }; class Number { public: ... virtual unsigned long getValue() {throw(NumberException);}; ...
3
by: Pushker Pradhan | last post by:
I have a function which should allocate memory, initialize it to some values and return the address of the initialized memory to the calling function. void getWaveletCoeffs(float *ld, float *hd,...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
7
by: rolan | last post by:
Hi, I'm having troubles with a returned float array which seems to think it's a single float. Axis is defined as a float on a public scope. void axisRotation(float v1, float v2){ float *v3,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.