Average Error: 0.1 → 0.1
Time: 5.1s
Precision: binary64
Cost: 6976
\[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
\[\mathsf{fma}\left(x \cdot x, 9, x \cdot -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 (+ (* x -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, ((x * -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(Float64(x * x), 9.0, Float64(Float64(x * -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[(N[(x * x), $MachinePrecision] * 9.0 + N[(N[(x * -12.0), $MachinePrecision] + 3.0), $MachinePrecision]), $MachinePrecision]
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\mathsf{fma}\left(x \cdot x, 9, x \cdot -12 + 3\right)

Error

Target

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

Derivation

  1. Initial program 0.1

    \[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(3, x \cdot \left(3 \cdot x - 4\right), 3\right)} \]
    Proof
    (fma.f64 3 (*.f64 x (-.f64 (*.f64 3 x) 4)) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 3 (*.f64 x (-.f64 (Rewrite<= *-commutative_binary64 (*.f64 x 3)) 4)) 3): 0 points increase in error, 0 points decrease in error
    (fma.f64 3 (Rewrite<= distribute-lft-out--_binary64 (-.f64 (*.f64 x (*.f64 x 3)) (*.f64 x 4))) 3): 1 points increase in error, 0 points decrease in error
    (fma.f64 3 (-.f64 (Rewrite<= *-commutative_binary64 (*.f64 (*.f64 x 3) x)) (*.f64 x 4)) 3): 0 points increase in error, 0 points decrease in error
    (Rewrite<= fma-def_binary64 (+.f64 (*.f64 3 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 x 4))) 3)): 7 points increase in error, 4 points decrease in error
    (+.f64 (*.f64 3 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 x 4))) (Rewrite<= metadata-eval (*.f64 3 1))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= distribute-lft-in_binary64 (*.f64 3 (+.f64 (-.f64 (*.f64 (*.f64 x 3) x) (*.f64 x 4)) 1))): 3 points increase in error, 0 points decrease in error
  3. Taylor expanded in x around 0 0.1

    \[\leadsto \color{blue}{-12 \cdot x + \left(3 + 9 \cdot {x}^{2}\right)} \]
  4. Simplified0.1

    \[\leadsto \color{blue}{\mathsf{fma}\left(-12, x, 3\right) + x \cdot \left(x \cdot 9\right)} \]
    Proof
    (+.f64 (fma.f64 -12 x 3) (*.f64 x (*.f64 x 9))): 0 points increase in error, 0 points decrease in error
    (+.f64 (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -12 x) 3)) (*.f64 x (*.f64 x 9))): 0 points increase in error, 0 points decrease in error
    (+.f64 (+.f64 (*.f64 -12 x) 3) (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x x) 9))): 12 points increase in error, 14 points decrease in error
    (+.f64 (+.f64 (*.f64 -12 x) 3) (Rewrite<= *-commutative_binary64 (*.f64 9 (*.f64 x x)))): 0 points increase in error, 0 points decrease in error
    (+.f64 (+.f64 (*.f64 -12 x) 3) (*.f64 9 (Rewrite<= unpow2_binary64 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate-+r+_binary64 (+.f64 (*.f64 -12 x) (+.f64 3 (*.f64 9 (pow.f64 x 2))))): 0 points increase in error, 0 points decrease in error
  5. Applied egg-rr0.1

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 9, \mathsf{fma}\left(-12, x, 3\right)\right)} \]
  6. Applied egg-rr0.1

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

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

Alternatives

Alternative 1
Error1.0
Cost840
\[\begin{array}{l} \mathbf{if}\;x \leq -0.58:\\ \;\;\;\;x \cdot \left(-12 + x \cdot 9\right)\\ \mathbf{elif}\;x \leq 0.58:\\ \;\;\;\;x \cdot -12 + 3\\ \mathbf{else}:\\ \;\;\;\;x \cdot -12 + \left(x \cdot x\right) \cdot 9\\ \end{array} \]
Alternative 2
Error1.0
Cost712
\[\begin{array}{l} t_0 := x \cdot \left(-12 + x \cdot 9\right)\\ \mathbf{if}\;x \leq -0.58:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.58:\\ \;\;\;\;x \cdot -12 + 3\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error2.0
Cost584
\[\begin{array}{l} t_0 := \left(x \cdot x\right) \cdot 9\\ \mathbf{if}\;x \leq -0.58:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 0.2:\\ \;\;\;\;3\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error2.0
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -0.58:\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \mathbf{elif}\;x \leq 0.2:\\ \;\;\;\;3\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot x\right) \cdot 9\\ \end{array} \]
Alternative 5
Error1.5
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -1.55:\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \mathbf{elif}\;x \leq 0.215:\\ \;\;\;\;x \cdot -12 + 3\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot x\right) \cdot 9\\ \end{array} \]
Alternative 6
Error0.1
Cost576
\[3 + x \cdot \left(-12 + x \cdot 9\right) \]
Alternative 7
Error21.5
Cost64
\[3 \]

Error

Reproduce

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