?

Average Error: 0.2% → 0.08%
Time: 6.9s
Precision: binary64
Cost: 7104

?

\[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
\[\mathsf{fma}\left(z \cdot 2, z, z \cdot z\right) + x \cdot y \]
(FPCore (x y z)
 :precision binary64
 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
(FPCore (x y z) :precision binary64 (+ (fma (* z 2.0) z (* z z)) (* x y)))
double code(double x, double y, double z) {
	return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
double code(double x, double y, double z) {
	return fma((z * 2.0), z, (z * z)) + (x * y);
}
function code(x, y, z)
	return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z))
end
function code(x, y, z)
	return Float64(fma(Float64(z * 2.0), z, Float64(z * z)) + Float64(x * y))
end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
code[x_, y_, z_] := N[(N[(N[(z * 2.0), $MachinePrecision] * z + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\mathsf{fma}\left(z \cdot 2, z, z \cdot z\right) + x \cdot y

Error?

Target

Original0.2%
Target0.17%
Herbie0.08%
\[\left(3 \cdot z\right) \cdot z + y \cdot x \]

Derivation?

  1. Initial program 0.2

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

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

    [Start]0.2

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

    +-commutative [=>]0.2

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

    fma-def [=>]0.09

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

    associate-+l+ [=>]0.08

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

    fma-def [=>]0.08

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

    count-2 [=>]0.08

    \[ \mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, \color{blue}{2 \cdot \left(z \cdot z\right)}\right)\right) \]
  3. Applied egg-rr0.19

    \[\leadsto \color{blue}{\left(z \cdot z + 2 \cdot \left(z \cdot z\right)\right) + x \cdot y} \]
  4. Applied egg-rr0.08

    \[\leadsto \color{blue}{\mathsf{fma}\left(z \cdot 2, z, z \cdot z\right)} + x \cdot y \]
  5. Final simplification0.08

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

Alternatives

Alternative 1
Error18.88%
Cost1101
\[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 1.7 \cdot 10^{-81} \lor \neg \left(z \cdot z \leq 3.8 \cdot 10^{-47}\right) \land z \cdot z \leq 1.2 \cdot 10^{+23}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(z \cdot z\right) \cdot 3\\ \end{array} \]
Alternative 2
Error18.74%
Cost1100
\[\begin{array}{l} \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{-82}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \cdot z \leq 4 \cdot 10^{-47}:\\ \;\;\;\;z \cdot \left(z \cdot 3\right)\\ \mathbf{elif}\;z \cdot z \leq 5 \cdot 10^{+17}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(z \cdot z\right) \cdot 3\\ \end{array} \]
Alternative 3
Error0.22%
Cost576
\[x \cdot y + z \cdot \frac{z}{0.3333333333333333} \]
Alternative 4
Error0.19%
Cost576
\[x \cdot y + \left(z \cdot z\right) \cdot 3 \]
Alternative 5
Error37.63%
Cost192
\[x \cdot y \]

Error

Reproduce?

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

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

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