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.3 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f6204cee2f9f77e2,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-01-12 04:27:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.icl.net!newsfeed.fjserv.net!opentransit.net!jussieu.fr!enst.fr!not-for-mail From: Michal Nowak Newsgroups: comp.lang.ada Subject: Constraint_Error in arithmetic expressions Date: Sun, 12 Jan 2003 13:36:57 +0100 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-2 Content-Transfer-Encoding: 7BIT X-Trace: avanie.enst.fr 1042374422 34870 137.194.161.2 (12 Jan 2003 12:27:02 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Sun, 12 Jan 2003 12:27:02 +0000 (UTC) Return-Path: X-Mailer: Calypso Version 3.30.00.00 (3) Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.13 Precedence: bulk X-Reply-To: vinnie@inetia.pl List-Unsubscribe: , List-Id: comp.lang.ada mail<->news gateway List-Post: List-Help: List-Subscribe: , Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:32937 Date: 2003-01-12T13:36:57+01:00 Hello, I've been reading chapter about exceptions in Cohen's "Ada as a Second Language". In section 14.3.1 Constraint_Error there are points concerning raising exception in arithmetic expressions: "In the evaluation of an arithmetic expression, an intermediate result that overflows may raise Constraint_Error, but it is not guaranteed to do so. There are several reasons for this: ... o As explained in Section 4.6, a type declared as type T is range A .. B; really declares T to be a subtype of some anonymous type whose range includes the range A .. B. For Constraint_Error to be raised, it is not enough for the subtype range A .. B to be exceeded; the wider base-subtype range T'Base'First .. T'Base'Last must be exceeded. Whether this happens depends on the anonymous type that the compiler selects for T." Suppose that if I declare: type T is range 1 .. 200; A : T := 150; B : T := 150; the compiler may choose 1 byte (0 .. 255) or 2 bytes (0 .. 65535) for example, for the base subtype. If I do: (A + B)/2 I will get Constraint_Error if the compiler has chosen the range (0 .. 255) for the base type (because 150 + 150 > 255) or I will not if it has chosen the range (0 .. 65535). For the rest of the post assume that the compiler chooses the base type to be in range (0 .. 255), so if I do: A : T := 100; B : T := 105; C : T := (A + B)/2; I may (or may not) get an exception, but if do: A : T := 150; B : T := 150; C : T := (A + B)/2; I will get an exception. Am I right? However, in "ASL" there is another paragraph just after the one mentioned above: o Some compilers may avoid Constraint_Error by using a type with even a larger range than the _base subtype_ to evaluate arithmetic expressions. For example, a compiler that represents values of type Integer in a word of memory may use two words to hold the result of a multiplication. This might avoid overflow in an expressions like A*B/C. I must say that it made me a bit confused. From the first quoted paragraph I concluded that if T'Base'First or T'Base'Last will be excluded I will get an exception. Reading the second made me think that it does not have to be true, so it contradicts the first one. I suppose that I'm missing something, but I don't know what. I appreciate any explanations, Michal -- ----------------------------------------------------------------- -- ___ _ -- / _ \ | | I Choose Ada: -- | |_| | ___| | _____ The Most Trusted Name in Software (TM) -- | _ | | __ | | __ | -- |_| |_| |_____|_ |_____|_ http://www.adaic.org/whyada/choose.html -- -- -----------------------------------------------------------------