comp.lang.ada
 help / color / mirror / Atom feed
* Problem with pragmas
@ 1999-06-30  0:00 Robert Schien
  1999-06-30  0:00 ` Florian Weimer
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robert Schien @ 1999-06-30  0:00 UTC (permalink / raw)


I need to access the erf (Gaussian error function) from the C-Math-Library.
But the following program does not work correctly:


 with Ada.Float_Text_IO;
 use  Ada.Float_Text_IO;
 with Ada.Text_IO;
 use  Ada.Text_IO;
 with Interfaces.C;
 use  Interfaces.C;

 procedure pragtest is      

 y: float;
 c_y: C_float;

-- Accessing Gaussian error function
function erf (x: in C_float) return C_float;
   pragma Import(C,erf);

begin
y:=0.3;
c_y:=c_float(y);
c_y:=erf(c_y);
y:=float(c_y);
put(y);
 end pragtest;

This program is compiled and linked with the command
gnatmake pragtest.adb -largs -lm

But when I execute pragtest, the printed result is always 0.0.
(The correct result is something between 0 and 1, in this case
about 0.32)

What did I miss?

The platform is FreeBSD-4.0-current (x86) with GNAT 3.11p.

TIA
Robert




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with pragmas
  1999-06-30  0:00 Problem with pragmas Robert Schien
  1999-06-30  0:00 ` Florian Weimer
  1999-06-30  0:00 ` Robert I. Eachus
@ 1999-06-30  0:00 ` David
  2 siblings, 0 replies; 5+ messages in thread
From: David @ 1999-06-30  0:00 UTC (permalink / raw)


In article <FE4vxK.20D@robkaos.ruhr.de>,
  robsch@robkaos.ruhr.de (Robert Schien) wrote:
> I need to access the erf (Gaussian error function) from the C-Math-
Library.
> But the following program does not work correctly:
>

What you missed was that the type of the parameters is not C_Float,
but Double.

The following version of your program works correctly, and eliminates
the need for the -largs on the command line, as well.

with Ada.Float_Text_IO;
use  Ada.Float_Text_IO;
with Ada.Text_IO;
use  Ada.Text_IO;
with Interfaces.C;
use  Interfaces.C;

procedure Pragtest is
   pragma Linker_Options ("-lm");
   Y: Float;
   C_Y: Double;

   -- Accessing Gaussian error function
   function Erf (X: in Double) return Double;
   pragma Import(C,Erf);

begin
   Y:=0.3;
   C_Y:=Double(Y);
   C_Y:=Erf(C_Y);
   Y:=Float(C_Y);
   Put(Y);
end Pragtest;


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with pragmas
  1999-06-30  0:00 Problem with pragmas Robert Schien
  1999-06-30  0:00 ` Florian Weimer
@ 1999-06-30  0:00 ` Robert I. Eachus
  1999-06-30  0:00 ` David
  2 siblings, 0 replies; 5+ messages in thread
From: Robert I. Eachus @ 1999-06-30  0:00 UTC (permalink / raw)


Robert Schien wrote:

> I need to access the erf (Gaussian error function) from the C-Math-Library.
> But the following program does not work correctly:

> But when I execute pragtest, the printed result is always 0.0.
> (The correct result is something between 0 and 1, in this case
> about 0.32)
> 
> What did I miss?
 
    It could be a bug, or a library function that just hasn't been
implemented in that version of Unix.  But there is another possibility,
one of those fun C rules.  The C math libraries actually take double
parameters, and the C compiler converts from float if necessary.   In
fact all scalar floating point parameters are passed as double or in
floating point registers.  On arcitectures where floating point
parameters are passed in registers, you won't notice a problem.  (Then
why have C_float in Interfaces.C?  Because you will often have floats in
structs that you need to interface to.)  I'm not sure about FreeBSD-4.0,
but if the call sequence was defined so it would work on 386 chips or
earlier, the parameters may not be passed in registers.

    (It also might be a good idea for Ada compilers to warn when C
interfaces are declared with C_float parameters.  Sometimes it is right
for example for 
OS calls.  Also on most hardware the parameters will be passed in
floating point registers, so the compiler shouldn't force the change.)

-- 

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with pragmas
  1999-06-30  0:00 Problem with pragmas Robert Schien
@ 1999-06-30  0:00 ` Florian Weimer
  1999-07-01  0:00   ` Robert Schien
  1999-06-30  0:00 ` Robert I. Eachus
  1999-06-30  0:00 ` David
  2 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 1999-06-30  0:00 UTC (permalink / raw)


robsch@robkaos.ruhr.de (Robert Schien) writes:

> -- Accessing Gaussian error function
> function erf (x: in C_float) return C_float;
>    pragma Import(C,erf);

My manpage for erf(3) says that this function uses the `double' C type
(instead of `float').  I guess your example woll work if you replace
`C_Float' by `double'.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with pragmas
  1999-06-30  0:00 ` Florian Weimer
@ 1999-07-01  0:00   ` Robert Schien
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Schien @ 1999-07-01  0:00 UTC (permalink / raw)


Florian Weimer <fw@s.netic.de> writes:

>robsch@robkaos.ruhr.de (Robert Schien) writes:

>> -- Accessing Gaussian error function
>> function erf (x: in C_float) return C_float;
>>    pragma Import(C,erf);

>My manpage for erf(3) says that this function uses the `double' C type
>(instead of `float').  I guess your example woll work if you replace
>`C_Float' by `double'.

Yes, that's right! :-) Sometimes one overlooks the simplest things. :)

Robert




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1999-07-01  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-30  0:00 Problem with pragmas Robert Schien
1999-06-30  0:00 ` Florian Weimer
1999-07-01  0:00   ` Robert Schien
1999-06-30  0:00 ` Robert I. Eachus
1999-06-30  0:00 ` David

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox