From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ef2e6181b4b6365e,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!z14g2000cwz.googlegroups.com!not-for-mail From: "Garry" Newsgroups: comp.lang.ada Subject: Passing float values to C Date: 12 Feb 2005 08:58:43 -0800 Organization: http://groups.google.com Message-ID: <1108227523.424858.160530@z14g2000cwz.googlegroups.com> NNTP-Posting-Host: 68.230.2.181 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1108227527 15631 127.0.0.1 (12 Feb 2005 16:58:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 12 Feb 2005 16:58:47 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: z14g2000cwz.googlegroups.com; posting-host=68.230.2.181; posting-account=EMtA8g0AAAA-UNlGV18CQWXjhlUjxiBB Xref: g2news1.google.com comp.lang.ada:8286 Date: 2005-02-12T08:58:43-08:00 List-Id: 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 #include /* 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