Average Error: 0.2 → 0.1
Time: 7.4s
Precision: binary64
Cost: 6848
\[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
\[\mathsf{fma}\left(x, x \cdot 9 - 12, 3\right) \]
(FPCore (x) :precision binary64 (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))
(FPCore (x) :precision binary64 (fma x (- (* x 9.0) 12.0) 3.0))
double code(double x) {
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
double code(double x) {
	return fma(x, ((x * 9.0) - 12.0), 3.0);
}
function code(x)
	return Float64(3.0 * Float64(Float64(Float64(Float64(x * 3.0) * x) - Float64(x * 4.0)) + 1.0))
end
function code(x)
	return fma(x, Float64(Float64(x * 9.0) - 12.0), 3.0)
end
code[x_] := N[(3.0 * N[(N[(N[(N[(x * 3.0), $MachinePrecision] * x), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
code[x_] := N[(x * N[(N[(x * 9.0), $MachinePrecision] - 12.0), $MachinePrecision] + 3.0), $MachinePrecision]
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\mathsf{fma}\left(x, x \cdot 9 - 12, 3\right)

Error

Target

Original0.2
Target0.1
Herbie0.1
\[3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right) \]

Derivation

  1. Initial program 0.2

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Simplified0.1

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 9, -12\right), 3\right)} \]
    Proof
    (fma.f64 x (fma.f64 x 9 -12) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 x (fma.f64 x (Rewrite<= metadata-eval (*.f64 3 3)) -12) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 x (fma.f64 x (*.f64 3 3) (Rewrite<= metadata-eval (*.f64 -4 3))) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 x (fma.f64 x (*.f64 3 3) (*.f64 (Rewrite<= metadata-eval (neg.f64 4)) 3)) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 x (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x (*.f64 3 3)) (*.f64 (neg.f64 4) 3))) 3): 0 points increase in error, 4 points decrease in error
    (fma.f64 x (+.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x 3) 3)) (*.f64 (neg.f64 4) 3)) 3): 8 points increase in error, 3 points decrease in error
    (fma.f64 x (Rewrite<= distribute-rgt-in_binary64 (*.f64 3 (+.f64 (*.f64 x 3) (neg.f64 4)))) 3): 1 points increase in error, 0 points decrease in error
    (fma.f64 x (*.f64 3 (Rewrite<= sub-neg_binary64 (-.f64 (*.f64 x 3) 4))) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 x (Rewrite<= *-commutative_binary64 (*.f64 (-.f64 (*.f64 x 3) 4) 3)) 3): 0 points increase in error, 0 points decrease in error
    (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x (*.f64 (-.f64 (*.f64 x 3) 4) 3)) 3)): 0 points increase in error, 0 points decrease in error
    (+.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x (-.f64 (*.f64 x 3) 4)) 3)) 3): 17 points increase in error, 11 points decrease in error
    (+.f64 (*.f64 (Rewrite<= distribute-rgt-out--_binary64 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 4 x))) 3) 3): 1 points increase in error, 0 points decrease in error
    (+.f64 (*.f64 (-.f64 (*.f64 (*.f64 x 3) x) (Rewrite<= *-commutative_binary64 (*.f64 x 4))) 3) 3): 0 points increase in error, 0 points decrease in error
    (Rewrite=> distribute-lft1-in_binary64 (*.f64 (+.f64 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 x 4)) 1) 3)): 3 points increase in error, 0 points decrease in error
    (Rewrite<= *-commutative_binary64 (*.f64 3 (+.f64 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 x 4)) 1))): 0 points increase in error, 0 points decrease in error
  3. Taylor expanded in x around 0 0.1

    \[\leadsto \mathsf{fma}\left(x, \color{blue}{9 \cdot x - 12}, 3\right) \]
  4. Final simplification0.1

    \[\leadsto \mathsf{fma}\left(x, x \cdot 9 - 12, 3\right) \]

Alternatives

Alternative 1
Error0.1
Cost832
\[3 \cdot \left(1 + \left(3 \cdot \left(x \cdot x\right) + x \cdot -4\right)\right) \]
Alternative 2
Error0.2
Cost832
\[3 \cdot \left(\left(x \cdot \left(x \cdot 3\right) + x \cdot -4\right) + 1\right) \]
Alternative 3
Error0.9
Cost712
\[\begin{array}{l} t_0 := x \cdot \left(x \cdot 9 - 12\right)\\ \mathbf{if}\;x \leq -0.8676585674290122:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10266196555053207:\\ \;\;\;\;3 + x \cdot -12\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error1.9
Cost584
\[\begin{array}{l} t_0 := 9 \cdot \left(x \cdot x\right)\\ \mathbf{if}\;x \leq -0.8676585674290122:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10266196555053207:\\ \;\;\;\;3\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error1.5
Cost584
\[\begin{array}{l} t_0 := 9 \cdot \left(x \cdot x\right)\\ \mathbf{if}\;x \leq -336.42660365535187:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10266196555053207:\\ \;\;\;\;3 + x \cdot -12\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error1.5
Cost584
\[\begin{array}{l} t_0 := x \cdot \left(x \cdot 9\right)\\ \mathbf{if}\;x \leq -336.42660365535187:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.10266196555053207:\\ \;\;\;\;3 + x \cdot -12\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error1.9
Cost576
\[3 \cdot \left(x \cdot \left(x \cdot 3\right) + 1\right) \]
Alternative 8
Error20.7
Cost64
\[3 \]

Error

Reproduce

herbie shell --seed 2022291 
(FPCore (x)
  :name "Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D"
  :precision binary64

  :herbie-target
  (+ 3.0 (- (* (* 9.0 x) x) (* 12.0 x)))

  (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))