comp.lang.ada
 help / color / mirror / Atom feed
* Floating point representation-help
@ 2002-08-12 12:29 Ashwath
  2002-08-12 13:08 ` David C. Hoos
  0 siblings, 1 reply; 3+ messages in thread
From: Ashwath @ 2002-08-12 12:29 UTC (permalink / raw)


Have look at the following program 
with TEXT_IO; 
with UNCHECKED_CONVERSION; 
procedure UNCHECK_DEMO is 
package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER); 
function INT_TO_FLT is 
new UNCHECKED_CONVERSION( 
SOURCE => LONG_LONG_INTEGER, 
TARGET => FLOAT); 
INT : LONG_LONG_INTEGER; 
FLT : FLOAT; 
begin 
INT:=2#11111111100000000000000000000000#; 
FLT:=INT_TO_FLT(INT); 
INT_IO.put(INT,0,2); 
end; 

Now look at the following sentences which explains how to find the
exact value of a 32 bit word (This 32 bit word is IEEE single
precision floating point standard representation).

The value V represented by the word may be determined as
follows:(Concentrate on condition 2).

1)If E=255 and F is nonzero, then V=NaN ("Not a number") 
2)If E=255 and F is zero and S is 1, then V=-Infinity 
3)If E=255 and F is zero and S is 0, then V=Infinity 
4)If 0<E<255 then V=(-1)**S * 2 ** (E-127) * (1.F) where "1.F" is
intended to represent the binary number created by prefixing F with an
implicit leading 1 and a binary point.
5)If E=0 and F is nonzero, then V=(-1)**S * 2 ** (-126) * (0.F) These
are "unnormalized" values.
6)If E=0 and F is zero and S is 1, then V=-0 
7)If E=0 and F is zero and S is 0, then V=0 
Now in the above program I am making the float variable FLT to take
the value so as satisfy the condition 2. I was expecting exception to
be raised when i run this program(because the value should be infinity
according to condition 2), but it did'nt.
Any Idea why?Pls let me know if I am doing any stupid thing in the
program.



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

* Re: Floating point representation-help
  2002-08-12 12:29 Floating point representation-help Ashwath
@ 2002-08-12 13:08 ` David C. Hoos
  2002-08-12 23:43   ` Robert Dewar
  0 siblings, 1 reply; 3+ messages in thread
From: David C. Hoos @ 2002-08-12 13:08 UTC (permalink / raw)



----- Original Message ----- 
From: "Ashwath" <vashwathus@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Monday, August 12, 2002 7:29 AM
Subject: Floating point representation-help


> Have look at the following program 
> with TEXT_IO; 
> with UNCHECKED_CONVERSION; 
> procedure UNCHECK_DEMO is 
> package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER); 
> function INT_TO_FLT is 
> new UNCHECKED_CONVERSION( 
> SOURCE => LONG_LONG_INTEGER, 
> TARGET => FLOAT); 
> INT : LONG_LONG_INTEGER; 
> FLT : FLOAT; 
> begin 
> INT:=2#11111111100000000000000000000000#; 
> FLT:=INT_TO_FLT(INT); 
> INT_IO.put(INT,0,2); 
> end; 
First, You have not specified which compiler and platform
you're using, and things of this nature may be both platform-
_and_ compoler-dependent.

Second, the above program does not compile with GNAT on
Linux, for several reasons, one of which is that the size of
Long_Long_Integer on that compiler and platform is not the
same as the size of Float on that compiler and platform.

The following program does compile, and shows that the value
of Flt is indeed infinity.

There is nothing that says that computing a value of inifinity with
IEEE Floating point  representation should raise an exception,
since +inf, -Inf, and NaN are all legal IEEE Floating values.

However, performing a computation with such a value as an
operand should raise an exception.

with Interfaces;
with Text_IO;
with Unchecked_Conversion;
procedure Uncheck_Demo is
   function Int_To_Flt is new Unchecked_Conversion
     (Source => Interfaces.Unsigned_32,
      Target => Float);
   Int : Interfaces.Unsigned_32;
   Flt : Float;
begin
   Int:=2#11111111100000000000000000000000#;
   Flt:=Int_To_Flt(Int);
   Text_IO.Put_Line (Float'Image (Flt));
end;

Furthermore, in my modified version of your program, there is still
something that may be compiler-, and/or platform-dependent --
i.e., the declaration of Flt as type Float.  There is no guarantee
that every compiler on every platform defines the type Float as
32-bit IEEE floating point.

The GNAT compiler does make types available that re 32- and 64-
bit IEEE floating point on platforms where the underlying hardware
supports it.





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

* Re: Floating point representation-help
  2002-08-12 13:08 ` David C. Hoos
@ 2002-08-12 23:43   ` Robert Dewar
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Dewar @ 2002-08-12 23:43 UTC (permalink / raw)


"David C. Hoos" <david.c.hoos.sr@ada95.com> wrote in message news:<mailman.1029157742.20629.comp.lang.ada@ada.eu.org>...

> However, performing a computation with such a value as an
> operand should raise an exception.

No, that's wrong you are making up rules that do not exist.
Remember that Float is an unconstrained type!



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

end of thread, other threads:[~2002-08-12 23:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-12 12:29 Floating point representation-help Ashwath
2002-08-12 13:08 ` David C. Hoos
2002-08-12 23:43   ` Robert Dewar

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