Return float problem.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • YunBin Lee

    #1

    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(PyOb ject *self, PyObject *args)
    {
    double n=0.0,result=0. 0;
    if (!PyArg_ParseTu ple (args,"d:fact", &n))
    return NULL;
    result = fact(n);
    result = fact(result);
    return Py_BuildValue(" d",result);
    }

    PyObject *wrap_test(PyOb ject *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.00000 0 <-- Why not is 0.444?
    -1140850688.0 <--- Why not is 0.444?

  • John Machin

    #2
    Re: Return float problem.

    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(PyOb ject *self, PyObject *args)
    {
    double n=0.0,result=0. 0;
    if (!PyArg_ParseTu ple (args,"d:fact", &n))
    return NULL;
    result = fact(n);
    result = fact(result);
    return Py_BuildValue(" d",result);
    }
    >
    PyObject *wrap_test(PyOb ject *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.00000 0 <-- 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

    Comment

    • YunBin Lee

      #3
      Re: Return float problem.

      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?

      Comment

      • John Machin

        #4
        Re: Return float problem.

        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.

        Comment

        • YunBin Lee

          #5
          Re: Return float problem.

          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....@gma il.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?

          Comment

          Working...