Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D

Percentage Accurate: 99.8% → 99.9%
Time: 9.0s
Alternatives: 7
Speedup: 2.1×

Specification

?
\[\begin{array}{l} \\ 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \end{array} \]
(FPCore (x) :precision binary64 (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))
double code(double x) {
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 3.0d0 * ((((x * 3.0d0) * x) - (x * 4.0d0)) + 1.0d0)
end function
public static double code(double x) {
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
def code(x):
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0)
function code(x)
	return Float64(3.0 * Float64(Float64(Float64(Float64(x * 3.0) * x) - Float64(x * 4.0)) + 1.0))
end
function tmp = code(x)
	tmp = 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.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]
\begin{array}{l}

\\
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 7 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \end{array} \]
(FPCore (x) :precision binary64 (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))
double code(double x) {
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 3.0d0 * ((((x * 3.0d0) * x) - (x * 4.0d0)) + 1.0d0)
end function
public static double code(double x) {
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0);
}
def code(x):
	return 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.0)
function code(x)
	return Float64(3.0 * Float64(Float64(Float64(Float64(x * 3.0) * x) - Float64(x * 4.0)) + 1.0))
end
function tmp = code(x)
	tmp = 3.0 * ((((x * 3.0) * x) - (x * 4.0)) + 1.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]
\begin{array}{l}

\\
3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right)
\end{array}

Alternative 1: 99.9% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(x, 9, -12\right), x, 3\right) \end{array} \]
(FPCore (x) :precision binary64 (fma (fma x 9.0 -12.0) x 3.0))
double code(double x) {
	return fma(fma(x, 9.0, -12.0), x, 3.0);
}
function code(x)
	return fma(fma(x, 9.0, -12.0), x, 3.0)
end
code[x_] := N[(N[(x * 9.0 + -12.0), $MachinePrecision] * x + 3.0), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\mathsf{fma}\left(x, 9, -12\right), x, 3\right)
\end{array}
Derivation
  1. Initial program 99.8%

    \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right)} \cdot x - x \cdot 4\right) + 1\right) \]
    2. lift-*.f64N/A

      \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) + 1\right) \]
    3. lift-*.f64N/A

      \[\leadsto 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - \color{blue}{x \cdot 4}\right) + 1\right) \]
    4. lift--.f64N/A

      \[\leadsto 3 \cdot \left(\color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} + 1\right) \]
    5. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 1 \cdot 3} \]
    6. lift--.f64N/A

      \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} \cdot 3 + 1 \cdot 3 \]
    7. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
    8. *-commutativeN/A

      \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
    9. lift-*.f64N/A

      \[\leadsto \left(x \cdot \left(x \cdot 3\right) - \color{blue}{x \cdot 4}\right) \cdot 3 + 1 \cdot 3 \]
    10. distribute-lft-out--N/A

      \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 1 \cdot 3 \]
    11. associate-*l*N/A

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 1 \cdot 3 \]
    12. metadata-evalN/A

      \[\leadsto x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right) + \color{blue}{3} \]
    13. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
    14. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 - 4\right) \cdot 3}, 3\right) \]
    15. sub-negN/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 + \left(\mathsf{neg}\left(4\right)\right)\right)} \cdot 3, 3\right) \]
    16. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot 3} + \left(\mathsf{neg}\left(4\right)\right)\right) \cdot 3, 3\right) \]
    17. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3, \mathsf{neg}\left(4\right)\right)} \cdot 3, 3\right) \]
    18. metadata-eval99.9

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, -4\right) \cdot 3, 3\right)} \]
  5. Step-by-step derivation
    1. lift-fma.f64N/A

      \[\leadsto x \cdot \left(\color{blue}{\mathsf{fma}\left(x, 3, -4\right)} \cdot 3\right) + 3 \]
    2. *-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(3 \cdot \mathsf{fma}\left(x, 3, -4\right)\right)} + 3 \]
    3. associate-*l*N/A

      \[\leadsto \color{blue}{\left(x \cdot 3\right) \cdot \mathsf{fma}\left(x, 3, -4\right)} + 3 \]
    4. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot \mathsf{fma}\left(x, 3, -4\right) + 3 \]
    5. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot 3\right)} \cdot \mathsf{fma}\left(x, 3, -4\right) + 3 \]
    6. associate-*l*N/A

      \[\leadsto \color{blue}{x \cdot \left(3 \cdot \mathsf{fma}\left(x, 3, -4\right)\right)} + 3 \]
    7. *-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(\mathsf{fma}\left(x, 3, -4\right) \cdot 3\right)} + 3 \]
    8. lift-*.f64N/A

      \[\leadsto x \cdot \color{blue}{\left(\mathsf{fma}\left(x, 3, -4\right) \cdot 3\right)} + 3 \]
    9. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\mathsf{fma}\left(x, 3, -4\right) \cdot 3\right) \cdot x} + 3 \]
    10. lower-fma.f6499.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(x, 3, -4\right) \cdot 3, x, 3\right)} \]
    11. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, 3, -4\right) \cdot 3}, x, 3\right) \]
    12. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{3 \cdot \mathsf{fma}\left(x, 3, -4\right)}, x, 3\right) \]
    13. lift-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(3 \cdot \color{blue}{\left(x \cdot 3 + -4\right)}, x, 3\right) \]
    14. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(3 \cdot \left(\color{blue}{x \cdot 3} + -4\right), x, 3\right) \]
    15. distribute-lft-inN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{3 \cdot \left(x \cdot 3\right) + 3 \cdot -4}, x, 3\right) \]
    16. lift-*.f64N/A

      \[\leadsto \mathsf{fma}\left(3 \cdot \color{blue}{\left(x \cdot 3\right)} + 3 \cdot -4, x, 3\right) \]
    17. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(3 \cdot \color{blue}{\left(3 \cdot x\right)} + 3 \cdot -4, x, 3\right) \]
    18. associate-*r*N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(3 \cdot 3\right) \cdot x} + 3 \cdot -4, x, 3\right) \]
    19. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{9} \cdot x + 3 \cdot -4, x, 3\right) \]
    20. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot 9} + 3 \cdot -4, x, 3\right) \]
    21. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, 9, 3 \cdot -4\right)}, x, 3\right) \]
    22. metadata-eval99.9

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(x, 9, -12\right), x, 3\right)} \]
  7. Add Preprocessing

Alternative 2: 98.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \mathsf{fma}\left(9, x, -12\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 0.0005)
   (fma x -12.0 3.0)
   (* x (fma 9.0 x -12.0))))
double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = fma(x, -12.0, 3.0);
	} else {
		tmp = x * fma(9.0, x, -12.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 0.0005)
		tmp = fma(x, -12.0, 3.0);
	else
		tmp = Float64(x * fma(9.0, x, -12.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.0005], N[(x * -12.0 + 3.0), $MachinePrecision], N[(x * N[(9.0 * x + -12.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \mathsf{fma}\left(9, x, -12\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 + -12 \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{-12 \cdot x + 3} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot -12} + 3 \]
      3. metadata-evalN/A

        \[\leadsto x \cdot \color{blue}{\left(\mathsf{neg}\left(12\right)\right)} + 3 \]
      4. metadata-evalN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{12 \cdot 1}\right)\right) + 3 \]
      5. lft-mult-inverseN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(12 \cdot \color{blue}{\left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right)\right) + 3 \]
      6. associate-*l*N/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}}\right)\right) + 3 \]
      7. distribute-lft-neg-inN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}\right)} + 3 \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}, 3\right)} \]
      9. distribute-lft-neg-inN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{neg}\left(\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}\right)}, 3\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12 \cdot \left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right), 3\right) \]
      11. lft-mult-inverseN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(12 \cdot \color{blue}{1}\right), 3\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12}\right), 3\right) \]
      13. metadata-eval98.7

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{-12}, 3\right) \]
    5. Simplified98.7%

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

    if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64)))

    1. Initial program 99.6%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right)} \cdot x - x \cdot 4\right) + 1\right) \]
      2. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) + 1\right) \]
      3. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - \color{blue}{x \cdot 4}\right) + 1\right) \]
      4. lift--.f64N/A

        \[\leadsto 3 \cdot \left(\color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} + 1\right) \]
      5. distribute-rgt-inN/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 1 \cdot 3} \]
      6. lift--.f64N/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} \cdot 3 + 1 \cdot 3 \]
      7. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      8. *-commutativeN/A

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      9. lift-*.f64N/A

        \[\leadsto \left(x \cdot \left(x \cdot 3\right) - \color{blue}{x \cdot 4}\right) \cdot 3 + 1 \cdot 3 \]
      10. distribute-lft-out--N/A

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 1 \cdot 3 \]
      11. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 1 \cdot 3 \]
      12. metadata-evalN/A

        \[\leadsto x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right) + \color{blue}{3} \]
      13. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      14. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 - 4\right) \cdot 3}, 3\right) \]
      15. sub-negN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 + \left(\mathsf{neg}\left(4\right)\right)\right)} \cdot 3, 3\right) \]
      16. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot 3} + \left(\mathsf{neg}\left(4\right)\right)\right) \cdot 3, 3\right) \]
      17. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3, \mathsf{neg}\left(4\right)\right)} \cdot 3, 3\right) \]
      18. metadata-eval99.8

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, \color{blue}{-4}\right) \cdot 3, 3\right) \]
    4. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, -4\right) \cdot 3, 3\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2} \cdot \left(9 - 12 \cdot \frac{1}{x}\right)} \]
    6. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(9 - 12 \cdot \frac{1}{x}\right) \]
      2. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(x \cdot \left(9 - 12 \cdot \frac{1}{x}\right)\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{x \cdot \left(x \cdot \left(9 - 12 \cdot \frac{1}{x}\right)\right)} \]
      4. sub-negN/A

        \[\leadsto x \cdot \left(x \cdot \color{blue}{\left(9 + \left(\mathsf{neg}\left(12 \cdot \frac{1}{x}\right)\right)\right)}\right) \]
      5. distribute-rgt-inN/A

        \[\leadsto x \cdot \color{blue}{\left(9 \cdot x + \left(\mathsf{neg}\left(12 \cdot \frac{1}{x}\right)\right) \cdot x\right)} \]
      6. distribute-lft-neg-inN/A

        \[\leadsto x \cdot \left(9 \cdot x + \color{blue}{\left(\left(\mathsf{neg}\left(12\right)\right) \cdot \frac{1}{x}\right)} \cdot x\right) \]
      7. metadata-evalN/A

        \[\leadsto x \cdot \left(9 \cdot x + \left(\color{blue}{-12} \cdot \frac{1}{x}\right) \cdot x\right) \]
      8. associate-*l*N/A

        \[\leadsto x \cdot \left(9 \cdot x + \color{blue}{-12 \cdot \left(\frac{1}{x} \cdot x\right)}\right) \]
      9. lft-mult-inverseN/A

        \[\leadsto x \cdot \left(9 \cdot x + -12 \cdot \color{blue}{1}\right) \]
      10. metadata-evalN/A

        \[\leadsto x \cdot \left(9 \cdot x + \color{blue}{-12}\right) \]
      11. lower-fma.f6499.0

        \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(9, x, -12\right)} \]
    7. Simplified99.0%

      \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(9, x, -12\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \mathsf{fma}\left(9, x, -12\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 98.3% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 0.0005)
   (fma x -12.0 3.0)
   (* x (* x 9.0))))
double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = fma(x, -12.0, 3.0);
	} else {
		tmp = x * (x * 9.0);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 0.0005)
		tmp = fma(x, -12.0, 3.0);
	else
		tmp = Float64(x * Float64(x * 9.0));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.0005], N[(x * -12.0 + 3.0), $MachinePrecision], N[(x * N[(x * 9.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot \left(x \cdot 9\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 + -12 \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{-12 \cdot x + 3} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot -12} + 3 \]
      3. metadata-evalN/A

        \[\leadsto x \cdot \color{blue}{\left(\mathsf{neg}\left(12\right)\right)} + 3 \]
      4. metadata-evalN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{12 \cdot 1}\right)\right) + 3 \]
      5. lft-mult-inverseN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(12 \cdot \color{blue}{\left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right)\right) + 3 \]
      6. associate-*l*N/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}}\right)\right) + 3 \]
      7. distribute-lft-neg-inN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}\right)} + 3 \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}, 3\right)} \]
      9. distribute-lft-neg-inN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{neg}\left(\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}\right)}, 3\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12 \cdot \left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right), 3\right) \]
      11. lft-mult-inverseN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(12 \cdot \color{blue}{1}\right), 3\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12}\right), 3\right) \]
      13. metadata-eval98.7

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{-12}, 3\right) \]
    5. Simplified98.7%

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

    if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64)))

    1. Initial program 99.6%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right)} \cdot x - x \cdot 4\right) + 1\right) \]
      2. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) + 1\right) \]
      3. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - \color{blue}{x \cdot 4}\right) + 1\right) \]
      4. lift--.f64N/A

        \[\leadsto 3 \cdot \left(\color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} + 1\right) \]
      5. distribute-rgt-inN/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 1 \cdot 3} \]
      6. lift--.f64N/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} \cdot 3 + 1 \cdot 3 \]
      7. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      8. *-commutativeN/A

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      9. lift-*.f64N/A

        \[\leadsto \left(x \cdot \left(x \cdot 3\right) - \color{blue}{x \cdot 4}\right) \cdot 3 + 1 \cdot 3 \]
      10. distribute-lft-out--N/A

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 1 \cdot 3 \]
      11. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 1 \cdot 3 \]
      12. metadata-evalN/A

        \[\leadsto x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right) + \color{blue}{3} \]
      13. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      14. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 - 4\right) \cdot 3}, 3\right) \]
      15. sub-negN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 + \left(\mathsf{neg}\left(4\right)\right)\right)} \cdot 3, 3\right) \]
      16. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot 3} + \left(\mathsf{neg}\left(4\right)\right)\right) \cdot 3, 3\right) \]
      17. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3, \mathsf{neg}\left(4\right)\right)} \cdot 3, 3\right) \]
      18. metadata-eval99.8

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, \color{blue}{-4}\right) \cdot 3, 3\right) \]
    4. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, -4\right) \cdot 3, 3\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
      2. unpow2N/A

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
      3. lower-*.f6496.6

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
    7. Simplified96.6%

      \[\leadsto \color{blue}{9 \cdot \left(x \cdot x\right)} \]
    8. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \color{blue}{\left(9 \cdot x\right) \cdot x} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\left(x \cdot 9\right)} \cdot x \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(x \cdot 9\right) \cdot x} \]
      4. lower-*.f6496.7

        \[\leadsto \color{blue}{\left(x \cdot 9\right)} \cdot x \]
    9. Applied egg-rr96.7%

      \[\leadsto \color{blue}{\left(x \cdot 9\right) \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(x \cdot 9\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 98.3% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 0.0005)
   (fma x -12.0 3.0)
   (* 9.0 (* x x))))
double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = fma(x, -12.0, 3.0);
	} else {
		tmp = 9.0 * (x * x);
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 0.0005)
		tmp = fma(x, -12.0, 3.0);
	else
		tmp = Float64(9.0 * Float64(x * x));
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.0005], N[(x * -12.0 + 3.0), $MachinePrecision], N[(9.0 * N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\

\mathbf{else}:\\
\;\;\;\;9 \cdot \left(x \cdot x\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 + -12 \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{-12 \cdot x + 3} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot -12} + 3 \]
      3. metadata-evalN/A

        \[\leadsto x \cdot \color{blue}{\left(\mathsf{neg}\left(12\right)\right)} + 3 \]
      4. metadata-evalN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{12 \cdot 1}\right)\right) + 3 \]
      5. lft-mult-inverseN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(12 \cdot \color{blue}{\left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right)\right) + 3 \]
      6. associate-*l*N/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}}\right)\right) + 3 \]
      7. distribute-lft-neg-inN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}\right)} + 3 \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}, 3\right)} \]
      9. distribute-lft-neg-inN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{neg}\left(\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}\right)}, 3\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12 \cdot \left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right), 3\right) \]
      11. lft-mult-inverseN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(12 \cdot \color{blue}{1}\right), 3\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12}\right), 3\right) \]
      13. metadata-eval98.7

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{-12}, 3\right) \]
    5. Simplified98.7%

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

    if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64)))

    1. Initial program 99.6%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right)} \cdot x - x \cdot 4\right) + 1\right) \]
      2. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) + 1\right) \]
      3. lift-*.f64N/A

        \[\leadsto 3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - \color{blue}{x \cdot 4}\right) + 1\right) \]
      4. lift--.f64N/A

        \[\leadsto 3 \cdot \left(\color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} + 1\right) \]
      5. distribute-rgt-inN/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) \cdot 3 + 1 \cdot 3} \]
      6. lift--.f64N/A

        \[\leadsto \color{blue}{\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right)} \cdot 3 + 1 \cdot 3 \]
      7. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{\left(x \cdot 3\right) \cdot x} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      8. *-commutativeN/A

        \[\leadsto \left(\color{blue}{x \cdot \left(x \cdot 3\right)} - x \cdot 4\right) \cdot 3 + 1 \cdot 3 \]
      9. lift-*.f64N/A

        \[\leadsto \left(x \cdot \left(x \cdot 3\right) - \color{blue}{x \cdot 4}\right) \cdot 3 + 1 \cdot 3 \]
      10. distribute-lft-out--N/A

        \[\leadsto \color{blue}{\left(x \cdot \left(x \cdot 3 - 4\right)\right)} \cdot 3 + 1 \cdot 3 \]
      11. associate-*l*N/A

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right)} + 1 \cdot 3 \]
      12. metadata-evalN/A

        \[\leadsto x \cdot \left(\left(x \cdot 3 - 4\right) \cdot 3\right) + \color{blue}{3} \]
      13. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(x \cdot 3 - 4\right) \cdot 3, 3\right)} \]
      14. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 - 4\right) \cdot 3}, 3\right) \]
      15. sub-negN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(x \cdot 3 + \left(\mathsf{neg}\left(4\right)\right)\right)} \cdot 3, 3\right) \]
      16. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot 3} + \left(\mathsf{neg}\left(4\right)\right)\right) \cdot 3, 3\right) \]
      17. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 3, \mathsf{neg}\left(4\right)\right)} \cdot 3, 3\right) \]
      18. metadata-eval99.8

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, \color{blue}{-4}\right) \cdot 3, 3\right) \]
    4. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 3, -4\right) \cdot 3, 3\right)} \]
    5. Taylor expanded in x around inf

      \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{9 \cdot {x}^{2}} \]
      2. unpow2N/A

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
      3. lower-*.f6496.6

        \[\leadsto 9 \cdot \color{blue}{\left(x \cdot x\right)} \]
    7. Simplified96.6%

      \[\leadsto \color{blue}{9 \cdot \left(x \cdot x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;9 \cdot \left(x \cdot x\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 78.5% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 0.0005) (fma x -12.0 3.0) (* x x)))
double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = fma(x, -12.0, 3.0);
	} else {
		tmp = x * x;
	}
	return tmp;
}
function code(x)
	tmp = 0.0
	if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 0.0005)
		tmp = fma(x, -12.0, 3.0);
	else
		tmp = Float64(x * x);
	end
	return tmp
end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.0005], N[(x * -12.0 + 3.0), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3 + -12 \cdot x} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{-12 \cdot x + 3} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot -12} + 3 \]
      3. metadata-evalN/A

        \[\leadsto x \cdot \color{blue}{\left(\mathsf{neg}\left(12\right)\right)} + 3 \]
      4. metadata-evalN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{12 \cdot 1}\right)\right) + 3 \]
      5. lft-mult-inverseN/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(12 \cdot \color{blue}{\left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right)\right) + 3 \]
      6. associate-*l*N/A

        \[\leadsto x \cdot \left(\mathsf{neg}\left(\color{blue}{\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}}\right)\right) + 3 \]
      7. distribute-lft-neg-inN/A

        \[\leadsto x \cdot \color{blue}{\left(\left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}\right)} + 3 \]
      8. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\mathsf{neg}\left(12 \cdot \frac{1}{{x}^{2}}\right)\right) \cdot {x}^{2}, 3\right)} \]
      9. distribute-lft-neg-inN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{neg}\left(\left(12 \cdot \frac{1}{{x}^{2}}\right) \cdot {x}^{2}\right)}, 3\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12 \cdot \left(\frac{1}{{x}^{2}} \cdot {x}^{2}\right)}\right), 3\right) \]
      11. lft-mult-inverseN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(12 \cdot \color{blue}{1}\right), 3\right) \]
      12. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(x, \mathsf{neg}\left(\color{blue}{12}\right), 3\right) \]
      13. metadata-eval98.7

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{-12}, 3\right) \]
    5. Simplified98.7%

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

    if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64)))

    1. Initial program 99.6%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{9 \cdot {x}^{3}} \]
    4. Step-by-step derivation
      1. unpow3N/A

        \[\leadsto 9 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)} \]
      2. unpow2N/A

        \[\leadsto 9 \cdot \left(\color{blue}{{x}^{2}} \cdot x\right) \]
      3. associate-*r*N/A

        \[\leadsto \color{blue}{\left(9 \cdot {x}^{2}\right) \cdot x} \]
      4. *-commutativeN/A

        \[\leadsto \color{blue}{x \cdot \left(9 \cdot {x}^{2}\right)} \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{x \cdot \left(9 \cdot {x}^{2}\right)} \]
      6. *-commutativeN/A

        \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot 9\right)} \]
      7. lower-*.f64N/A

        \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot 9\right)} \]
      8. unpow2N/A

        \[\leadsto x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot 9\right) \]
      9. lower-*.f6430.5

        \[\leadsto x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot 9\right) \]
    5. Simplified30.5%

      \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot x\right) \cdot 9\right)} \]
    6. Taylor expanded in x around inf

      \[\leadsto \color{blue}{{x}^{2}} \]
    7. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \color{blue}{x \cdot x} \]
      2. lower-*.f6462.7

        \[\leadsto \color{blue}{x \cdot x} \]
    8. Simplified62.7%

      \[\leadsto \color{blue}{x \cdot x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification81.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(x, -12, 3\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 78.1% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (* x (* x 3.0)) (* x 4.0)) 0.0005) 3.0 (* x x)))
double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = 3.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (((x * (x * 3.0d0)) - (x * 4.0d0)) <= 0.0005d0) then
        tmp = 3.0d0
    else
        tmp = x * x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005) {
		tmp = 3.0;
	} else {
		tmp = x * x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if ((x * (x * 3.0)) - (x * 4.0)) <= 0.0005:
		tmp = 3.0
	else:
		tmp = x * x
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(Float64(x * Float64(x * 3.0)) - Float64(x * 4.0)) <= 0.0005)
		tmp = 3.0;
	else
		tmp = Float64(x * x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (((x * (x * 3.0)) - (x * 4.0)) <= 0.0005)
		tmp = 3.0;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[(x * N[(x * 3.0), $MachinePrecision]), $MachinePrecision] - N[(x * 4.0), $MachinePrecision]), $MachinePrecision], 0.0005], 3.0, N[(x * x), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\
\;\;\;\;3\\

\mathbf{else}:\\
\;\;\;\;x \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64))) < 5.0000000000000001e-4

    1. Initial program 100.0%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3} \]
    4. Step-by-step derivation
      1. Simplified97.1%

        \[\leadsto \color{blue}{3} \]

      if 5.0000000000000001e-4 < (-.f64 (*.f64 (*.f64 x #s(literal 3 binary64)) x) (*.f64 x #s(literal 4 binary64)))

      1. Initial program 99.6%

        \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
      2. Add Preprocessing
      3. Taylor expanded in x around inf

        \[\leadsto \color{blue}{9 \cdot {x}^{3}} \]
      4. Step-by-step derivation
        1. unpow3N/A

          \[\leadsto 9 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)} \]
        2. unpow2N/A

          \[\leadsto 9 \cdot \left(\color{blue}{{x}^{2}} \cdot x\right) \]
        3. associate-*r*N/A

          \[\leadsto \color{blue}{\left(9 \cdot {x}^{2}\right) \cdot x} \]
        4. *-commutativeN/A

          \[\leadsto \color{blue}{x \cdot \left(9 \cdot {x}^{2}\right)} \]
        5. lower-*.f64N/A

          \[\leadsto \color{blue}{x \cdot \left(9 \cdot {x}^{2}\right)} \]
        6. *-commutativeN/A

          \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot 9\right)} \]
        7. lower-*.f64N/A

          \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot 9\right)} \]
        8. unpow2N/A

          \[\leadsto x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot 9\right) \]
        9. lower-*.f6430.5

          \[\leadsto x \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot 9\right) \]
      5. Simplified30.5%

        \[\leadsto \color{blue}{x \cdot \left(\left(x \cdot x\right) \cdot 9\right)} \]
      6. Taylor expanded in x around inf

        \[\leadsto \color{blue}{{x}^{2}} \]
      7. Step-by-step derivation
        1. unpow2N/A

          \[\leadsto \color{blue}{x \cdot x} \]
        2. lower-*.f6462.7

          \[\leadsto \color{blue}{x \cdot x} \]
      8. Simplified62.7%

        \[\leadsto \color{blue}{x \cdot x} \]
    5. Recombined 2 regimes into one program.
    6. Final simplification80.2%

      \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot \left(x \cdot 3\right) - x \cdot 4 \leq 0.0005:\\ \;\;\;\;3\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
    7. Add Preprocessing

    Alternative 7: 50.7% accurate, 27.0× speedup?

    \[\begin{array}{l} \\ 3 \end{array} \]
    (FPCore (x) :precision binary64 3.0)
    double code(double x) {
    	return 3.0;
    }
    
    real(8) function code(x)
        real(8), intent (in) :: x
        code = 3.0d0
    end function
    
    public static double code(double x) {
    	return 3.0;
    }
    
    def code(x):
    	return 3.0
    
    function code(x)
    	return 3.0
    end
    
    function tmp = code(x)
    	tmp = 3.0;
    end
    
    code[x_] := 3.0
    
    \begin{array}{l}
    
    \\
    3
    \end{array}
    
    Derivation
    1. Initial program 99.8%

      \[3 \cdot \left(\left(\left(x \cdot 3\right) \cdot x - x \cdot 4\right) + 1\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{3} \]
    4. Step-by-step derivation
      1. Simplified51.4%

        \[\leadsto \color{blue}{3} \]
      2. Add Preprocessing

      Developer Target 1: 99.8% accurate, 1.2× speedup?

      \[\begin{array}{l} \\ 3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right) \end{array} \]
      (FPCore (x) :precision binary64 (+ 3.0 (- (* (* 9.0 x) x) (* 12.0 x))))
      double code(double x) {
      	return 3.0 + (((9.0 * x) * x) - (12.0 * x));
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 3.0d0 + (((9.0d0 * x) * x) - (12.0d0 * x))
      end function
      
      public static double code(double x) {
      	return 3.0 + (((9.0 * x) * x) - (12.0 * x));
      }
      
      def code(x):
      	return 3.0 + (((9.0 * x) * x) - (12.0 * x))
      
      function code(x)
      	return Float64(3.0 + Float64(Float64(Float64(9.0 * x) * x) - Float64(12.0 * x)))
      end
      
      function tmp = code(x)
      	tmp = 3.0 + (((9.0 * x) * x) - (12.0 * x));
      end
      
      code[x_] := N[(3.0 + N[(N[(N[(9.0 * x), $MachinePrecision] * x), $MachinePrecision] - N[(12.0 * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      3 + \left(\left(9 \cdot x\right) \cdot x - 12 \cdot x\right)
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024214 
      (FPCore (x)
        :name "Diagrams.Tangent:$catParam from diagrams-lib-1.3.0.3, D"
        :precision binary64
      
        :alt
        (! :herbie-platform default (+ 3 (- (* (* 9 x) x) (* 12 x))))
      
        (* 3.0 (+ (- (* (* x 3.0) x) (* x 4.0)) 1.0)))