

Why a*a*a*a*a*a cannot be optimized to (a*a*a)*(a*a*a)? - ciupicri
http://stackoverflow.com/questions/6430448/why-aaaaaa-cannot-be-optimized-to-aaaaaa

======
dougws
When I first read the headline, I definitely thought that was a regular
expression which could be "optimized" to a+. I guess I'm more inclined to read
"*" as the Kleene operator than as multiplication...

~~~
Natsu
I'm glad to know that I'm not alone in doing this.

------
kaiwetzel
In numerical analysis class _numerical stability_ was a major theme[1]. It was
very definitely eye-opening how quickly rounding/truncation errors can bite
you in seemingly trivial situations!

On the other hand, if I have a function that works on parameters in a
specified range (say, 0.0 to 1.0) and returns results which is supposed to be
correct to a specified accuracy I would _love_ to have the compiler do all the
possible optimizations without having to specify a compiler flag (doing so
for, say, just a single function can be quite annoying!). Maybe approaches
like Haskell's type inference will, eventually, produce significantly faster
code because they can do things like this?

In a somewhat related case, it would be awesome if the compiler recognized
properties like associativeness, distributiveness, commutitiveness of
composite functions and rearange things optimally for performance. It's really
nice to see so much development in languages and compilers at the moment
(llvm, javascript, functional languages like Haskell, etc.) :°)

[1] <http://en.wikipedia.org/wiki/Numerical_stability>

~~~
ori_b
So you want something like GCC's -funsafe-math flag.

------
pixelbath
The short answer: Because floating point math is not associative.

[http://en.wikipedia.org/wiki/Floating_point#Accuracy_problem...](http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)

------
zheng
As a user of Stack Overflow only when google brings me there, I'm curious as
to why the answer received so many upvotes? It is certainly a good answer, but
the score seems disproportional to the utility.

~~~
retube
I just feel sorry for the guy that answered. He's got +685 upvotes at time of
writing and no dvs so he should have ~ 7,000 points from this, but apparently
SO imposes a daily cap of 200.

~~~
davweb
I think the Daily Cap is intended for situations just like this. So if you see
someone with 5k+ reputation it's a good sign they've given good answers to a
fair few questions (or asked good questions) over a period of time rather than
just got lucky with a question that found its way on to Twitter. Because
reputation entitles you do certain things like close questions SO is careful
about how you can earn it.

~~~
retube
Yeah makes sense. Still, I'd be a bit gutted :)

------
bxc
There are other ways of dealing with "fake real numbers" (which is what
doubles are)

One example is interval arithmetic.
<http://en.wikipedia.org/wiki/Interval_arithmetic>

None of them seem very mainstream: the predominant thinking is that floats are
"the right way" to badly approximate real numbers, that works well enough
except when you're working with pounds and pence, when "everyone" knows the
right way is to use fixed point...

That's not going to help the above associativity problem, but it fits in the
theme of "LAAAA LAAAA LAAAA lets gloss over the bits of floating point that
don't follow the rules"

------
nbpoole
Google Cache version:
[http://webcache.googleusercontent.com/search?q=cache%3Ahttp%...](http://webcache.googleusercontent.com/search?q=cache%3Ahttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F6430448%2Fwhy-
aaaaaa-cannot-be-optimized-to-aaaaaa)

------
dspillett
It can. But only for integer data types.

As already pointed out, for floating point data types the two versions could
(and probably will) result in slightly different answers due to rounding
issues.

~~~
Dylan16807
Why does it being different matter? Compilers can do all kinds of things that
change answers.

~~~
ori_b
They can't do it on code that doesn't invoke undefined behavior. This is not
undefined behavior.

~~~
Dylan16807
Obviously they can't do _whatever they want_ but they can decide if they want
to store variables in registers or main memory, they can change the order of a
variety of operations, etc. Does C specify order of evaluation with
multiplication? If so I wasn't aware of that. But you'll still have some
compilers giving you 32 bit precision on intermediate results and some giving
you 80 bits.

~~~
ori_b
> they can decide if they want to store variables in registers or main memory,
> they can change the order of a variety of operations, etc.

As long as they follow the "as if rule", which states that any transformations
that is done to code that does _not invoke undefined behavior_ will produce
the same final result as if the transformations were not done. Reordering
floating point multiplications does not follow this rule.

(Watching stuff in a debugger doesn't count for this rule)

~~~
Dylan16807
1\. Are you saying that multiplications _have_ a specified order? I asked that
but didn't get a response.

2\. The compiler is allowed to either leave the float inside the FPU or store
it back to main memory after each operation, is it not? These give different
results.

Somewhere around here I have a chart of the differences in floating point
outputs of different GCC versions. And no, fastmath is not on.

~~~
ori_b
Yes. Multiplication is left associative. I'm not sure that C defines the
precision of floating point operations, although I think it sets an upper
bound. I may be wrong, and I think that the C99 spec (optionally?) pushes
compilers to conform to the iee754

------
kleiba
Is this on the front page of HN because of the many upvotes it got, or did it
get the many upvotes because it is on the front page of HN?

~~~
sorbus
It got many upvotes (or a small number of upvotes directly after being posted)
which pushed it to the front page of HN, where it gets greatly increased
visibility and hence more upvotes.

------
pkulak
Because it brings down Stack Overflow?

