Problem passing float to C

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problem passing float to C

Sepp Meier
Hi all

I want to pass a float from C to python and from
python to c with getters and setters in oo-style, but
it doesn't work.

(The same construct with an Integer works)

Every suggestion is appreciated.


/*test.c*/-------------------------------------------

struct Test{
  int integer;
  float fpoint;
};

struct Test* newTest(){
  struct Test* test = (struct
Test*)malloc(sizeof(struct Test));
  return test;
}

int getInt(struct Test* t){
  return t->integer;
}

float getFloat(struct Test* t){
  return t->fpoint;
}

void setInt(struct Test* t, int i){
  t->integer = i;
}

void setFloat(struct Test* t, float f){
  t->fpoint = f;
}

/*test.i*/-------------------------------------------
//test.i

%module test
%{
#include <python/Python.h>
%}

%inline
%{
  struct Test{
    int integer;
    float fpoint;
  };
%}

%extend Test {
  void setInt(int i){setInt(self, i);}

  void setFloat(float f){setFloat(self, f);}

  int getInt(){return getInt(self);}

  float getFloat(){return setInt(self);}
};

struct Test* newTest();


python output----------------------------------------
>>> import test
>>> t = test.newTest()
>>> t.getInt()
134985952
>>> t.setInt(123)
>>> t.getInt()
123
>>> t.integer
123
>>> t.setFloat(1.4)
>>> t.getFloat()
1.0
>>> t.fpoint
3.6893488147419103e+19
>>> t.fpoint = 234.234
>>> t.getFloat()
1.0
>>> t.fpoint
234.23399353027344
>>>    


       

       
               
___________________________________________________________
Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user