Re: Problems with C floats
- Posted by mattlewis (admin) Jan 31, 2010
- 2379 views
irv said...
irv said...
Yes, that is it (eubins). I compiled 3058 from source, and it works ok.
HOWEVER - while sending floats now works, getting them back from a c routine still fails!
I updated my test to:
// irv.c // build with $ gcc -shared -fPIC -ldl irv.c -o irv.so #include <stdio.h> void test_float( float foo, double bar, int baz ){ printf( "double: %e\nfloat: %e\nint: %d\n", foo, bar, baz ); } float test_irv( int *ptr, float f1, float f2, float f3, int i ){ printf( "ptr: %p\nf1: %f (%x)\nf2: %f (%x)\nf3: %f (%x)\ni: %d\n", ptr, f1, *(int*)(&f1), f2, *(int*)(&f2), f3, *(int*)(&f3), i ); return f1 + f2; }
...and...
-- irv.ex include std/dll.e constant irv = open_dll( "./irv.so" ), test_float = define_c_proc( irv, "test_float", {C_FLOAT, C_DOUBLE, C_INT}), test_irv = define_c_func( irv, "test_irv", {C_POINTER, C_FLOAT, C_FLOAT, C_FLOAT, C_INT}, C_FLOAT ) c_proc( test_float, { 0.25, 0.75, 9}) ? c_func( test_irv, { 0xDEADBEEF, 1.1, 2.3, 0.5, 8008135} )
And I get:
$ eui irv double: 2.500000e-01 float: 7.500000e-01 int: 9 ptr: 0xdeadbeef f1: 1.100000 (3f8ccccd) f2: 2.300000 (40133333) f3: 0.500000 (3f000000) i: 8008135 3.399999976
Or did you mean as a callback? That shouldn't have ever worked. A euphoria callback routine assumes that it's getting back all 32-bit integers, so you'd need to convert the float:
function my_callback( atom the_float ) poke4( buffer, the_float ) the_float = float32_to_atom( peek( { buffer, 4 } ) ) return 0 end function
Matt