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,b3c479d8e293030d X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Unconstrained base subtype questions Date: Thu, 31 Mar 2011 22:18:29 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx03.eternal-september.org; posting-host="dFCm8HWntFqmDIilBLqEJQ"; logging-data="10819"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nx/5wleoRgi8LFm2+p+WpowHToPTyxF4=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (darwin) Cancel-Lock: sha1:dIAvgrcjAYVkFTQy9sJMXoVLOYE= sha1:H1bg8NNDIhoUMu5tEGpbwmgYT8s= Xref: g2news2.google.com comp.lang.ada:19607 Date: 2011-03-31T22:18:29+01:00 List-Id: "Alex Mentis" writes: > The following does not cause a constraint error in my version of GNAT > on my system: > > ... > > Integer_Result := (Integer'Last + Integer'Last) / 2; > > ... > > > If I understand correctly, this is because the Integer operators are > defined for operands of type Integer'Base, which is an unconstrained > subtype and allows the operands to be stored in extended-length > registers so that intermediate values in calculations do not overflow. > > My questions are: > > 1) Do I understand correctly what's going on? > > 2) Does the language make any guarantees about preventing spurious > overflow, or am I just getting lucky with my compiler/architecture? If > guarantees are made by the language, what are they? That's a compile-time calculation, and any Ada compiler should work it out using infinite-precision arithmetic. with Ada.Text_IO; use Ada.Text_IO; procedure Very_Large is Integer_Result : Integer; begin Integer_Result := 10**128 / 10**127; Put_Line (Integer'Image (Integer_Result)); end Very_Large; $ gnatmake very_large.adb gcc -c very_large.adb gnatbind -x very_large.ali gnatlink very_large.ali $ ./very_large 10 As against with Ada.Text_IO; use Ada.Text_IO; procedure Very_Large is Integer_Result : Integer; begin Integer_Result := Integer'Last; Integer_Result := Integer_Result + Integer'Last; Integer_Result := Integer_Result / 2; Put_Line (Integer'Image (Integer_Result)); end Very_Large; $ gnatmake very_large.adb gcc -c very_large.adb very_large.adb:6:37: warning: value not in range of type "Standard.Integer" very_large.adb:6:37: warning: "Constraint_Error" will be raised at run time gnatbind -x very_large.ali gnatlink very_large.ali $ ./very_large raised CONSTRAINT_ERROR : very_large.adb:6 overflow check failed Note that the compiler knew that was going to happen. If the overflow wasn't visible at compile time, you'd have to tell GNAT to perform run-time integer overflow checks using -gnato. Other compiler writers may have different views about whether run-time integer overflow checks should be off by default :-)