|
ru.linux- RU.LINUX --------------------------------------------------------------------- From : Serguei Miridonov 2:5020/400 25 Mar 2005 22:03:42 To : Eugene B. Berdnikov Subject : Re: Need GUI development tool (Kylix or something) -------------------------------------------------------------------------------- Eugene B. Berdnikov wrote: > Serguei Miridonov <mirsev@cicese.mx> wrote: > SM> Мне кажется, что Вы ошибаетесь, поскольку определение "... так, как > SM> считает нужным" неприменимо к языку, которым можно описать алгоритм > SM> вычислений с жёстко заданной последовательностью. > > Можно, да. Расставив скобки. А если скобок нет - поведение не определено, > фактически это значит, что компилятор имеет право поступать так, как > захотели поступить его авторы. Только в том случае, если результат от порядка не зависит (см. ниже). > Ссылки. Сначала - B.Straustroup, "The C++ Programming Language", 3d ed.,] > Addson-Wesley, 1998. > > 6.2.2 Evaluation order. > > The order of evaluation of subexpressions within an expression is undefined. > In particular, you cannot assume, that expression is evaluated left to > right. For example... > [...] > Parentheses can be used to force grouping. For example, a*b/c means > (a*b)/c so parentheses must be used to get a*(b/c). Обратите внимание: "... a*b/c means (a*b)/c ..." И далее: > Стандарт C++ (возможно, это тоже драфт). ISO/IEC 14882:1998(E) > > Во введении (раздел 1.9) достаточно ясно сказано: > > [Note: operators can be regrouped according to the usual mathematical > ^^^^^^^^^^^^^^^^^^^^^^^^^^ > rules only where the operators really are associative or commutative. 9) Вот здесь ключевая фраза: "only where the operators really are associative or commutative". В случае же с double, float, complex, и даже иногда с integer, как хорошо показано далее в примере, operators _really_ are _not_ associative or commutative, т.е. в реальных вычислениях ассоциативность и коммутативность не обеспечиваются. А в таких случаях последовательность для этих операций - слева направо, что и написано в стандарте языка, а также проиллюстрировано в этом прекрасном примере: > For example, in the following fragment > > int a, b; /*...*/ > a = a + 32760 + b + 5; > > the expression statement behaves exactly the same as > > a = (((a + 32760) + b) + 5); > > due to the associativity and precedence of these operators. Thus, the > result of the sum (a + 32760) is next added to b, and that result is > then added to 5 which results in the value assigned to a. On a machine > in which overflows produce an exception and in which the range of values > representable by an int is [Ѕ 32768,+ 32767], the implementation cannot > rewrite this expression as > > a = ((a + b) + 32765); > > since if the values for a and b were, respectively, Ѕ 32754 and Ѕ 15, > the sum a + b would produce an exception while the original expression > would not; nor can the expression be rewritten either as > > a = ((a + 32765) + b); > > or > > a = (a + (b + 32765)); > > since the values for a and b might have been, respectively, 4 and Ѕ 8 > or Ѕ 17 an d 12. However on a machine in which overflows do not produce an > exception and in which the results of overflows are reversible, the above > expression statement can be rewritten by the implementation in any of the > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > above ways because the same result will occur. ] > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > Hасчёт умножений и делений специальных примеров в тексте нет, > хотя там такие же операции над полями порядка, > но сам текст, по-моему, претендует на общность. > > Hу можно ещё упомянуть 5й раздел там же: > > 5 Expressions > > 4. Except where noted, the order of evaluation of operands of individual > operators and subexpressions of individual expressions, and the order > in which side effects take place, is unspecified [53]. > -------------------------------------------------------------------- > > В общем, из общей логики - неопределённое поведение для потребителя > есть свобода действий для транслятора. Как раз с группировкой, ассоциативностью и коммутативностью - свобода минимальна: только то тех пор, пока результат не отличается от того, который получился бы при порядке вычислений предопределённым стандартом. --- ifmail v.2.15dev5.3 * Origin: CICESE Research Center, Ensenada, Mexico (2:5020/400) Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор Архивное /ru.linux/13396dedf5000.html, оценка из 5, голосов 10
|