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

Error

Target

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

Derivation

  1. Initial program 0.1

    \[x \cdot \left(y + z\right) + z \cdot 5 \]
  2. Taylor expanded in x around 0 0.1

    \[\leadsto \color{blue}{\left(y + z\right) \cdot x + 5 \cdot z} \]
  3. Simplified0.0

    \[\leadsto \color{blue}{\mathsf{fma}\left(z, 5, \left(y + z\right) \cdot x\right)} \]
    Proof
    (fma.f64 z 5 (*.f64 (+.f64 y z) x)): 0 points increase in error, 0 points decrease in error
    (fma.f64 z 5 (Rewrite<= *-commutative_binary64 (*.f64 x (+.f64 y z)))): 0 points increase in error, 0 points decrease in error
    (Rewrite=> fma-udef_binary64 (+.f64 (*.f64 z 5) (*.f64 x (+.f64 y z)))): 10 points increase in error, 0 points decrease in error
    (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 x (+.f64 y z)) (*.f64 z 5))): 0 points increase in error, 0 points decrease in error
    (+.f64 (Rewrite=> *-commutative_binary64 (*.f64 (+.f64 y z) x)) (*.f64 z 5)): 0 points increase in error, 0 points decrease in error
    (+.f64 (*.f64 (+.f64 y z) x) (Rewrite=> *-commutative_binary64 (*.f64 5 z))): 0 points increase in error, 0 points decrease in error
  4. Final simplification0.0

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

Alternatives

Alternative 1
Error0.1
Cost6848
\[\mathsf{fma}\left(x, z + y, z \cdot 5\right) \]
Alternative 2
Error16.0
Cost848
\[\begin{array}{l} t_0 := z \cdot \left(5 + x\right)\\ \mathbf{if}\;z \leq -7.4 \cdot 10^{-35}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.2 \cdot 10^{-120}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;z \leq 4.7 \cdot 10^{-67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 2.8 \cdot 10^{-43}:\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error23.7
Cost720
\[\begin{array}{l} \mathbf{if}\;x \leq -6.2 \cdot 10^{-16}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;x \leq 3.2 \cdot 10^{-63}:\\ \;\;\;\;z \cdot 5\\ \mathbf{elif}\;x \leq 10^{+43}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;x \leq 7.5 \cdot 10^{+169}:\\ \;\;\;\;z \cdot x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]
Alternative 4
Error1.2
Cost712
\[\begin{array}{l} t_0 := \left(z + y\right) \cdot x\\ \mathbf{if}\;x \leq -240000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 5:\\ \;\;\;\;z \cdot 5 + y \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error13.2
Cost584
\[\begin{array}{l} t_0 := z \cdot \left(5 + x\right)\\ \mathbf{if}\;z \leq -1.42 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;z \leq 6 \cdot 10^{+27}:\\ \;\;\;\;\left(z + y\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error0.1
Cost576
\[z \cdot 5 + \left(z + y\right) \cdot x \]
Alternative 7
Error24.9
Cost456
\[\begin{array}{l} \mathbf{if}\;z \leq -1.35 \cdot 10^{-24}:\\ \;\;\;\;z \cdot 5\\ \mathbf{elif}\;z \leq 6 \cdot 10^{+27}:\\ \;\;\;\;y \cdot x\\ \mathbf{else}:\\ \;\;\;\;z \cdot 5\\ \end{array} \]
Alternative 8
Error40.2
Cost192
\[y \cdot x \]

Error

Reproduce

herbie shell --seed 2022330 
(FPCore (x y z)
  :name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendOutside from plot-0.2.3.4, C"
  :precision binary64

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

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