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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f9957894e0bdf128 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!feeder.erje.net!feeder2.news.saunalahti.fi!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Thu, 15 Jan 2009 22:57:04 +0200 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) References: <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com> <71gqm49eatq868htrvd7eghm3m8su8kcbl@4ax.com> <7b017de2-951a-414a-8290-111353fe02f8@r15g2000prd.googlegroups.com> <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com> <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com> <9fa07f9b-90bf-4a28-bf8e-09c2152bd43f@o40g2000prn.googlegroups.com> <9b117764-7b49-4b76-98b2-f7fee00c8c76@z27g2000prd.googlegroups.com> <496f726e$0$30222$9b4e6d93@newsspool1.arcor-online.net> <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <496fa2ca$0$2085$4f793bc4@news.tdc.fi> Organization: TDC Internet Services NNTP-Posting-Host: laku61.adsl.netsonic.fi X-Trace: 1232052939 news.tdc.fi 2085 81.17.205.61:32915 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:3352 Date: 2009-01-15T22:57:04+02:00 List-Id: 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 . @ .