The Motley Fool Discussion Boards

Previous Page

Fun & Games / Math Recreations & Puzzles

URL:  http://boards.fool.com/rev2217norm-the-standards-of-most-programming-29567152.aspx

Subject:  Re: A (complex) puzzle Date:  9/27/2011  11:15 AM
Author:  rmhj Number:  5793 of 5865

rev2217/norm: The standards of most programming languages, including Fortran and C/C++, explicitly forbid optimizations that fail to preserve the integrity of parentheses in order to give programmers control of the sequence of operations when it matters. Thus, such an optimization is illegal.

This is (or was) part of the FORTRAN (IV) standard; I can't say about newer FORTRANs, though I doubt it has changed. It is *not* part of the C standard, and never has been. C guarantees ordering of statements separated by &&, ||, ^^, and a few others, but guarantees nothing about optimization. From Harbison & Steele, Fifth Edition, Section 7.3.3, "Parenthesized Expressions": "Parentheses do not necessarily force a particular evaluation order.... The purpose of the parenthesized expression is simply to delimit the enclosed expression for grouping purposes, either to defeat the default precedence of operators or make the code more readable."

That said, I doubt that many C optimizers would apply this identity to optimize this expression.

rj
Copyright 1996-2014 trademark and the "Fool" logo is a trademark of The Motley Fool, Inc. Contact Us