What are examples of cases where floating-point $aaaa\ne(aa)(aa)$?

As explained in answers to this question on SO, due to non-associativity of floating-point arithmetic repeated multiplication like $aaaa$ can't be optimized to $(aa)(aa)$. Of course, aside from just precision in mantissa, multiplications can lead to overflow of exponent. But in this case of $n$-time multiplication are there any values of $a$ and mantissa length, so that, ignoring any peculiarities of exponent under-/overflows we'd get $$aaaa\ne(aa)(aa)?$$

asked Jun 23, 2015 at 11:40 6,885 2 2 gold badges 26 26 silver badges 58 58 bronze badges

$\begingroup$ And another double precision example $a = 1 + 2^ <-27>+ 2^$, with the terms in hex: \begin a &= 3FF0000002000001\\ aa &= 3FF0000004000002\\ (aa)a &= 3FF0000006000004\\ ((aa)a)a &= 3FF0000008000006\\ (aa)(aa) &= 3FF0000008000005 \end Hint: Round to nearest/even. $\endgroup$

Commented Jun 23, 2015 at 12:39

1 Answer 1

$\begingroup$

Let $a=1.5+u$, where $u$ is the machine epsilon near $1.5$ (that is, the smallest $u$ such that $1.5+u \ne 1.5$).

Then $a^2=2.25+3u+u^2$. Assuming rounding to nearest, this gives $a^2=2.25+4u$.

Then $(a^2)(a^2)=5.0625 + 18 u + 16 u^2$, which rounds to $5.0625 +20u$.

On the other hand, $a^3=a(a^2)=(1.5+u)(2.25+4u)=3.375+8.25 u+4 u^2$, which rounds to $3.375+8u$.

Finally, $a^4=a(a^3)=(1.5+u)(3.375+8u)=5.0625+15.375u+8u^2$, which rounds to $5.0625+16 u$.

So, $a(a(aa))\ne(aa)(aa)$ in this case.

Note how $a(a(aa))$ is more accurate than $(aa)(aa)$ in this case, even if it takes more multiplications.