Page 1 of 1
Passing FLOAT's
Posted: Sat Nov 06, 2004 4:21 pm
by pjot
Hi,
Using this Shared Object:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int int_print (int i)
{
printf("%d\n", i);
return(0);
}
int float_print (float f)
{
printf("%f\n", f);
return(0);
}
...and the following newLisp code:
Code: Select all
#!/usr/bin/newlisp
(import "./lib.so" "int_print")
(import "./lib.so" "float_print")
(int_print 345)
(float_print 1.2)
(exit)
...delivers this result:
345
0.000000
It seems that passing a float to a C function does not work? Or is there something wrong in the code?
Also, is it possible to pass an array to a C function?
Peter
Posted: Sat Nov 06, 2004 4:37 pm
by pjot
Passing an INT array to a C function works:
Code: Select all
C-code:
int array_print (int i[])
{
printf("%d\n", i[0]);
printf("%d\n", i[1]);
return(0);
}
--------------------------------------------------
NewLisp:
(import "./lib.so" "array_print")
(array_print (append (pack "lu" 16432) (pack "lu" 54321)))
An array with floats also works fine this way. Now for the single floats.... ;-)
Posted: Sat Nov 06, 2004 4:40 pm
by eddier
I've seen this error (%f and double) or (%lf and float) many times in plain C. I bet if you change float to double you will have no problem.
Eddie
Posted: Sat Nov 06, 2004 4:47 pm
by pjot
No; suppose I change my C-function this way:
Code: Select all
int float_print (float f)
{
printf("%f\n", f);
printf("%g\n", f);
printf("%e\n", f);
return(0);
------------------------------------------
(import "./float.so" "float_print")
(float_print 1.2)
}
Then the result is:
0.000000
4.17233e-08
4.172325e-08
Strange! Also PACKing within newLisp does not help me here.
Posted: Sat Nov 06, 2004 5:21 pm
by eddier
Yes, but doesn't newlisp use doubles internally and not floats?
in (float_print 1.2) the 1.2 is being passed as a double isn't it?
Eddie
Posted: Sat Nov 06, 2004 5:29 pm
by pjot
So you mean it's never going to work anyway?
Even if it is passed as double, at least one of the PRINTF modifiers should show the number correctly then?
Posted: Sat Nov 06, 2004 5:29 pm
by newdep
looks like an "Endian" issue to me...
Posted: Sat Nov 06, 2004 5:33 pm
by Lutz
Code: Select all
int float_print(double num)
{
printf(">>%lf<<", num);
return(0);
}
gcc test.c -shared -o testlib.so
newLISP v.8.2.6 Copyright (c) 2004 Lutz Mueller. All rights reserved.
> (import "/usr/home/nuevatec/testlib.so" "float_print")
float_print <281A153C>
> (float_print 1.234)
>>1.234000<<0
>
Eddie is right: newLISP uses double
Lutz
Posted: Sat Nov 06, 2004 5:34 pm
by newdep
never take a posting too literaly...:-)
> (unpack "lf" 1.2)
Segmentation fault
:-)
Posted: Sat Nov 06, 2004 5:36 pm
by pjot
Yes I understand, but this is too bad; it means that many C-library's cannot accept a 'float' coming in from newLisp. There are functions which declare their arguments as float, I cannot pass float values to them.
Posted: Sat Nov 06, 2004 5:41 pm
by Lutz
yes you can:
(get-integer (pack "f" 1.23)) => 1067282596
The integer coming out is what you pass to the function which takes float.
Lutz
Posted: Sat Nov 06, 2004 5:43 pm
by pjot
Great! This is what I was looking for.
Thanks Lutz!!
Posted: Sat Nov 06, 2004 5:47 pm
by Lutz
Sorry doesn't work ... but should ... investigating ...
Lutz
Posted: Sat Nov 06, 2004 5:52 pm
by newdep
.. its floating segmf's today...:-)
> (get-integer (get-float (pack "lf" (float 12314127.641234))))
Segmentation fault
Posted: Sat Nov 06, 2004 5:57 pm
by Lutz
Works just fine, I forgot to compile:
#include <stdio.h>
int float_print(float num)
{
printf(">>%f<<", num);
return(0);
}
> (import "/usr/home/nuevatec/testlib.so" "float_print")
float_print <281A153C>
> (float_print (get-integer (pack "f" 1.234)))
>>1.234000<<0
>
Lutz
Posted: Sat Nov 06, 2004 5:58 pm
by pjot
I don't want to be disturbing but it works for me!
Code: Select all
int float_print (float f)
{
printf("%f\n", f);
printf("%g\n", f);
printf("%e\n", f);
return(0);
}
-----------------------------------------------
NewLisp:
(import "./float.so" "float_print")
(float_print (get-integer (pack "f" 1.23)))
Result:
1.230000
1.23
1.230000e+00
Posted: Sat Nov 06, 2004 5:58 pm
by Lutz
Norman, Norman ... too much of that dark beer ;)
Lutz
Posted: Sat Nov 06, 2004 5:59 pm
by Lutz
yes, it works just fine, I forgot to compile
Lutz
Posted: Sat Nov 06, 2004 5:59 pm
by pjot
Yeah he is drinking too much... ;-)
Posted: Sat Nov 06, 2004 6:06 pm
by newdep
Yes we call it "Dentergems Double" But it floats good ;-)
Posted: Sat Nov 06, 2004 6:09 pm
by Lutz
I am still waiting for that case of "Dentergems Double" you guys promised me some time ago ...
Lutz
Posted: Sat Nov 06, 2004 6:11 pm
by newdep
Heee Lutz yes your right ;-)
Pjot..how about it? Your traveling to the states next week!
Cant you post a case for Lutz?
Posted: Sat Nov 06, 2004 6:12 pm
by pjot
Well, I will be in Utah, not Florida unfortunately... ;-)
Posted: Sat Nov 06, 2004 6:16 pm
by Lutz
In Utah on Sundays they don't serve beer with more than 6% alcohol content . In Florida they don't sell beer in supermarkets before noon on Sundays (so people don't come drunken to church :) )
Lutz
Posted: Sat Nov 06, 2004 6:19 pm
by newdep
hahahah..sounds logic... Well here in The Netherlands we have this standard
environment-rule "where a church is is a bar" ;-) So actualy every town with a church
has the bar across the street ;-) Im wont explain the results ;-)