?

Average Accuracy: 99.8% → 99.9%
Time: 11.4s
Precision: binary64
Cost: 13248

?

\[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
\[\mathsf{fma}\left(x, x \cdot 9, \mathsf{fma}\left(-12, x, 3\right)\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) (fma -12.0 x 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), fma(-12.0, x, 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(x * 9.0), fma(-12.0, x, 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[(x * 9.0), $MachinePrecision] + N[(-12.0 * x + 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, x \cdot 9, \mathsf{fma}\left(-12, x, 3\right)\right)

Error?

Target

Original99.8%
Target99.9%
Herbie99.9%
\[3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right) \]

Derivation?

  1. Initial program 99.8%

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

    \[\leadsto \color{blue}{3 \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(3, x, -4\right), 1\right)} \]
    Proof

    [Start]99.8

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

    *-commutative [=>]99.8

    \[ 3 \cdot \left(\left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) + 1\right) \]

    distribute-lft-out-- [=>]99.8

    \[ 3 \cdot \left(\color{blue}{x \cdot \left(x \cdot 3 - 4\right)} + 1\right) \]

    fma-def [=>]99.8

    \[ 3 \cdot \color{blue}{\mathsf{fma}\left(x, x \cdot 3 - 4, 1\right)} \]

    *-commutative [=>]99.8

    \[ 3 \cdot \mathsf{fma}\left(x, \color{blue}{3 \cdot x} - 4, 1\right) \]

    fma-neg [=>]99.8

    \[ 3 \cdot \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(3, x, -4\right)}, 1\right) \]

    metadata-eval [=>]99.8

    \[ 3 \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(3, x, \color{blue}{-4}\right), 1\right) \]
  3. Taylor expanded in x around 0 99.9%

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

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

    [Start]99.9

    \[ -12 \cdot x + \left(3 + 9 \cdot {x}^{2}\right) \]

    associate-+r+ [=>]99.9

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

    fma-def [=>]99.9

    \[ \color{blue}{\mathsf{fma}\left(-12, x, 3\right)} + 9 \cdot {x}^{2} \]

    *-commutative [=>]99.9

    \[ \mathsf{fma}\left(-12, x, 3\right) + \color{blue}{{x}^{2} \cdot 9} \]

    unpow2 [=>]99.9

    \[ \mathsf{fma}\left(-12, x, 3\right) + \color{blue}{\left(x \cdot x\right)} \cdot 9 \]
  5. Applied egg-rr99.9%

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

    [Start]99.9

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

    +-commutative [=>]99.9

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

    associate-*l* [=>]99.9

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

    fma-def [=>]99.9

    \[ \color{blue}{\mathsf{fma}\left(x, x \cdot 9, \mathsf{fma}\left(-12, x, 3\right)\right)} \]
  6. Final simplification99.9%

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

Alternatives

Alternative 1
Accuracy98.4%
Cost713
\[\begin{array}{l} \mathbf{if}\;x \leq -0.58 \lor \neg \left(x \leq 0.58\right):\\ \;\;\;\;x \cdot \left(x \cdot 9 + -12\right)\\ \mathbf{else}:\\ \;\;\;\;3 + x \cdot -12\\ \end{array} \]
Alternative 2
Accuracy96.8%
Cost585
\[\begin{array}{l} \mathbf{if}\;x \leq -0.58 \lor \neg \left(x \leq 0.2\right):\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \mathbf{else}:\\ \;\;\;\;3\\ \end{array} \]
Alternative 3
Accuracy97.6%
Cost585
\[\begin{array}{l} \mathbf{if}\;x \leq -1.55 \lor \neg \left(x \leq 1\right):\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \mathbf{else}:\\ \;\;\;\;3 + x \cdot -12\\ \end{array} \]
Alternative 4
Accuracy99.9%
Cost576
\[3 + x \cdot \left(x \cdot 9 + -12\right) \]
Alternative 5
Accuracy67.2%
Cost64
\[3 \]

Error

Reproduce?

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