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: a07f3367d7,99a0564a79f2932b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.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: Type of subtraction operator Reply-To: anon@anon.org (anon) References: <8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Fri, 29 May 2009 02:09:30 GMT NNTP-Posting-Host: 12.65.204.65 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1243562970 12.65.204.65 (Fri, 29 May 2009 02:09:30 GMT) NNTP-Posting-Date: Fri, 29 May 2009 02:09:30 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:6097 Date: 2009-05-29T02:09:30+00:00 List-Id: The deal is that the GNAT compiler, like most other compilers preform some inline type conversion first, then calculate the value of Z as a constant since both X and Y value are known. And no error because in the case of Ada, Natural is a subtype of the Integer type. Z : Integer := Integer ( X ) - Integer ( Y ) ; -- converted to Z : Integer := -2 ; But, if you use the following; Z : Integer := Natural ( X - Y ) ; -- This should expand into the following statement unless you use -- pragma Suppress ( Range_Check ) ; -- Z : Integer := Integer ( Natural ( X - Y ) ) ; -- then to Z : Integer := Integer ( Natural ( -2 ) ) ; you will get the CONSTRAINT_ERROR. Note: Also, using the two statements Z : Integer := Natural ( X - Y ) ; pragma Suppress ( Range_Check, On => Z ) ; will will cause a CONSTRAINT_ERROR to occur at run time. But should it, since Range checking for Z has been suppressed. In <8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com>, Maciej Sobczak writes: >According to 4.5.3/2, each numeric type has a subtraction operator >with this specification: > >function "-"(Left, Right : T) return T > >This means that the type of difference is the same as the type of >operands. > >Which means that if T is Natural, then I should expect >CONSTRAINT_ERROR when the result of subtraction is less than 0. >But it does not happen: > >with Ada.Text_IO; >procedure Test is > > X : Natural := 5; > Y : Natural := 7; > Z : Integer := X - Y; -- here the result is not in Natural'Range > >begin > Ada.Text_IO.Put_Line (Integer'Image (Z)); >end Test; > >The above happily prints -2. >I thought initially that it is a subtype that is responsible for this, >but defining My_Natural type as a completely new type (with >appropriate type cast when Z is initialized) gives the same result. > >I conclude that the effective type of difference (the return type of >the "-" operator) is T'Base instead of T. >Is that right? Is this a bug in AARM or did I misunderstand something? > >-- >Maciej Sobczak * www.msobczak.com * www.inspirel.com > >Database Access Library for Ada: www.inspirel.com/soci-ada