Passing FLOAT's

Started by pjot, November 06, 2004, 08:21:39 AM

Previous topic - Next topic

pjot

Hi,



Using this Shared Object:

#include <stdio.h>
#include <stdlib.h>

int int_print (int i)
{
printf("%dn", i);
return(0);
}
int float_print (float f)
{
printf("%fn", f);
return(0);
}

...and the following newLisp code:

#!/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

pjot

#1
Passing an INT array to a C function works:



C-code:

int array_print (int i[])
{
printf("%dn", i[0]);
printf("%dn", 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.... ;-)

eddier

#2
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

pjot

#3
No; suppose I change my C-function this way:

int float_print (float f)
{
printf("%fn", f);
printf("%gn", f);
printf("%en", 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.

eddier

#4
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

pjot

#5
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?

newdep

#6
looks like an "Endian" issue to me...
-- (define? (Cornflakes))

Lutz

#7

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

newdep

#8
never take a posting too literaly...:-)



> (unpack "lf" 1.2)

Segmentation fault



:-)
-- (define? (Cornflakes))

pjot

#9
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.

Lutz

#10
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

pjot

#11
Great! This is what I was looking for.



Thanks Lutz!!

Lutz

#12
Sorry doesn't work ... but should ... investigating ...



Lutz

newdep

#13
.. its floating segmf's today...:-)



> (get-integer (get-float (pack "lf" (float 12314127.641234))))

Segmentation fault
-- (define? (Cornflakes))

Lutz

#14
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