comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
Date: Thu, 15 Jan 2009 22:57:04 +0200
Date: 2009-01-15T22:57:04+02:00	[thread overview]
Message-ID: <496fa2ca$0$2085$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>

ChristopherL wrote:
> How can I be getting the below error message with
> "Unchecked_Conversion".
> Isn't Unchecked_conversion just suppose to copy bits?

Perhaps because your Ada compiler applies the Ada 95 rules, in 
which a call of (an instance of) Unchecked_Conversion is 
"erroneous" if the result is a scalar with an "invalid 
representation" (RM95 13.9.1(12)). In your program, the assignment 
statement

    Result2 := Get_Bits (Result1);

applies Get_Bits to Result1, which has the value 201, so Get_Bits 
tries to return a Short_integer2 with the value 201, which is 
outside the range of Short_integer2 and so is an invalid 
representation. Your compiler may signal this error by raising the 
exception (but "erroneous" execution can have any result, in 
principle).

Under the Ada 2005 rules, you can get away with this assignment -- 
it is not "erroneous" -- but the only thing you can then do with 
Result2 is to check if it is 'Valid (RM05 13.9.1(12/2)); all other 
uses of Result2 are "erroneous" (until it is assigned some valid 
value).

One way to avoid this error is to remove the range constraints on 
Short_integer2, or widen them to include 201. However, even after 
reading all of this thread I don't understand what you want to 
achieve, so I don't know what to suggest -- except to ask your boss 
for a clearer statement of the problem.

> 
> 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 (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
> 
>   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
> 
>   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);
> 
> 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
> +++
> --------------------------------------------------------------------------------------------------------------

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2009-01-15 20:57 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com>
2009-01-14  1:33 ` How to put 200 into an integer sub-type of 16 bits (code included) Brian Drummond
     [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
2009-01-14  9:06     ` Ludovic Brenta
2009-01-14 10:08     ` Georg Bauhaus
2009-01-14 10:29       ` Georg Bauhaus
2009-01-14 12:47     ` Brian Drummond
2009-01-14 12:53     ` Brian Drummond
     [not found]       ` <f4894476-851e-493f-93a2-168976bd97fb@s1g2000prg.googlegroups.com>
2009-01-14 16:08         ` Adam Beneschan
2009-01-14 21:17           ` sjw
     [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
2009-01-14 20:41             ` Adam Beneschan
     [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
2009-01-14 22:47                 ` Adam Beneschan
2009-01-14 23:11                 ` Ludovic Brenta
2009-01-15  9:56                 ` Stuart
2009-01-15 12:40                 ` Stuart
2009-01-15 14:02                 ` Stephen Leake
2009-01-15 15:54                   ` Dmitry A. Kazakov
2009-01-15 16:29                     ` Hyman Rosen
2009-01-15 22:15                       ` Dmitry A. Kazakov
2009-01-16  9:11                         ` Jacob Sparre Andersen
2009-01-16 11:03                           ` Mike H
2009-01-16 22:16                           ` Brian Drummond
     [not found]               ` <c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com>
2009-01-15  2:39                 ` SteveD@teranews.com
     [not found]               ` <3625f980-4406-4f51-b494-dd4a48cab840@p36g2000prp.googlegroups.com>
2009-01-15  6:53                 ` Michael Bode
2009-01-15  8:57                   ` Martin
     [not found]               ` <fc154f52-7168-447a-bcd3-6ece9066ebf7@r37g2000prr.googlegroups.com>
2009-01-15 10:29                 ` Georg Bauhaus
2009-01-14 21:32             ` sjw
2009-01-14 21:51             ` Brian Drummond
2009-01-14 18:49     ` Jeffrey R. Carter
2009-01-15 10:12       ` Quote of the Day (Re: " Peter Hermann
2009-01-14 21:09   ` sjw
2009-01-14 21:16     ` Adam Beneschan
2009-01-14 23:09       ` Martin
2009-01-15  0:07         ` Adam Beneschan
2009-01-15  3:09           ` Randy Brukardt
2009-01-15 16:28             ` Adam Beneschan
2009-01-15 21:21               ` Robert A Duff
2009-01-16  1:17                 ` Adam Beneschan
2009-01-16 14:55                   ` Robert A Duff
2009-01-15 14:44     ` Brian Drummond
     [not found]       ` <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com>
2009-01-15 16:36         ` How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) Adam Beneschan
     [not found]         ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
2009-01-15 17:09           ` christoph.grein
2009-01-15 17:17             ` Adam Beneschan
2009-01-15 17:29               ` Georg Bauhaus
     [not found]                 ` <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>
2009-01-15 18:40                   ` Mike H
     [not found]                     ` <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>
2009-01-15 20:57                       ` Niklas Holsti [this message]
     [not found]                 ` <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com>
2009-01-16  3:17                   ` Steve D
     [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
2009-01-15 18:15               ` Martin
2009-01-15 18:34               ` Stuart
2009-01-15 17:23           ` Georg Bauhaus
     [not found]         ` <d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com>
2009-01-15 17:50           ` Stuart
2009-01-15 20:48 Martin
     [not found] ` <8a7769ad-133f-4587-9239-34ce90978aa1@v38g2000yqb.googlegroups.com>
2009-01-22  1:30   ` Keith Thompson
replies disabled

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