?

Average Accuracy: 99.8% → 99.9%
Time: 5.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

Original99.8%
Target99.8%
Herbie99.9%
\[x \cdot x + y \cdot \left(y + \left(y + y\right)\right) \]

Derivation?

  1. Initial program 99.8%

    \[\left(\left(x \cdot x + y \cdot y\right) + y \cdot y\right) + y \cdot y \]
  2. Simplified99.9%

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

    [Start]99.8

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

    +-commutative [=>]99.8

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

    fma-def [=>]99.9

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

    associate-+l+ [=>]99.9

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

    fma-def [=>]99.9

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

    distribute-lft-out [=>]99.9

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

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

Alternatives

Alternative 1
Accuracy80.5%
Cost1101
\[\begin{array}{l} \mathbf{if}\;y \cdot y \leq 1.95 \cdot 10^{-180} \lor \neg \left(y \cdot y \leq 1.05 \cdot 10^{-133}\right) \land y \cdot y \leq 2.2 \cdot 10^{-123}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;3 \cdot \left(y \cdot y\right)\\ \end{array} \]
Alternative 2
Accuracy80.6%
Cost1101
\[\begin{array}{l} \mathbf{if}\;y \cdot y \leq 10^{-181} \lor \neg \left(y \cdot y \leq 4 \cdot 10^{-134}\right) \land y \cdot y \leq 2 \cdot 10^{-123}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot 3\right)\\ \end{array} \]
Alternative 3
Accuracy80.3%
Cost977
\[\begin{array}{l} t_0 := x \cdot x + y \cdot y\\ \mathbf{if}\;x \leq -350000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -3.05 \cdot 10^{-27}:\\ \;\;\;\;3 \cdot \left(y \cdot y\right)\\ \mathbf{elif}\;x \leq -3.1 \cdot 10^{-141} \lor \neg \left(x \leq 5.8 \cdot 10^{-62}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(y \cdot 3\right)\\ \end{array} \]
Alternative 4
Accuracy99.8%
Cost576
\[x \cdot x + y \cdot \left(y \cdot 3\right) \]
Alternative 5
Accuracy57.8%
Cost192
\[x \cdot x \]

Error

Reproduce?

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