* Passing float values to C
@ 2005-02-12 16:58 Garry
2005-02-12 18:05 ` tmoran
2005-02-13 0:38 ` Jeffrey Carter
0 siblings, 2 replies; 6+ messages in thread
From: Garry @ 2005-02-12 16:58 UTC (permalink / raw)
ADA experts, what am I doing wrong?
I want to be able to pass float values (single and double precision) to
C. I have not been successful in getting it to work.
TIA
Garry
Code follows:
/* marktime.c */
#include <stdio.h>
#include <string.h>
/* C printf int routine */
void cprintfi(char *mesg, int *t1) {
char dbgbuf[512];
sprintf(dbgbuf,mesg,t1);
marktime(dbgbuf);
}
/* C printf short routine */
void cprintfs(char *mesg, short *t1) {
char dbgbuf[512];
sprintf(dbgbuf,mesg,t1);
marktime(dbgbuf);
}
/* C printf long routine */
void cprintfl(char *mesg, long *t1) {
char dbgbuf[512];
sprintf(dbgbuf,mesg,t1);
marktime(dbgbuf);
}
/* C printf string routine */
void cprintfstr(char *mesg) {
char dbgbuf[512];
memset(dbgbuf,0,sizeof(dbgbuf));
sprintf(dbgbuf,mesg);
marktime(dbgbuf);
}
/* C printf float routine */
void cprintff(char *mesg, float *t1) {
char dbgbuf[512];
printf("cprintff: t1=%f = %g\n",t1,t1);
sprintf(dbgbuf,mesg,t1);
marktime(dbgbuf);
}
/* C printf double float routine */
void cprintfdf(char *mesg, float *t1) {
char dbgbuf[512];
printf("cprintfdf: t1=%f = %g\n",t1,t1);
sprintf(dbgbuf,mesg,t1);
marktime(dbgbuf);
}
/* marktime.c */
-- hsdbg.ads --
with Interfaces.C;
with Interfaces.C.Strings;
package HSDBG is
-- C printf str routine
procedure cprintfstr(Variable : Interfaces.C.Strings.chars_ptr);
pragma Import(C, cprintfstr, "cprintfstr");
-- C printf int routine
procedure cprintfi(Variable : Interfaces.C.Strings.chars_ptr; val
: Integer );
pragma Import(C, cprintfi, "cprintfi");
-- C printf short routine
procedure cprintfs(Variable : Interfaces.C.Strings.chars_ptr; val
: Interfaces.C.short );
pragma Import(C, cprintfs, "cprintfs");
-- C printf long routine
procedure cprintfl(Variable : Interfaces.C.Strings.chars_ptr; val
: Interfaces.C.long );
pragma Import(C, cprintfl, "cprintfl");
-- C printf float routine (32 bits)
procedure cprintff(Variable : Interfaces.C.Strings.chars_ptr; val
: Interfaces.C.C_float );
pragma Import(C, cprintff, "cprintff");
-- C printf double float routine (64 bits)
procedure cprintfdf(Variable : Interfaces.C.Strings.chars_ptr;
val : Interfaces.C.C_float );
pragma Import(C, cprintfdf, "cprintfdf");
end HSDBG;
-- hsdbg.ads --
-- tst.adb --
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with hsdbg; use hsdbg;
procedure tst is
PI : constant := 3.14159_26535_8979;
type REAL is digits 6; -- Single Precision
Floating Point
type LONG_REAL is digits 9; -- Double Precision
Floating Point
type INTEGER_SHORT is range -32768..32767; -- 16 Bit Integer
type INTEGER_LONG is range -2**31.. 2**31-1; -- 32 Bit Integer
type RADIANS_LONG is new LONG_REAL range -3.0*PI..3.0*PI;
subtype LATITUDE_TYPE is RADIANS_LONG range -PI/2.0 .. PI/2.0;
subtype LONGITUDE_TYPE is RADIANS_LONG range -PI..PI;
C_Format1 : chars_ptr := New_String("ADA routine: value=%d");
C_Format2 : chars_ptr := New_String("Testing of C strings");
C_Format3 : chars_ptr := New_String("ADA calling cprintff:
value=%f");
C_Format4 : chars_ptr := New_String("ADA calling cprintfdf:
value=%f");
step : Integer;
package Int_IO is new Integer_IO (Integer);
use Int_IO;
package Flt_IO is new Float_IO (float);
use Flt_IO;
cnt : Integer;
s : short;
l : long;
lat : LATITUDE_TYPE;
lon : LONGITUDE_TYPE;
f : float;
d : double;
begin
put_line ("test started");
step := 1;
l :=1234567890;
s:=255;
-- lat := 1.5678;
-- lon := -1.5678;
f := 123.456789;
d := 123456789.9876543210;
-- cprintfi(C_Format1,step);
-- cnt:=22;
-- cprintfi(C_Format1,cnt);
-- cprintfstr(C_Format2);
-- cprintfs(C_Format1,s);
-- put_line("calling cprintfl(C_Format1,l)");
-- cprintfl(C_Format1,l);
put_line("floating value of f = ");
Ada.Float_Text_IO.put(f);new_line;
put_line("calling cprintff(C_Format3,f)");
cprintff(C_Format3,C_Float(f));
put_line("calling cprintfdf(C_Format4,d)");
cprintfdf(C_Format4,C_Float(d));
put_line("end of my test program");
end tst;
-- tst.adb --
go1.bat
gcc -c marktime.c
gnatmake -c tst.adb
gnatbind tst.ali
gnatlink tst.ali marktime.o
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Passing float values to C
2005-02-12 16:58 Passing float values to C Garry
@ 2005-02-12 18:05 ` tmoran
2005-02-12 18:08 ` Pascal Obry
2005-02-13 0:38 ` Jeffrey Carter
1 sibling, 1 reply; 6+ messages in thread
From: tmoran @ 2005-02-12 18:05 UTC (permalink / raw)
> void cprintfs(char *mesg, short *t1) {
That says you are not passing a short float - you are passing a pointer
(to a short float).
> procedure cprintfs(Variable : Interfaces.C.Strings.chars_ptr; val
> : Interfaces.C.short );
> pragma Import(C, cprintfs, "cprintfs");
That says you are passing a short float, val.
Which is it?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Passing float values to C
2005-02-12 18:05 ` tmoran
@ 2005-02-12 18:08 ` Pascal Obry
2005-02-12 19:59 ` Martin Krischik
0 siblings, 1 reply; 6+ messages in thread
From: Pascal Obry @ 2005-02-12 18:08 UTC (permalink / raw)
tmoran@acm.org writes:
> > procedure cprintfs(Variable : Interfaces.C.Strings.chars_ptr; val
> > : Interfaces.C.short );
> > pragma Import(C, cprintfs, "cprintfs");
> That says you are passing a short float, val.
No, Interfaces.C.short is a short *int*.
Pascal.
--
--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Passing float values to C
2005-02-12 18:08 ` Pascal Obry
@ 2005-02-12 19:59 ` Martin Krischik
2005-02-12 21:36 ` tmoran
0 siblings, 1 reply; 6+ messages in thread
From: Martin Krischik @ 2005-02-12 19:59 UTC (permalink / raw)
Pascal Obry wrote:
>
> tmoran@acm.org writes:
>
>> > procedure cprintfs(Variable : Interfaces.C.Strings.chars_ptr; val
>> > : Interfaces.C.short );
>> > pragma Import(C, cprintfs, "cprintfs");
>> That says you are passing a short float, val.
>
> No, Interfaces.C.short is a short *int*.
Which is OK - the C type was also "short int".
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Passing float values to C
2005-02-12 19:59 ` Martin Krischik
@ 2005-02-12 21:36 ` tmoran
0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2005-02-12 21:36 UTC (permalink / raw)
>> No, Interfaces.C.short is a short *int*.
>
>Which is OK - the C type was also "short int".
Yes, I was sloppy. All of his cprint* functions that take a "val"
parameter take it as a pointer, not a value, and all his Ada specs falsely
declare it to be a value, not a pointer. I copied the "short" one
as an example but talked about the "float" one.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Passing float values to C
2005-02-12 16:58 Passing float values to C Garry
2005-02-12 18:05 ` tmoran
@ 2005-02-13 0:38 ` Jeffrey Carter
1 sibling, 0 replies; 6+ messages in thread
From: Jeffrey Carter @ 2005-02-13 0:38 UTC (permalink / raw)
Garry wrote:
> ADA experts, what am I doing wrong?
> I want to be able to pass float values (single and double precision) to
> C. I have not been successful in getting it to work.
You have not told us what your program does that is not correct, so it's
hard to tell. I can guess, though.
None of your Ada procedures with a numeric argument should work. All of
your C functions have pointers to numeric variables, and all of your Ada
procedures are passing numeric values to them.
--
Jeff Carter
"So if I understand 'The Matrix Reloaded' correctly, the Matrix is
basically a Microsoft operating system--it runs for a while and
then crashes and reboots. By design, no less. Neo is just a
memory leak that's too hard to fix, so they left him in ... The
users don't complain because they're packed in slush and kept
sedated."
Marin D. Condic
65
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2005-02-13 0:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-12 16:58 Passing float values to C Garry
2005-02-12 18:05 ` tmoran
2005-02-12 18:08 ` Pascal Obry
2005-02-12 19:59 ` Martin Krischik
2005-02-12 21:36 ` tmoran
2005-02-13 0:38 ` Jeffrey Carter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox