Re: Does the order matter to add a sequence of floating numbers



On Oct 30, 4:10 pm, JosephLee <shoupe...@xxxxxxxxx> wrote:
Let's say float arr[100], we want to add them up, does the order
matter? what is the different between arr[0] +..+ arr[99], and
arr[99]+...+arr[0]?

Floating point addition is not associative. That means, the order does
matter. Within an expression, float operations should be evaluated in
a strict sequence that is defined in the standards. The reason is that
in float additions the two numbers must be shifted so that their
exponents match, which means truncation errors depend on the size of
each number. The effect may be highly significant - for example in IIR
filters and other operations that use feedback.

In C, the order for floating point evaluation is defined as left-to-
right through an expression, and respecting the order of sequence
points (, and ;). Many C compilers ignore the requirement to respect
the order of sequence points (strictly, all operations between two
sequence points must be completed and all their side effects evaluated
before any operation within the next papir of sequence points can
begin). Some compilers assume float is associative, which leads to
different results on different optimiztion settings. Some compilers
allow a pragma like dirty_float.

Chris
================
Chris Bore
BORES Signal Processing
www.bores.com

.



Relevant Pages

  • Re: Must an expression be evaluated before its value is used?
    ... this is not a sequence point. ... The evaluation dependency has a consequence of ... seperate the assignment of 5 to x from the assignment of 11 to x. ... comma operator without first evaluating it's second operand. ...
    (comp.std.c)
  • Scott & Andrei article on DCLP and threading... Flawed ?
    ... am unable to make a solid case for strict ordering of 1,2 and 3. ... "As we tried to point out in the article, sequence points don't offer ... state of the execution environment. ... There is a sequence point at the completion of evaluation of each ...
    (comp.lang.cpp)
  • Re: Linux X demo
    ... precedence to the order of evaluation. ... evaluation of expressions is undefined. ... THE ORDER OF EVALUATION BETWEEN SEQUENCE POINTS. ...
    (alt.lang.asm)
  • Re: Must an expression be evaluated before its value is used?
    ... properly define the ordering of sequence points. ... The evaluation order is defined by the abstract machine (even if it is ... there is a sequence point after the evaluation of the first operand. ... "2 The left operand of a comma operator is evaluated as a void ...
    (comp.std.c)
  • Re: "Variables" tutorial available (Windows, mingw/msvc)
    ... For the benefit of those on this newsgroup who haven't got the standard ... the order of evaluation of operands of individual ... previous and next sequence point a scalar object shall have its stored ... But the standard says that it _is_ undefined behaviour. ...
    (comp.lang.cpp)