comp.lang.ada
 help / color / mirror / Atom feed
* Float precision - gnat vs objectada
@ 2004-12-01 15:46 P Torle
  2004-12-01 18:50 ` David C. Hoos
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: P Torle @ 2004-12-01 15:46 UTC (permalink / raw)


I'm working on a Ada95/C project that shall run on both Linux and
Windows.
I have some problems getting the same float precision on both
platforms.
Example:
-----------------------------
subtype Real is Long_Float;
pi : Real;

pi := 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
Real_Io.Put(pi, Aft => 25);
-----------------------------

Results:
Linux: 3.1415926535897931200000000E+00
Win32: 3.1415926535897931159979634E+00

The thing is, the following float-precision definitions are equal on
both Linux and Windows:

Real'Machine_Mantissa  : 53
Real'Machine_Emin      : -1021
Real'Machine_Emax      : 1024
Real'Digits            : 15

So why the difference? 

The Windows code is built with ObjectAda/adacomp and Microsoft tools
cl.exe/link.exe.
The Linux code is built with gnat/gcc 3.4.2



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

* Re: Float precision - gnat vs objectada
  2004-12-01 15:46 Float precision - gnat vs objectada P Torle
@ 2004-12-01 18:50 ` David C. Hoos
  2004-12-01 19:03 ` Martin Krischik
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: David C. Hoos @ 2004-12-01 18:50 UTC (permalink / raw)
  To: P Torle; +Cc: comp.lang.ada@ada.eu.org

Anything beyond 3.14159_26535_89793 is lost when storing
the value in the 64-bit floating point variable, because there is
room for only 53 bits of mantissa.

To generate more than 15 significant digits requires extending
the mantissa with binary zeroes, which of course are not
accurate.

Both compilers appear to have done a little of that -- gnat to
a reasonable extent, and ObjectAda to an unreasonable extent.

If your program had called for Aft = 14, or even 15, the
results would have been identical.  Note that the 16th digit
after the decimal point is wrong from _both_ compilers, compared
to the true value.  However to expect the 16th and subsequent
digits after the decimal point from _any_ compiler, given a 53-bit
mantissa is unreasonable.



----- Original Message ----- 
From: "P Torle" <knutby@torle.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada-france.org>
Sent: Wednesday, December 01, 2004 9:46 AM
Subject: Float precision - gnat vs objectada


> I'm working on a Ada95/C project that shall run on both Linux and
> Windows.
> I have some problems getting the same float precision on both
> platforms.
> Example:
> -----------------------------
> subtype Real is Long_Float;
> pi : Real;
> 
> pi := 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
> Real_Io.Put(pi, Aft => 25);
> -----------------------------
> 
> Results:
> Linux: 3.1415926535897931200000000E+00
> Win32: 3.1415926535897931159979634E+00
> 
> The thing is, the following float-precision definitions are equal on
> both Linux and Windows:
> 
> Real'Machine_Mantissa  : 53
> Real'Machine_Emin      : -1021
> Real'Machine_Emax      : 1024
> Real'Digits            : 15
> 
> So why the difference? 
> 
> The Windows code is built with ObjectAda/adacomp and Microsoft tools
> cl.exe/link.exe.
> The Linux code is built with gnat/gcc 3.4.2
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
> 



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

* Re: Float precision - gnat vs objectada
  2004-12-01 15:46 Float precision - gnat vs objectada P Torle
  2004-12-01 18:50 ` David C. Hoos
@ 2004-12-01 19:03 ` Martin Krischik
  2004-12-02  7:51   ` Jeffrey Carter
  2004-12-01 23:32 ` Mark Lorenzen
  2004-12-02  8:32 ` Dmitry A. Kazakov
  3 siblings, 1 reply; 7+ messages in thread
From: Martin Krischik @ 2004-12-01 19:03 UTC (permalink / raw)


P Torle wrote:

> I'm working on a Ada95/C project that shall run on both Linux and
> Windows.
> I have some problems getting the same float precision on both
> platforms.
> Example:
> -----------------------------
> subtype Real is Long_Float;

Maybe you want to use Long_Long_Float for maximum precision.

See http://en.wikibooks.org/wiki/Programming:Ada:Packages:Standard:GNAT for
predefined float types in GNAT

And, of course, when you need compatiblty betreen  GNAT and ObjectAda you
will need to define your own float type.

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

* Re: Float precision - gnat vs objectada
  2004-12-01 15:46 Float precision - gnat vs objectada P Torle
  2004-12-01 18:50 ` David C. Hoos
  2004-12-01 19:03 ` Martin Krischik
@ 2004-12-01 23:32 ` Mark Lorenzen
  2004-12-02  8:32 ` Dmitry A. Kazakov
  3 siblings, 0 replies; 7+ messages in thread
From: Mark Lorenzen @ 2004-12-01 23:32 UTC (permalink / raw)


knutby@torle.com (P Torle) writes:

> I'm working on a Ada95/C project that shall run on both Linux and
> Windows.
> I have some problems getting the same float precision on both
> platforms.
> Example:
> -----------------------------
> subtype Real is Long_Float;
> pi : Real;
> 
> pi := 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;

Just a little comment: The values of Pi and e are defined in the
package Ada.Numerics, so you do not need to define them yourself.

Regards,
- Mark Lorenzen



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

* Re: Float precision - gnat vs objectada
  2004-12-01 19:03 ` Martin Krischik
@ 2004-12-02  7:51   ` Jeffrey Carter
  2004-12-02 10:10     ` Martin Krischik
  0 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2004-12-02  7:51 UTC (permalink / raw)


Martin Krischik wrote:
> P Torle wrote:
> 
>>subtype Real is Long_Float;
> 
> Maybe you want to use Long_Long_Float for maximum precision.

The only predefined floating-point type required by the ARM is Float. 
Maybe he wants to use an application-specific floating-point type, and 
should realize that if he asks for more digits than the type supports, 
he will get garbage.

-- 
Jeff Carter
"If a sperm is wasted, God gets quite irate."
Monty Python's the Meaning of Life
56




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

* Re: Float precision - gnat vs objectada
  2004-12-01 15:46 Float precision - gnat vs objectada P Torle
                   ` (2 preceding siblings ...)
  2004-12-01 23:32 ` Mark Lorenzen
@ 2004-12-02  8:32 ` Dmitry A. Kazakov
  3 siblings, 0 replies; 7+ messages in thread
From: Dmitry A. Kazakov @ 2004-12-02  8:32 UTC (permalink / raw)


On 1 Dec 2004 07:46:38 -0800, P Torle wrote:

> I'm working on a Ada95/C project that shall run on both Linux and
> Windows.
> I have some problems getting the same float precision on both
> platforms.

To get the same precision tell the compiler which one you need.

type My_Float is digits N;

> Example:
> -----------------------------
> subtype Real is Long_Float;
> pi : Real;
> 
> pi := 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
> Real_Io.Put(pi, Aft => 25);
> -----------------------------
> 
> Results:
> Linux: 3.1415926535897931200000000E+00
> Win32: 3.1415926535897931159979634E+00
> 
> The thing is, the following float-precision definitions are equal on
> both Linux and Windows:
> 
> Real'Machine_Mantissa  : 53
> Real'Machine_Emin      : -1021
> Real'Machine_Emax      : 1024
> Real'Digits            : 15
> 
> So why the difference? 

Probably because the implementations of Real_IO.Put are slightly different.
But anything after 3.14159_26535_8979 is just a noise. The problem is not
whether Long_Floats are different or not. The problem is that Aft=>25 >
T'Digits.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Float precision - gnat vs objectada
  2004-12-02  7:51   ` Jeffrey Carter
@ 2004-12-02 10:10     ` Martin Krischik
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Krischik @ 2004-12-02 10:10 UTC (permalink / raw)


Jeffrey Carter wrote:

> Martin Krischik wrote:
>> P Torle wrote:
>> 
>>>subtype Real is Long_Float;
>> 
>> Maybe you want to use Long_Long_Float for maximum precision.
> 
> The only predefined floating-point type required by the ARM is Float.
> Maybe he wants to use an application-specific floating-point type, and
> should realize that if he asks for more digits than the type supports,
> he will get garbage.

My silent hope was that ObjectAda does not have a Long_Long_Float and then
understanding would have come to him all by its own ;-).

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

end of thread, other threads:[~2004-12-02 10:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-01 15:46 Float precision - gnat vs objectada P Torle
2004-12-01 18:50 ` David C. Hoos
2004-12-01 19:03 ` Martin Krischik
2004-12-02  7:51   ` Jeffrey Carter
2004-12-02 10:10     ` Martin Krischik
2004-12-01 23:32 ` Mark Lorenzen
2004-12-02  8:32 ` Dmitry A. Kazakov

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