By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Return float problem.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.