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: a07f3367d7,99a0564a79f2932b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!p21g2000prn.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Type of subtraction operator Date: Thu, 28 May 2009 14:54:28 -0700 (PDT) Organization: http://groups.google.com Message-ID: <82dc202a-e725-4056-9a5b-967b2a47c1df@p21g2000prn.googlegroups.com> References: <8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1243547668 28781 127.0.0.1 (28 May 2009 21:54:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 28 May 2009 21:54:28 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p21g2000prn.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:6089 Date: 2009-05-28T14:54:28-07:00 List-Id: On May 28, 2:23=A0pm, Maciej Sobczak wrote: > 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: Natural is not a type. It's a subtype. The subtraction operator is defined on the underlying type, Integer's type, rather than the subtype. (Integer is also a subtype, rather than a type; see 3.5.4 (11).) The result of the function is then converted to the subtype Integer, and no Constraint_Error occurs since -2 is within Integer's range. > with Ada.Text_IO; > procedure Test is > > =A0 =A0X : Natural :=3D 5; > =A0 =A0Y : Natural :=3D 7; > =A0 =A0Z : Integer :=3D X - Y; =A0 -- here the result is not in Natural'R= ange > > begin > =A0 =A0Ada.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. My_Natural is not a type either. It's a subtype of some new integer base type whose base range will include both negative and positive numbers (symmetric about zero)---see 3.5.4(1), 3.5.4(9). > I conclude that the effective type of difference (the return type of > the "-" operator) is T'Base instead of T. No, it's T. 4.5.3(1) refers to T as a type, not a subtype. Your mistake, which is natural enough (hee hee hee), was in assuming that My_Natural is a type. The "-" operator is defined on the base type of which My_Natural is a subtype. Yep, it's confusing. It took me some time to figure all this out too. -- Adam