comp.lang.ada
 help / color / mirror / Atom feed
From: njt@minster.york.ac.uk (Nigel J. Tracey)
Subject: Unchecked Conversion and 'Valid
Date: 1996/10/07
Date: 1996-10-07T00:00:00+00:00	[thread overview]
Message-ID: <53anmp$2q3@netty.york.ac.uk> (raw)


I am using unchecked conversion to generate random Floats
over large ranges (up to Float'First..Float'Last). The
problem is that it generates values which are out of
the specified range for the float subtype, but 'Valid
still says that the value is valid. Having tried the same
thing for integer types the 'Valid attribute returns
False if the number is out of range. My question is
that if an array of bits is converted to a floating point
sub-type (using unchecked conversion), should 'Valid
indicate that the value is invalid if it is out of the
sub-types range?

The code I have used is below. The Valid attribute always
appears to be true even when the value of Conv_Float is
outside the range of My_Float.

Thanks for any help, information or guidance,

Nigel

--------------------
Nigel J. Tracey MEng, Research Associate (High Integrity Systems Group).
Department of Computer Science,   Office: X/D016
University of York,               Tel   : [0|+44]1904 432769
York, England.                    E-Mail: njt@minster.york.ac.uk
YO1 5DD.                          URL   : http://sv1pc161.cs.york.ac.uk/~njt



with Unchecked_Conversion;
th Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
with Text_IO; use Text_IO;

procedure Unchecked is

	type My_Float is digits 6 range -3.0E+15..+3.0+E15;

	type Bit_Type is new integer range 0..1;
	for Bit_Type'Size use 1;

	type Bit_Array_Type is array(Integer range <>) of Bit_Type;
	for Bit_Array_Type'Component_Size use 1;
	pragma Pack(Bit_Array_Type);

	type Float_Bit_Array_Type is new Bit_Array_Type(1..My_Float'Size);

	function Bit_Array_To_Float is new
	   Unchecked_Conversion(Float_Bit_Array_Type, My_Float);

	package Bit_IO is new Integer_IO(Bit_Type); use Bit_IO;
	package Int_IO is new Float_IO(My_Float)  ; use Int_IO;

	Float_Bit_Array : Float_Bit_Array_Type;
	Gen : Generator;
	Conv_Float : My_Float;

begin
   Reset(Gen);

   for I in 1..My_Float'Size
   loop
      if Random(Gen) > (0.5-Float'Small) then
         Float_Bit_Array(I) := 1;
      else
         Float_Bit_Array(I) := 0;
      end if;
   end loop;

   Conv_Float := Bit_Array_To_Float(Float_Bit_Array);

   Put_Line("The Float is");
   Put(Conv_Float);
   New_Line;
   if Conv_Float'Valid then
      Put_Line("This value is Valid");
   else
      Put_Line("This value is not Valid");
   end if;

end Unchecked;




                 reply	other threads:[~1996-10-07  0:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

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