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.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!IBM.COM!NCOHEN From: NCOHEN@IBM.COM ("Norman H. Cohen") Newsgroups: comp.lang.ada Subject: AND and expression evaluation Message-ID: <9005241409.AA10929@ajpo.sei.cmu.edu> Date: 24 May 90 13:21:10 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet List-Id: Ref: INFO-ADA Digest Volume 90 Issue 104 (Wed, May 23, 1990) Item #2 INFO-ADA Digest Volume 90 Issue 104 (Wed, May 23, 1990) Item #3 Rich Pattis (pattis@june.cs.washington.edu) quoted Ada as a Second Language out of context. A more complete excerpt is: > ... In the evaluation of an arithmetic expression, an intermediate > result that overflows may raise Numeric_Error, but it is not > guaranteed to do so. There are several reasons for this: > > An implementation may choose not to raise Numeric_Error in an > expression whose ultimate result can be determined without > actually performing the overflowing operation. For example, > an implementation may choose to optimize the expression > > b /= 0 AND a/b > c > > by evaluating it as if it were written > > b /= 0 AND THEN a/b > c > > Then a value of zero for b would not raise Numeric_Error. > However, this optimization is up to the individual compiler. > A programmer is guaranteed of avoiding the exception only by > writing the short circuit control form. > > ... Thus John Barnes and I agree that the "normal" semantics of AND is to evaluate both operands, and that the only way to GUARANTEE that the right operand won't be evaluated when b=0 is to write AND THEN. The optimization of not evaluating the right operand of AND, even though the division operator might raise an exception, is allowed by reference manual paragraph 11.6(7), which states, "A predefined operation need not be invoked at all, if its only possible effect is to propagate a predefined exception." If the right operand had been My_Function (a,b) > c rather than a/b > c such an optimization would not be allowed. (By the way, AI-00387 now allows and encourages an implementation to raise Constraint_Error rather than Numeric_Error in contexts where the reference manual calls for Numeric_Error to be raised.)