From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8af7fe40dc0a55ae X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!wn11feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Restarting Tread: Why isn't this program working with Reply-To: no to spamers (No@email.given.org) References: <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Fri, 16 Jan 2009 01:37:16 GMT NNTP-Posting-Host: 12.65.120.156 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1232069836 12.65.120.156 (Fri, 16 Jan 2009 01:37:16 GMT) NNTP-Posting-Date: Fri, 16 Jan 2009 01:37:16 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:4339 Date: 2009-01-16T01:37:16+00:00 List-Id: -- -- 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 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-----------------------------------