?

Average Error: 0.22% → 0.09%
Time: 8.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.22%
Target0.2%
Herbie0.09%
\[x \cdot x + y \cdot \left(y + \left(y + y\right)\right) \]

Derivation?

  1. Initial program 0.22

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

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

    [Start]0.22

    \[ \left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]

    +-commutative [=>]0.22

    \[ \color{blue}{y \cdot y + \left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right)} \]

    fma-def [=>]0.1

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

    associate-+l+ [=>]0.09

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

    fma-def [=>]0.09

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

    distribute-lft-out [=>]0.09

    \[ \mathsf{fma}\left(y, y, \mathsf{fma}\left(x, x, \color{blue}{y \cdot \left(y + y\right)}\right)\right) \]
  3. Final simplification0.09

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

Alternatives

Alternative 1
Error0.21%
Cost6848
\[\mathsf{fma}\left(x, x, 3 \cdot \left(y \cdot y\right)\right) \]
Alternative 2
Error19.94%
Cost1241
\[\begin{array}{l} t_0 := 3 \cdot \left(y \cdot y\right)\\ t_1 := y \cdot y + x \cdot x\\ \mathbf{if}\;y \leq -7.6 \cdot 10^{-13}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -4.9 \cdot 10^{-48}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq -4.4 \cdot 10^{-90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 1.1 \cdot 10^{-114} \lor \neg \left(y \leq 2.3 \cdot 10^{-75}\right) \land y \leq 4.5 \cdot 10^{-56}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot 3\right)\\ \end{array} \]
Alternative 3
Error0.22%
Cost960
\[y \cdot y + \left(y \cdot y + \left(y \cdot y + x \cdot x\right)\right) \]
Alternative 4
Error20.63%
Cost850
\[\begin{array}{l} \mathbf{if}\;y \leq -3.6 \cdot 10^{-12} \lor \neg \left(y \leq -9.2 \cdot 10^{-48} \lor \neg \left(y \leq -7.6 \cdot 10^{-91}\right) \land y \leq 5 \cdot 10^{-114}\right):\\ \;\;\;\;3 \cdot \left(y \cdot y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
Alternative 5
Error20.67%
Cost848
\[\begin{array}{l} t_0 := 3 \cdot \left(y \cdot y\right)\\ \mathbf{if}\;y \leq -3.6 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq -7.2 \cdot 10^{-48}:\\ \;\;\;\;x \cdot x\\ \mathbf{elif}\;y \leq -4.4 \cdot 10^{-90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;y \leq 8 \cdot 10^{-115}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot 3\right)\\ \end{array} \]
Alternative 6
Error0.22%
Cost576
\[3 \cdot \left(y \cdot y\right) + x \cdot x \]
Alternative 7
Error41.31%
Cost456
\[\begin{array}{l} \mathbf{if}\;x \leq -7.8 \cdot 10^{-118}:\\ \;\;\;\;x \cdot x\\ \mathbf{elif}\;x \leq 2.6 \cdot 10^{-160}:\\ \;\;\;\;y \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
Alternative 8
Error43.46%
Cost192
\[x \cdot x \]

Error

Reproduce?

herbie shell --seed 2023090 
(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)))