3 ms·
Compilers do no such thing: they do not assume that floating point + and * are associative, because they most definitely aren't. Roundoff errors are different,
by not2b 7y ago
Compilers do no such thing: they do not assume that floating point + and * are associative, because they most definitely aren't. Roundoff errors are different, and the effect of this can be huge.
Consider (a+b)+c vs a+(b+c).
Now suppose a is 1, b is 2.0^60, c is -b.
The first expression evaluates to 0, the second evaluates to 1.
Scientific and engineering code is carefully designed for numerical stability and compilers must not mess this up.
gcc has a "fast math" flag you can use if you don't care about the accuracy of your results.
- evozer 7y agoI don't understand your example, why would (1 + 2.060) - 2.060 == 0 while 1 + (2.060 - 2.060) == 1? Am I just misunderstanding what you wrote?
- not2b 7y agoNo, the site messed up my comment. b is supposed to be 2 raised to the 60th power. The two asterisks were removed. Let's try ^, b is 2^60, c is minus b. I edited my original comment.