* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [not found] <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com> @ 2009-01-15 21:22 ` Jeffrey R. Carter [not found] ` <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com> 2009-01-16 1:37 ` Restarting Tread: Why isn't this program working with anon ` (2 subsequent siblings) 3 siblings, 1 reply; 14+ messages in thread From: Jeffrey R. Carter @ 2009-01-15 21:22 UTC (permalink / raw) ChristopherL wrote: > > procedure test is > CONSTRAINT_ERROR raised in MAIN Obviously this error is coming from Main, not from the posted code for Test. We are unable to assist you with the error in Main since you have not posted its code for us. -- Jeff Carter "Your mother was a hamster and your father smelt of elderberries." Monty Python & the Holy Grail 06 ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com>]
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [not found] ` <37daf0e6-39b8-4820-a7fc-b6c5decf1ed8@q19g2000yqi.googlegroups.com> @ 2009-01-16 0:22 ` Jeffrey R. Carter 0 siblings, 0 replies; 14+ messages in thread From: Jeffrey R. Carter @ 2009-01-16 0:22 UTC (permalink / raw) ChristopherL wrote: > > I do not know what MAIN is. The only program that I compile, link, and > run is this test routine! Curious. What compiler are you using? -- Jeff Carter "Your mother was a hamster and your father smelt of elderberries." Monty Python & the Holy Grail 06 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with [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 @ 2009-01-16 1:37 ` anon [not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com> 2009-01-16 12:18 ` Stuart 3 siblings, 0 replies; 14+ messages in thread From: anon @ 2009-01-16 1:37 UTC (permalink / raw) -- -- 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----------------------------------- ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com>]
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [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> 0 siblings, 1 reply; 14+ messages in thread From: Adam Beneschan @ 2009-01-16 2:27 UTC (permalink / raw) On Jan 15, 12:53 pm, ChristopherL <clusard...@aol.com> wrote: > 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 ); > Result2: Short_integer; --to this, and I want to round Arg > > These 3 definitions of Arg and Result2 can not chanage. > > Arg will never be negative or greater than 200/201. > > 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 := Float'Size; --32 bits long > > Size_Of_short_integer: integer := Short_integer1'Size;--10 bits > long > Size_Of_short_int: integer := Short_integer2'Size;--10 bits long > > Size_Of_Natural: integer := A_Natural'Size; --8 bits long > > Arg : A_Float; > Result2 : Short_integer2; > Result1 : Short_integer1; > > function Get_Bits is new Unchecked_Conversion (Source => > Short_integer1, Target => Short_integer2); > > Begin > > Arg := 200.0; > > Result1 := Short_integer1(Arg + 0.5); -- Result1 becomes 201 > Result2 := Get_Bits (Result1); OK, I think I can help, but before that I want to be clear on something. Result2's subtype is defined with the range -128 .. 127, so Result2 will never have the value 200. It might have the same bit pattern as the number 200, but the actual value will be -56. If you used an instance of Text_IO.Integer_IO to print out Result2, the output would have "-56" in it. If that's not the result you want, then there's still a lot of confusion going on that we need to clear up first. But if you want help in getting things so that Result2 will be -56, then we can work on that. The problem here is that in the Ada definition of assignment, the value on the right side is converted to the subtype of the result, and that conversion could result in CONSTRAINT_ERROR. For some reason, your compiler has decided that Short_Integer2 is 10 bits. The Unchecked_Conversion is converting a 10-bit integer to a 10-bit integer, which basically means it doesn't do anything (just copying the bits). Then, the code has a 10-bit integer whose value is 200; then it checks the value against the bounds of the range, -128..127, and decides that the value is out of range. Not all compilers would behave this way. Many compilers would notice that, since the result of Get_Bits has subtype Short_Integer2 and the variable Result2 has the same subtype Short_Integer2, no actual checking is necessary (assuming the values are valid). Your compiler appears to do the check anyway. I think you'll have to force the compiler to work with 8-bit types instead of 10-bit types. Since you can't put a 'Size clause on a subtype of Integer, you'll need to declare new types: type Short_Integer1 is range 0 .. (2**8)-1; --not (2**10)-1 for Short_Integer1'Size use 8; type Short_Integer2 is range -(2**7) .. (2**7)-1; for Short_Integer2'Size use 8; Or, if Short_Integer2 *has* to be a subtype, then declare a new type with a different name, force its size to be 8, and do a type conversion from that new type to Short_Integer2 afterwards. If that still doesn't work, perhaps because your compiler doesn't know how to avoid looking at the leftover 2 bits, you may have to do something inelegant: if Result1 >= 128 then Result2 := Result1 - 256 else Result2 := Result1; end if; By the way, you probably don't want to add 0.5 in this code: Result1 := Short_integer1(Arg + 0.5); -- Result1 becomes 201 In some languages, when you convert a real to an integer, the language truncates to the next lower integer, so the way you do rounding is to add 0.5 and then do the conversion. In Ada, though, converting a real to an integer does the rounding. So adding your own 0.5 is wrong and will give the wrong results. Result1 := Short_Integer1 (Arg); is I think what you want. I hope this helps. -- Adam ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <9069fcf7-4257-4439-ad4a-8d7c8c17f5cf@v5g2000pre.googlegroups.com>]
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [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> 0 siblings, 2 replies; 14+ messages in thread From: Ludovic Brenta @ 2009-01-16 15:11 UTC (permalink / raw) On Jan 16, 3:11 pm, ChristopherL <clusard...@aol.com> wrote: > Thanks Adam, yours is a working solution, see below. > > Thanks everyone, > Chris L. > > On Jan 15, 6:27 pm, Adam Beneschan <a...@irvine.com> wrote: > > > > > I think you'll have to force the compiler to work with 8-bit types > > instead of 10-bit types. Since you can't put a 'Size clause on a > > subtype of Integer, you'll need to declare new types: > > > type Short_Integer1 is range 0 .. (2**8)-1; --not (2**10)-1 > > for Short_Integer1'Size use 8; > > > type Short_Integer2 is range -(2**7) .. (2**7)-1; > > for Short_Integer2'Size use 8; > > > Or, if Short_Integer2 *has* to be a subtype, then declare a new type > > with a different name, force its size to be 8, and do a type > > conversion from that new type to Short_Integer2 afterwards. > > with Unchecked_Conversion; > > with Integer_Text_IO ; > > procedure test is > type Short_integer1 is range 0.. ((2 ** 8) - 1); > for Short_Integer1'Size use 8; > > subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1); > > type Short_Integer3 is range -(2**7) .. (2**7)-1; > for Short_Integer3'Size use 8; > > 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 := Float'Size; --32 bits long > > Size_Of_short_integer: integer := Short_integer1'Size;--10 bits > long > Size_Of_short_int: integer := Short_integer2'Size;--10 bits long > > Size_Of_Natural: integer := A_Natural'Size; --8 bits long > > Arg : A_Float; > Result3 : Short_integer2; > Result2 : Short_integer3; > Result1 : Short_integer1; > > function Get_Bits2 is new Unchecked_Conversion (Source => > Short_integer1, Target => Short_integer3); > > Begin > > Arg := 200.0; > > Result1 := Short_integer1(Arg); -- Result1 becomes 200 > Result2 := Get_Bits2 (Result1); > Result3 := Short_integer2(Result2); > Integer_Text_IO.Put ( Result3 ) ; > End test; OK, you've managed to change the floating-point value 200.0 into an integer value of -56. The bit representations of these two values are different. (That's because the explicit type conversion of Arg from type Float to type Short_integer1 changes the representation to preserve the value). I'm curious to understand: why did you do that? PS. I think you can simplify your code into: declare type Unsigned_8 is mod 2 ** 8; for Unsigned_8'Size use 8; type Signed_8 is range - 2**7 .. 2**7 - 1; for Signed_8'Size use 8; function To_Signed_8 is new Ada.Unchecked_Conversion (Unsigned_8, Signed_8); Arg : Float := 200.0; Result : Signed_8 := To_Signed_8 (Unsigned_8 (Arg)); begin Ada.Text_IO.Put (Signed_8'Image (Result)); end; -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion 2009-01-16 15:11 ` Ludovic Brenta @ 2009-01-16 16:23 ` Martin [not found] ` <70172b19-360c-4eba-815c-ede747c3bcdf@w39g2000prb.googlegroups.com> 1 sibling, 0 replies; 14+ messages in thread From: Martin @ 2009-01-16 16:23 UTC (permalink / raw) On Jan 16, 3:11 pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote: > On Jan 16, 3:11 pm, ChristopherL <clusard...@aol.com> wrote: > > > > > Thanks Adam, yours is a working solution, see below. > > > Thanks everyone, > > Chris L. > > > On Jan 15, 6:27 pm, Adam Beneschan <a...@irvine.com> wrote: > > > > I think you'll have to force the compiler to work with 8-bit types > > > instead of 10-bit types. Since you can't put a 'Size clause on a > > > subtype of Integer, you'll need to declare new types: > > > > type Short_Integer1 is range 0 .. (2**8)-1; --not (2**10)-1 > > > for Short_Integer1'Size use 8; > > > > type Short_Integer2 is range -(2**7) .. (2**7)-1; > > > for Short_Integer2'Size use 8; > > > > Or, if Short_Integer2 *has* to be a subtype, then declare a new type > > > with a different name, force its size to be 8, and do a type > > > conversion from that new type to Short_Integer2 afterwards. > > > with Unchecked_Conversion; > > > with Integer_Text_IO ; > > > procedure test is > > type Short_integer1 is range 0.. ((2 ** 8) - 1); > > for Short_Integer1'Size use 8; > > > subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1); > > > type Short_Integer3 is range -(2**7) .. (2**7)-1; > > for Short_Integer3'Size use 8; > > > 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 := Float'Size; --32 bits long > > > Size_Of_short_integer: integer := Short_integer1'Size;--10 bits > > long > > Size_Of_short_int: integer := Short_integer2'Size;--10 bits long > > > Size_Of_Natural: integer := A_Natural'Size; --8 bits long > > > Arg : A_Float; > > Result3 : Short_integer2; > > Result2 : Short_integer3; > > Result1 : Short_integer1; > > > function Get_Bits2 is new Unchecked_Conversion (Source => > > Short_integer1, Target => Short_integer3); > > > Begin > > > Arg := 200.0; > > > Result1 := Short_integer1(Arg); -- Result1 becomes 200 > > Result2 := Get_Bits2 (Result1); > > Result3 := Short_integer2(Result2); > > Integer_Text_IO.Put ( Result3 ) ; > > End test; > > OK, you've managed to change the floating-point value 200.0 into an > integer value of -56. The bit representations of these two values are > different. (That's because the explicit type conversion of Arg from > type Float to type Short_integer1 changes the representation to > preserve the value). > > I'm curious to understand: why did you do that? > > PS. I think you can simplify your code into: > > declare > type Unsigned_8 is mod 2 ** 8; > for Unsigned_8'Size use 8; > type Signed_8 is range - 2**7 .. 2**7 - 1; > for Signed_8'Size use 8; > function To_Signed_8 is new Ada.Unchecked_Conversion (Unsigned_8, > Signed_8); > > Arg : Float := 200.0; > Result : Signed_8 := To_Signed_8 (Unsigned_8 (Arg)); > begin > Ada.Text_IO.Put (Signed_8'Image (Result)); > end; > > -- > Ludovic Brenta. Or using nothing but pre-defined standard types: with Ada.Text_IO; with Ada.Unchecked_Conversion; with Interfaces; procedure Test is function To_Integer_8 is new Ada.Unchecked_Conversion (Source => Interfaces.Unsigned_8, Target => Interfaces.Integer_8); Arg : Float := 200.0; Result : Interfaces.Integer_8 := To_Integer_8 (Interfaces.Unsigned_8 (Arg)); begin Ada.Text_IO.Put (Interfaces.Integer_8'Image (Result)); end Test; ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <70172b19-360c-4eba-815c-ede747c3bcdf@w39g2000prb.googlegroups.com>]
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [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 2 siblings, 0 replies; 14+ messages in thread From: Ludovic Brenta @ 2009-01-16 17:24 UTC (permalink / raw) ChristopherL wrote on comp.lang.ada: > On Jan 16, 7:11 am, Ludovic Brenta wrote: > > type Signed_8 is range - 2**7 .. 2**7 - 1; > > for Signed_8'Size use 8; > > I didn't use the second line in my program! Without it, my system > uses 10 bits for this. I'm going to use my solution for now. > > Also experimenting on my own, if I only make the below change the > variable Result has a value of -62 and not as above (-56). > > -- type Short_integer1 is range 0.. ((2 ** 8) - 1); > -- for Short_Integer1'Size use 8; > > type Short_integer1 is mod 2 ** 8; > for Short_Integer1'Size use 8; > > What does the following line of code do? It's new to me and have to > know! > > type Short_integer1 is mod 2 ** 8; This declares a modular type as defined in ARM 3.5.4: http://www.adaic.com/standards/05rm/html/RM-3-5-4.html I'm still curious to know what you are trying to achieve with all these conversions. As I pointed out, the result has neither the value nor the bit representation of the floating-point argument. -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [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 2 siblings, 0 replies; 14+ messages in thread From: Martin @ 2009-01-16 17:26 UTC (permalink / raw) On Jan 16, 5:08 pm, ChristopherL <clusard...@aol.com> wrote: > On Jan 16, 7:11 am, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote: > > > type Signed_8 is range - 2**7 .. 2**7 - 1; > > for Signed_8'Size use 8; > > I didn't use the second line in my program! Without it, my system > uses 10 bits for this. I'm going to use my solution for now. > > Also experimenting on my own, if I only make the below change the > variable Result has a value of -62 and not as above (-56). > > -- type Short_integer1 is range 0.. ((2 ** 8) - 1); > -- for Short_Integer1'Size use 8; > > type Short_integer1 is mod 2 ** 8; > for Short_Integer1'Size use 8; > > What does the following line of code do? It's new to me and have to > know! > > type Short_integer1 is mod 2 ** 8; > > Chris L. "mod" indicates a 'modular' type - i.e. unsigned, with wrap-round semantics (no exceptions). Analogous to C 'unsigned'. Useful for masking - using 'and', 'or', etc. If you want an unsigned 8-bit integer with those semantics, you can use the predefined type Interfaces.Unsigned_8. The package 'Interfaces' name indicates that you are 'close to the metal' in some why and you actually _care_ about the size of objects your manipulating. There is a very good Wikibook on Ada you know... http://en.wikibooks.org/wiki/Ada_Programming and in particular: http://en.wikibooks.org/wiki/Ada_Programming/Types/mod Cheers -- Martin ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [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 2 siblings, 0 replies; 14+ messages in thread From: Georg Bauhaus @ 2009-01-16 17:34 UTC (permalink / raw) ChristopherL schrieb: > On Jan 16, 7:11 am, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote: >> type Signed_8 is range - 2**7 .. 2**7 - 1; >> for Signed_8'Size use 8; > > I didn't use the second line in my program! Without it, my system > uses 10 bits for this. Is it actually doing that or is it just the debugger which says so? You can always test this with if Signed_8'Size = 10 then ... > What does the following line of code do? It's new to me and have to > know! > > type Short_integer1 is mod 2 ** 8; > Which text book are you using? This should be covered in every Ada text book in an early chapter. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [not found] <83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com> ` (2 preceding siblings ...) [not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com> @ 2009-01-16 12:18 ` Stuart 2009-01-16 23:14 ` sjw 3 siblings, 1 reply; 14+ messages in thread From: Stuart @ 2009-01-16 12:18 UTC (permalink / raw) "ChristopherL" <clusardi2k@aol.com> wrote in message news:83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com... > 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! <snip problem with Unchecked_Conversion> > I want to copy bits from an Ada float to a user defined short integer > maintaining the same bit representation. As said many times before this is a very unclear statement of what you are really trying to do and it is resulting in a lot of confusion. You really need to read through the replies in your previous thread - especially as you have re-introduced the confusion of bits and values again. <snip code> I will have another guess at what you might be trying to do and provide a solution: procedure ada_main is -- Things the OP says can't be changed: -- The definitions of Arg and Result2 can not chanage. subtype A_Float is Float; subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1); Arg : A_Float; Result2 : Short_integer2; -- Some things the OP has still not made clear: -- 1) What he is really trying to achieve. -- 2) Whether he wants to copy bits or values. -- 3) How he thinks values of Arg greater than 127.0 -- ought to be represented in an integer type with -- a range -128..127 -- 4) How rounding should take place (round up, round down, -- round nearest) -- Because of OP insistence that the the definitions cannot be changed -- and the fact Short_integer2 is based on integer which is implementation -- defined using Unchecked_Conversion here would be even more dubious -- than usual. -- Assuming he is expecting whole number values in Arg and -- wants a value in Result2 that is unique over the range -- 0.0 .. 200.0 and is easily relatable to the original -- value of Arg. -- Declare a sub-type capable of supporting all values of Arg and -- the computations we want to do: subtype S is Short_integer2'base range -256..200; -- REQUIREMENT: The lower bound of S must be such that -- Short_integer2'first-Short_integer'last-1 <= S'first <= -- -(max(Arg) + 1) -- The upper bound must be such that -- S'last >= max(Arg) -- NB the lower bound is chosen to control the underlying -- representation when Arg > Short_integer2'last -- Assumes the base type of Short_integer2 will allow this; -- compiler will complain if not. -- If the base type does not support the range, a suitable -- new type to do the integer based calculations will be needed -- and some explicit type conversions will have to be made. -- I can't be bothered to think of a meaningful name for the subtype!! Int_Arg : S; -- Holds the integer value of Arg. V : Short_integer2; pragma volatile(V); -- Just to force demonstration code to do something external begin Arg := 200.0; -- OP chosen test case! Int_Arg := S(Arg); -- Convert to an integer value using Ada default rounding. -- Resolve cases where the value is outside the range of Short_integer2 if (Int_Arg > Short_integer2'last) then Result2 := Int_Arg + S'first; -- Given the user specified constraints on Arg and the definition -- of Short_integer2 this will not exceed constraints. else Result2 := Int_Arg; end if; V := Result2; end ada_main; -- Stuart ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion 2009-01-16 12:18 ` Stuart @ 2009-01-16 23:14 ` sjw 0 siblings, 0 replies; 14+ messages in thread From: sjw @ 2009-01-16 23:14 UTC (permalink / raw) On Jan 16, 12:18 pm, "Stuart" <stu...@0.0> wrote: > "ChristopherL" <clusard...@aol.com> wrote in message > > news:83d19cd8-71ca-43c1-a3eb-05832290e565@r36g2000prf.googlegroups.com... > > > 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! > > <snip problem with Unchecked_Conversion> > > > I want to copy bits from an Ada float to a user defined short integer > > maintaining the same bit representation. (I'm sorry to reply to Stuart's post rather than OP's, but Google fails to show OP's posts at all! (perhaps there's a reason for that?)) Sorry to shout, Chris, but you CAN NOT FIT THE BITS OF A STANDARD FLOAT INTO ANYTHING SMALLER!!! IF YOU WANT THE SAME BIT REPRESENTATION YOU HAVE TO USE THE SAME NUMBER OF BITS!!! The following is another way of achieving unchecked conversion - the results on a little-endian machine (i686) are 2.00000E+02 -> 0 0 72 67 3.14159E+00 -> 219 15 73 64 with Ada.Numerics; with Ada.Text_IO; use Ada.Text_IO; with System.Storage_Elements; procedure Foo is F : Float; A : System.Storage_Elements.Storage_Array (1 .. 4); for A'Address use F'Address; pragma Import (Ada, A); -- prevent unwanted auto-initialisations function Image (B : System.Storage_Elements.Storage_Element) return String renames System.Storage_Elements.Storage_Element'Image; begin F := 200.0; Put_Line (Float'Image (F) & " -> " & Image (A (1)) & " " & Image (A (2)) & " " & Image (A (3)) & " " & Image (A (4))); F := Ada.Numerics.Pi; Put_Line (Float'Image (F) & " -> " & Image (A (1)) & " " & Image (A (2)) & " " & Image (A (3)) & " " & Image (A (4))); end Foo; ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion @ 2009-01-15 21:24 Martin [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com> 0 siblings, 1 reply; 14+ messages in thread From: Martin @ 2009-01-15 21:24 UTC (permalink / raw) On Jan 15, 8:53 pm, ChristopherL <clusard...@aol.com> wrote: > 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 ); > Result2: Short_integer; --to this, and I want to round Arg > > These 3 definitions of Arg and Result2 can not chanage. > > Arg will never be negative or greater than 200/201. > > 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 := Float'Size; --32 bits long > > Size_Of_short_integer: integer := Short_integer1'Size;--10 bits > long > Size_Of_short_int: integer := Short_integer2'Size;--10 bits long > > Size_Of_Natural: integer := A_Natural'Size; --8 bits long Are you sure about those sizes? Try: Put_Line ("Float'Size =" & Integer'Image (Float'Size)); Put_Line ("Short_integer1'Size =" & Integer'Image (Short_integer1'Size)); Put_Line ("Short_integer2'Size =" & Integer'Image (Short_integer2'Size)); Put_Line ("Natural'Size =" & Integer'Image (Natural'Size)); An aside - probably wasted if you don't understand subtypes and ranges - but you don't have to assign everything to 'integer'. Instead you can store the constants like this: Size_Of_Float : constant := Float'Size; The benefit is that you can mix this into just about any expression without having to bother about type conversions. > Result2 := Get_Bits (Result1); Result2 is of type Short_integer2 - which _you_ defined to be of the range -128 to 127 (inclusive). Why are you surprised that 201 does fit??? And that's assuming the Unchecked_Conversion works - double check the actual sizes! Cheers -- Martin ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com>]
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com> @ 2009-01-16 0:45 ` tmoran 2009-01-16 8:02 ` Martin 1 sibling, 0 replies; 14+ messages in thread From: tmoran @ 2009-01-16 0:45 UTC (permalink / raw) On Jan 15, 3:20 pm, ChristopherL <clusard...@aol.com> wrote: > On Jan 15, 5:24 pm, Martin <martin.do...@btopenworld.com> wrote: > > > And that's assuming the Unchecked_Conversion works - double > > check the actual sizes! > > In the debugger when I look at Float'Size it tells me 32. > When I look at Short_integer1'Size it tells me 10, and for > Short_integer2'Size it tell me 10. > > Chris L. An Unchecked_Conversion just copies bits. There's no way to copy 32 bits into something with room for only 10, and the compiler is pointing this out now. I suppose it could copy the leftmost 5 of the 32 bits, and the rightmost 5, and let you try to debug the result, but that isn't The Ada Way. Why don't you simply do a type conversion Result2 := Short_Integer2 (Result1); There will then be a run-time check and, if the value in Result1 will fit in a Short_Integer2, the assignment will take place. If it won't fit, it will raise an exception so you can debug your program. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: Restarting Tread: Why isn't this program working with Unchecked_Converstion [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com> 2009-01-16 0:45 ` tmoran @ 2009-01-16 8:02 ` Martin 1 sibling, 0 replies; 14+ messages in thread From: Martin @ 2009-01-16 8:02 UTC (permalink / raw) On Jan 15, 11:20 pm, ChristopherL <clusard...@aol.com> wrote: > On Jan 15, 5:24 pm, Martin <martin.do...@btopenworld.com> wrote: > > > And that's assuming the Unchecked_Conversion works - double > > check the actual sizes! > > In the debugger when I look at Float'Size it tells me 32. > When I look at Short_integer1'Size it tells me 10, and for > Short_integer2'Size it tell me 10. Ok, according the Bob Duff's explanation, that can happen. BUT hopefully you can now see that this is NOT PORTABLE and that you shouldn't rely on it - the next time you upgrade you compile (or change it completely), this may not be the case. Go and try the "GNAT GPL 2008" compiler on Windows from the ("GNAT Libre" site) and you'll see this. This is why using subtypes of Integer/Natural (their sizes are not always the same - just like C!!) are avoided and why you need to use your own types - see (lots of) other posts - if you want to be sure of sizes. This is the "Ada way". Don't fight it, it's easier but will require you to stop thinking in C. Cheers -- Martin ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2009-01-16 23:14 UTC | newest] Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [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 ` Restarting Tread: Why isn't this program working with anon [not found] ` <40239b21-b265-467f-9b27-5890fb2f4c67@w1g2000prm.googlegroups.com> 2009-01-16 2:27 ` Restarting Tread: Why isn't this program working with Unchecked_Converstion 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 2009-01-15 21:24 Martin [not found] ` <a8ef6226-db87-4c4c-b38e-9dbc77374f4c@t11g2000yqg.googlegroups.com> 2009-01-16 0:45 ` tmoran 2009-01-16 8:02 ` Martin
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox