From: anon@anon.org (anon)
Subject: Re: Restarting Tread: Why isn't this program working with
Date: Fri, 16 Jan 2009 01:37:16 GMT
Date: 2009-01-16T01:37:16+00:00 [thread overview]
Message-ID: <gxRbl.290368$Mh5.138665@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com
--
-- Three different versions. All version use 8-bits instead of 7-bits
-- because integer version of 200.0 may not fit in a 7-bit code.
-- use declare blocks to separate versions.
--
with Ada.Integer_Text_IO ;
with Ada.Float_Text_IO ;
with Ada.Text_IO ;
use Ada.Text_IO ;
with Ada.Unchecked_Conversion;
with Interfaces ;
use Interfaces ;
procedure test2 is
subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1);
subtype A_Float is Float;
subtype Short_integer2 is Integer range 0.. ((2 ** 8) - 1);
subtype A_Natural is Natural range 0..((2 ** 8) - 1);
--FYI1
Size_Of_Float : integer := Float'Size; -- 32
Size_Of_short_int: integer := Short_integer1'Size; -- 10 !!!!!!! Size is 10
--FYI2
Size_Of_short_integer: integer := Short_integer2'Size; -- 8
Size_Of_Natural: integer := A_Natural'Size; -- 8
Arg : A_Float;
Result : Short_integer1;
Begin
-- will cause error due to integer version of 200 is
-- greater than 2**7
begin
Arg := 200.0;
Result := Short_integer1(Arg);
exception
when Constraint_Error =>
Put_Line ( "Constraint_Error using Short_integer1" ) ;
end ;
-- using files
declare
Temp_String : String ( 1..10 ) ;
Temp : Integer ;
Last : Positive ;
Result2 : Short_integer2;
begin
Arg := 200.0;
Ada.Float_Text_IO.Put ( Temp_String, Arg, Aft => 0, Exp => 0 ) ;
Ada.Integer_Text_IO.Get ( Temp_String, Temp, Last ) ;
Result2 := Short_integer2(Temp);
Put_Line ( "Temp :=> " & Temp_String ) ;
Put ( "Result from file :=> " ) ;
Ada.Integer_Text_IO.Put ( Result2 ) ;
New_Line ;
exception
when Constraint_Error =>
Put_Line ( "Constraint_Error using Files" ) ;
Put_Line ( "Temp :=> " & Temp_String ) ;
end ;
-- Works but the number may be greater than 7 bits code.
declare
function To_Integer is new Ada.Unchecked_Conversion
( Float, Integer ) ;
Temp : Integer ;
begin
Arg := 200.0;
Temp := To_Integer ( Arg ) ;
Put ( "Result :=> " ) ;
Ada.Integer_Text_IO.Put ( Temp ) ;
new_line ;
exception
when Constraint_Error =>
Put_Line ( "Constraint_Error using Unchecked_Conversion" ) ;
end ;
-- Works but the number may be greater than 7 bits code, so using 8
declare
type Irec is array ( 0..3 ) of Unsigned_8 ;
function To_Integer is new Ada.Unchecked_Conversion
( Float, Irec ) ;
Temp : Irec ;
begin
Arg := 200.0;
Temp := To_Integer ( Arg ) ;
Put ( "Result :=> " ) ;
for index in IRec'range loop
Ada.Integer_Text_IO.Put ( Integer ( Temp ( index ) ), 8, 16 );
end loop ;
new_line ;
exception
when Constraint_Error =>
Put_Line ( "Constraint_Error using Unchecked_Conversion" ) ;
end ;
End test2;
In <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com>, ChristopherL <clusardi2k@aol.com> writes:
>I'm restarting the tread [How to put 200 into an integer sub-type of
>16 bits (code included)] with a new subject because the last was too
>long!
>
>How can I be getting the below error message with
>"Unchecked_Conversion".
>
>Isn't Unchecked_conversion just suppose to copy bits?
>
>I want to copy bits from an Ada float to a user defined short integer
>maintaining the same bit representation.
>
>Arg:Float; -- From this with a value of 200
>subtype Short_integer is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
>Result: Short_integer; --to this, and I want to round Arg
>
>The definitions of Arg and Result2 can not chanage.
>
>Chris L.
>
>with Unchecked_Conversion;
>
>procedure test is
> subtype Short_integer1 is Natural range 0.. ((2 ** 10) - 1);
> subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1);
>
> subtype A_Float is Float;
>
> subtype A_Natural is Natural range 0..((2 ** 8) - 1);
> -- The next line of code (if used) gives compilation error message:
> -- A_NATURAL does not denote a first subtype
> -- for A_Natural'Size use Short_integer1'Size;
>
> Size_Of_Float : integer :=3D Float'Size; --32 bits long
>
> Size_Of_short_integer: integer :=3D Short_integer1'Size;--10 bits
>long
> Size_Of_short_int: integer :=3D Short_integer2'Size;--10 bits long
>
> Size_Of_Natural: integer :=3D A_Natural'Size; --8 bits long
>
> Arg : A_Float;
> Result2 : Short_integer2;
> Result1 : Short_integer1;
>
> function Get_Bits is new Unchecked_Conversion (Source =3D>
> Short_integer1, Target =3D> Short_integer2);
>
>Begin
>
>Arg :=3D 200.0;
>
>Result1 :=3D Short_integer1(Arg + 0.5); -- Result1 becomes 201
>Result2 :=3D Get_Bits (Result1);
>
>End test;
>
>-- Error Message
>+++ Program started at 01/15/09 12:14:02
>
>Unhandled exception:
>
>CONSTRAINT_ERROR raised in MAIN
>
>Range_Check_Failed
>
>Exception raised at code address: 0x413516
>+++ Program completed with exit code 1 at 01/15/09 12:14:02
>+++
>---------------------------------------------------------------------------=
>=AD-----------------------------------
next prev parent reply other threads:[~2009-01-16 1:37 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com>
2009-01-15 21:22 ` Restarting Tread: Why isn't this program working with Unchecked_Converstion Jeffrey R. Carter
[not found] ` <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com>
2009-01-16 0:22 ` Jeffrey R. Carter
2009-01-16 1:37 ` anon [this message]
[not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com>
2009-01-16 2:27 ` Adam Beneschan
[not found] ` <9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com>
2009-01-16 15:11 ` Ludovic Brenta
2009-01-16 16:23 ` Martin
[not found] ` <70172b19-360c-4eba-815c-ede747c3bcdf@w39g2000prb.googlegroups.com>
2009-01-16 17:24 ` Ludovic Brenta
2009-01-16 17:26 ` Martin
2009-01-16 17:34 ` Georg Bauhaus
2009-01-16 12:18 ` Stuart
2009-01-16 23:14 ` sjw
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox