Q&A's, tips, howto's
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 4:21 pm
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
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 4:37 pm
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.... ;-)
Last edited by
pjot on Sat Nov 06, 2004 4:40 pm, edited 2 times in total.
eddier
Posts: 289 Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US
Post
by eddier » Sat Nov 06, 2004 4:40 pm
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
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 4:47 pm
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.
eddier
Posts: 289 Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US
Post
by eddier » Sat Nov 06, 2004 5:21 pm
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
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 5:29 pm
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?
Last edited by
pjot on Sat Nov 06, 2004 5:29 pm, edited 1 time in total.
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 5:29 pm
looks like an "Endian" issue to me...
-- (define? (Cornflakes))
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:33 pm
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
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 5:34 pm
never take a posting too literaly...:-)
> (unpack "lf" 1.2)
Segmentation fault
:-)
-- (define? (Cornflakes))
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 5:36 pm
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
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:41 pm
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
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 5:43 pm
Great! This is what I was looking for.
Thanks Lutz!!
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:47 pm
Sorry doesn't work ... but should ... investigating ...
Lutz
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 5:52 pm
.. its floating segmf's today...:-)
> (get-integer (get-float (pack "lf" (float 12314127.641234))))
Segmentation fault
-- (define? (Cornflakes))
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:57 pm
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
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 5:58 pm
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
Last edited by
pjot on Sat Nov 06, 2004 5:58 pm, edited 1 time in total.
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:58 pm
Norman, Norman ... too much of that dark beer ;)
Lutz
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 5:59 pm
yes, it works just fine, I forgot to compile
Lutz
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 5:59 pm
Yeah he is drinking too much... ;-)
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 6:06 pm
Yes we call it "Dentergems Double" But it floats good ;-)
-- (define? (Cornflakes))
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 6:09 pm
I am still waiting for that case of "Dentergems Double" you guys promised me some time ago ...
Lutz
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 6:11 pm
Heee Lutz yes your right ;-)
Pjot..how about it? Your traveling to the states next week!
Cant you post a case for Lutz?
-- (define? (Cornflakes))
pjot
Posts: 733 Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:
Post
by pjot » Sat Nov 06, 2004 6:12 pm
Well, I will be in Utah, not Florida unfortunately... ;-)
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Sat Nov 06, 2004 6:16 pm
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
newdep
Posts: 2038 Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands
Post
by newdep » Sat Nov 06, 2004 6:19 pm
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 ;-)
-- (define? (Cornflakes))