Return float problem.

2006-11-25 Thread 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 

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 

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:
[EMAIL PROTECTED]:~$ gcc -fpic -c -I. -I.. -I/usr/include/python2.4/ example.c
wrap.c
[EMAIL PROTECTED]:~$ gcc -shared -o example.so example.o wrap.o

then i used the example.so with command:
[EMAIL PROTECTED]:~$ python -c "import example; print example.fact(0.444)"
n=0.444000
n=-103079215.00 <-- Why not is 0.444?
-1140850688.0 <--- Why not is 0.444?

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Return float problem.

2006-11-25 Thread 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?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Return float problem.

2006-11-25 Thread 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!

On 11月26日, 上午11时12分, "YunBin Lee" <[EMAIL PROTECTED]>
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?

-- 
http://mail.python.org/mailman/listinfo/python-list