Return float problem.

Y

YunBin Lee

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?
 
J

John Machin

YunBin said:
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
 
Y

YunBin Lee

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?
 
J

John Machin

YunBin said:
John, Thanks for your help!


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.
 
Y

YunBin Lee

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!
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top