Average Error: 0.1 → 0.1
Time: 4.4s
Precision: binary64
Cost: 13376
\[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
\[\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right) \]
(FPCore (x y) :precision binary64 (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))
(FPCore (x y) :precision binary64 (fma y y (fma x x (* y (+ y y)))))
double code(double x, double y) {
	return (((x * x) + (y * y)) + (y * y)) + (y * y);
}
double code(double x, double y) {
	return fma(y, y, fma(x, x, (y * (y + y))));
}
function code(x, y)
	return Float64(Float64(Float64(Float64(x * x) + Float64(y * y)) + Float64(y * y)) + Float64(y * y))
end
function code(x, y)
	return fma(y, y, fma(x, x, Float64(y * Float64(y + y))))
end
code[x_, y_] := N[(N[(N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := N[(y * y + N[(x * x + N[(y * N[(y + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y
\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right)

Error

Target

Original0.1
Target0.1
Herbie0.1
\[x \cdot x + y \cdot \left(y + \left(y + y\right)\right) \]

Derivation

  1. Initial program 0.1

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Simplified0.1

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right)} \]
    Proof
    (fma.f64 y y (fma.f64 x x (*.f64 y (+.f64 y y)))): 0 points increase in error, 0 points decrease in error
    (fma.f64 y y (fma.f64 x x (Rewrite<= distribute-rgt-out_binary64 (+.f64 (*.f64 y y) (*.f64 y y))))): 1 points increase in error, 0 points decrease in error
    (fma.f64 y y (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x x) (+.f64 (*.f64 y y) (*.f64 y y))))): 1 points increase in error, 0 points decrease in error
    (fma.f64 y y (Rewrite<= associate-+l+_binary64 (+.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 y y)))): 4 points increase in error, 1 points decrease in error
    (Rewrite<= fma-def_binary64 (+.f64 (*.f64 y y) (+.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 y y)))): 16 points increase in error, 0 points decrease in error
    (Rewrite<= +-commutative_binary64 (+.f64 (+.f64 (+.f64 (*.f64 x x) (*.f64 y y)) (*.f64 y y)) (*.f64 y y))): 0 points increase in error, 0 points decrease in error
  3. Final simplification0.1

    \[\leadsto \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, y \cdot \left(y + y\right)\right)\right) \]

Alternatives

Alternative 1
Error12.8
Cost584
\[\begin{array}{l} t_0 := 3 \cdot \left(y \cdot y\right)\\ \mathbf{if}\;y \leq -2.7 \cdot 10^{-47}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 5.2 \cdot 10^{-83}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error0.1
Cost576
\[x \cdot x + 3 \cdot \left(y \cdot y\right) \]
Alternative 3
Error0.1
Cost576
\[y \cdot \left(y \cdot 3\right) + x \cdot x \]
Alternative 4
Error27.0
Cost192
\[x \cdot x \]

Error

Reproduce

herbie shell --seed 2022325 
(FPCore (x y)
  :name "Linear.Quaternion:$c/ from linear-1.19.1.3, E"
  :precision binary64

  :herbie-target
  (+ (* x x) (* y (+ y (+ y y))))

  (+ (+ (+ (* x x) (* y y)) (* y y)) (* y y)))