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

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

    [Start]0.1

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

    distribute-rgt-in [=>]0.1

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

    associate-+l+ [=>]0.1

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

    *-commutative [=>]0.1

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

    fma-def [=>]0.1

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

    distribute-lft-out [=>]0.1

    \[ \mathsf{fma}\left(x, y, \color{blue}{z \cdot \left(x + 5\right)}\right) \]
  3. Taylor expanded in x around 0 0.1

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

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

    [Start]0.1

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

    +-commutative [=>]0.1

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

    *-commutative [=>]0.1

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

    *-commutative [<=]0.1

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

    fma-def [=>]0.0

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

    +-commutative [=>]0.0

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

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

Alternatives

Alternative 1
Error0.1
Cost6848
\[\mathsf{fma}\left(x, y, z \cdot \left(5 + x\right)\right) \]
Alternative 2
Error12.9
Cost848
\[\begin{array}{l} t_0 := x \cdot \left(z + y\right)\\ t_1 := z \cdot \left(5 + x\right)\\ \mathbf{if}\;x \leq -0.0195:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq -8.4 \cdot 10^{-54}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq -2.15 \cdot 10^{-114}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 5.4 \cdot 10^{-10}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error24.6
Cost721
\[\begin{array}{l} \mathbf{if}\;x \leq -4.8 \cdot 10^{-16}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq -2.6 \cdot 10^{-54} \lor \neg \left(x \leq -5 \cdot 10^{-115}\right) \land x \leq 5.4 \cdot 10^{-10}:\\ \;\;\;\;z \cdot 5\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 4
Error24.7
Cost720
\[\begin{array}{l} \mathbf{if}\;x \leq -4.8 \cdot 10^{-16}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq -1.6 \cdot 10^{-54}:\\ \;\;\;\;z \cdot 5\\ \mathbf{elif}\;x \leq -2.15 \cdot 10^{-114}:\\ \;\;\;\;x \cdot y\\ \mathbf{elif}\;x \leq 5:\\ \;\;\;\;z \cdot 5\\ \mathbf{else}:\\ \;\;\;\;z \cdot x\\ \end{array} \]
Alternative 5
Error1.2
Cost713
\[\begin{array}{l} \mathbf{if}\;x \leq -550000000000 \lor \neg \left(x \leq 5\right):\\ \;\;\;\;x \cdot \left(z + y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y + z \cdot 5\\ \end{array} \]
Alternative 6
Error16.1
Cost585
\[\begin{array}{l} \mathbf{if}\;z \leq -6.2 \cdot 10^{-79} \lor \neg \left(z \leq 8.5 \cdot 10^{-147}\right):\\ \;\;\;\;z \cdot \left(5 + x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot y\\ \end{array} \]
Alternative 7
Error0.1
Cost576
\[x \cdot \left(z + y\right) + z \cdot 5 \]
Alternative 8
Error35.0
Cost192
\[z \cdot 5 \]

Error

Reproduce

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