Average Error: 0.1 → 0.0
Time: 3.5s
Precision: binary64
Cost: 13376
\[\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z \]
\[\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, z \cdot \left(z + z\right)\right)\right) \]
(FPCore (x y z)
 :precision binary64
 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
(FPCore (x y z) :precision binary64 (fma z z (fma x y (* z (+ z z)))))
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, z, fma(x, y, (z * (z + z))));
}
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 fma(z, z, fma(x, y, Float64(z * Float64(z + z))))
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[(z * z + N[(x * y + N[(z * N[(z + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, z \cdot \left(z + z\right)\right)\right)

Error

Target

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

Derivation

  1. Initial program 0.1

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

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

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

Alternatives

Alternative 1
Error13.5
Cost1620
\[\begin{array}{l} t_0 := 3 \cdot \left(z \cdot z\right)\\ \mathbf{if}\;z \cdot z \leq 5 \cdot 10^{-190}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \cdot z \leq 2.55 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \cdot z \leq 2.3 \cdot 10^{-92}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \cdot z \leq 6.6 \cdot 10^{-38}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \cdot z \leq 1.32 \cdot 10^{+20}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error13.5
Cost1620
\[\begin{array}{l} t_0 := z \cdot \left(z \cdot 3\right)\\ \mathbf{if}\;z \cdot z \leq 2 \cdot 10^{-193}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \cdot z \leq 2 \cdot 10^{-129}:\\ \;\;\;\;3 \cdot \left(z \cdot z\right)\\ \mathbf{elif}\;z \cdot z \leq 2.2 \cdot 10^{-92}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;z \cdot z \leq 2 \cdot 10^{-41}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \cdot z \leq 10^{+20}:\\ \;\;\;\;x \cdot y\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error0.1
Cost576
\[3 \cdot \left(z \cdot z\right) + x \cdot y \]
Alternative 4
Error0.1
Cost576
\[z \cdot \left(z \cdot 3\right) + x \cdot y \]
Alternative 5
Error24.3
Cost192
\[x \cdot y \]

Error

Reproduce

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