invcot (example 3.9)

Percentage Accurate: 6.5% → 99.5%
Time: 19.0s
Alternatives: 7
Speedup: 21.0×

Specification

?
\[-0.026 < x \land x < 0.026\]
\[\begin{array}{l} \\ \frac{1}{x} - \frac{1}{\tan x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
	return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x):
	return (1.0 / x) - (1.0 / math.tan(x))
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function tmp = code(x)
	tmp = (1.0 / x) - (1.0 / tan(x));
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x} - \frac{1}{\tan x}
\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: 6.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{x} - \frac{1}{\tan x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
	return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x):
	return (1.0 / x) - (1.0 / math.tan(x))
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function tmp = code(x)
	tmp = (1.0 / x) - (1.0 / tan(x));
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}

Alternative 1: 99.5% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right)\\ \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), t\_0 \cdot 1.0973936899862826 \cdot 10^{-5}, 0.037037037037037035\right) \cdot \frac{x}{\mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), -0.007407407407407408, \mathsf{fma}\left(0.0004938271604938272, t\_0, 0.1111111111111111\right)\right)} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fma (fma x x 0.0) (fma x x 0.0) 0.0)))
   (*
    (fma (fma x x 0.0) (* t_0 1.0973936899862826e-5) 0.037037037037037035)
    (/
     x
     (fma
      (fma x x 0.0)
      -0.007407407407407408
      (fma 0.0004938271604938272 t_0 0.1111111111111111))))))
double code(double x) {
	double t_0 = fma(fma(x, x, 0.0), fma(x, x, 0.0), 0.0);
	return fma(fma(x, x, 0.0), (t_0 * 1.0973936899862826e-5), 0.037037037037037035) * (x / fma(fma(x, x, 0.0), -0.007407407407407408, fma(0.0004938271604938272, t_0, 0.1111111111111111)));
}
function code(x)
	t_0 = fma(fma(x, x, 0.0), fma(x, x, 0.0), 0.0)
	return Float64(fma(fma(x, x, 0.0), Float64(t_0 * 1.0973936899862826e-5), 0.037037037037037035) * Float64(x / fma(fma(x, x, 0.0), -0.007407407407407408, fma(0.0004938271604938272, t_0, 0.1111111111111111))))
end
code[x_] := Block[{t$95$0 = N[(N[(x * x + 0.0), $MachinePrecision] * N[(x * x + 0.0), $MachinePrecision] + 0.0), $MachinePrecision]}, N[(N[(N[(x * x + 0.0), $MachinePrecision] * N[(t$95$0 * 1.0973936899862826e-5), $MachinePrecision] + 0.037037037037037035), $MachinePrecision] * N[(x / N[(N[(x * x + 0.0), $MachinePrecision] * -0.007407407407407408 + N[(0.0004938271604938272 * t$95$0 + 0.1111111111111111), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right)\\
\mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), t\_0 \cdot 1.0973936899862826 \cdot 10^{-5}, 0.037037037037037035\right) \cdot \frac{x}{\mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), -0.007407407407407408, \mathsf{fma}\left(0.0004938271604938272, t\_0, 0.1111111111111111\right)\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(\frac{1}{45} \cdot {x}^{2} + \frac{1}{3}\right)} \]
    3. unpow2N/A

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

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

      \[\leadsto x \cdot \left(\color{blue}{x \cdot \left(\frac{1}{45} \cdot x\right)} + \frac{1}{3}\right) \]
    6. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(x, \frac{1}{45} \cdot x, \frac{1}{3}\right)} \]
    7. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{\frac{1}{45} \cdot x + 0}, \frac{1}{3}\right) \]
    8. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{1}{45}} + 0, \frac{1}{3}\right) \]
    9. accelerator-lowering-fma.f6499.3

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.022222222222222223, 0\right)}, 0.3333333333333333\right) \]
  5. Simplified99.3%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.022222222222222223, 0\right), 0.3333333333333333\right)} \]
  6. Applied egg-rr99.5%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(1.0973936899862826 \cdot 10^{-5}, \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right), 0\right), 0.037037037037037035\right) \cdot x}{\mathsf{fma}\left(0.0004938271604938272, \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right), 0.1111111111111111\right) - 0.3333333333333333 \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.022222222222222223, 0\right), 0\right)}} \]
  7. Applied egg-rr99.5%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right) \cdot 1.0973936899862826 \cdot 10^{-5}, 0.037037037037037035\right) \cdot \frac{x}{\mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), -0.007407407407407408, \mathsf{fma}\left(0.0004938271604938272, \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x, 0\right), 0\right), 0.1111111111111111\right)\right)}} \]
  8. Add Preprocessing

Alternative 2: 99.5% accurate, 3.2× speedup?

\[\begin{array}{l} \\ x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, 0.00021164021164021165, 0.0021164021164021165\right), 0.022222222222222223\right), 0.3333333333333333\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  x
  (fma
   (* x x)
   (fma
    (* x x)
    (fma (* x x) 0.00021164021164021165 0.0021164021164021165)
    0.022222222222222223)
   0.3333333333333333)))
double code(double x) {
	return x * fma((x * x), fma((x * x), fma((x * x), 0.00021164021164021165, 0.0021164021164021165), 0.022222222222222223), 0.3333333333333333);
}
function code(x)
	return Float64(x * fma(Float64(x * x), fma(Float64(x * x), fma(Float64(x * x), 0.00021164021164021165, 0.0021164021164021165), 0.022222222222222223), 0.3333333333333333))
end
code[x_] := N[(x * N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * 0.00021164021164021165 + 0.0021164021164021165), $MachinePrecision] + 0.022222222222222223), $MachinePrecision] + 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, 0.00021164021164021165, 0.0021164021164021165\right), 0.022222222222222223\right), 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right)\right)\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right)\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right)\right) + \frac{1}{3}\right)} \]
    3. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right), \frac{1}{3}\right)} \]
  5. Simplified99.5%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, x \cdot 0.00021164021164021165, 0.0021164021164021165\right), 0.022222222222222223\right), 0.3333333333333333\right)} \]
  6. Taylor expanded in x around 0

    \[\leadsto x \cdot \color{blue}{\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right)\right)\right)} \]
  7. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right)\right) + \frac{1}{3}\right)} \]
    2. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right), \frac{1}{3}\right)} \]
    3. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right), \frac{1}{3}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45} + {x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right), \frac{1}{3}\right) \]
    5. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{{x}^{2} \cdot \left(\frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}\right) + \frac{1}{45}}, \frac{1}{3}\right) \]
    6. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}, \frac{1}{45}\right)}, \frac{1}{3}\right) \]
    7. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    8. *-lowering-*.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{2}{945} + \frac{1}{4725} \cdot {x}^{2}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    9. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \color{blue}{\frac{1}{4725} \cdot {x}^{2} + \frac{2}{945}}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    10. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \color{blue}{{x}^{2} \cdot \frac{1}{4725}} + \frac{2}{945}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    11. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{4725}, \frac{2}{945}\right)}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    12. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{4725}, \frac{2}{945}\right), \frac{1}{45}\right), \frac{1}{3}\right) \]
    13. *-lowering-*.f6499.5

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.00021164021164021165, 0.0021164021164021165\right), 0.022222222222222223\right), 0.3333333333333333\right) \]
  8. Simplified99.5%

    \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, 0.00021164021164021165, 0.0021164021164021165\right), 0.022222222222222223\right), 0.3333333333333333\right)} \]
  9. Add Preprocessing

Alternative 3: 99.5% accurate, 3.8× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), x, x \cdot 0.3333333333333333\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (fma
  (* (* x x) (fma x (* x 0.0021164021164021165) 0.022222222222222223))
  x
  (* x 0.3333333333333333)))
double code(double x) {
	return fma(((x * x) * fma(x, (x * 0.0021164021164021165), 0.022222222222222223)), x, (x * 0.3333333333333333));
}
function code(x)
	return fma(Float64(Float64(x * x) * fma(x, Float64(x * 0.0021164021164021165), 0.022222222222222223)), x, Float64(x * 0.3333333333333333))
end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * 0.0021164021164021165), $MachinePrecision] + 0.022222222222222223), $MachinePrecision]), $MachinePrecision] * x + N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), x, x \cdot 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right) + \frac{1}{3}\right)} \]
    3. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right)} \]
    4. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    5. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{neg}\left(\color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) + 0\right)}\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    6. distribute-neg-inN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)\right) + \left(\mathsf{neg}\left(0\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    7. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{{x}^{2}} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    8. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    9. metadata-evalN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x + \color{blue}{0}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    10. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, x, 0\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    11. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\frac{2}{945} \cdot {x}^{2} + \frac{1}{45}}, \frac{1}{3}\right) \]
    12. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \frac{2}{945} \cdot \color{blue}{\left(x \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    13. associate-*r*N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\left(\frac{2}{945} \cdot x\right) \cdot x} + \frac{1}{45}, \frac{1}{3}\right) \]
    14. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{x \cdot \left(\frac{2}{945} \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    15. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\mathsf{fma}\left(x, \frac{2}{945} \cdot x, \frac{1}{45}\right)}, \frac{1}{3}\right) \]
    16. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\frac{2}{945} \cdot x + 0}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    17. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{2}{945}} + 0, \frac{1}{45}\right), \frac{1}{3}\right) \]
    18. accelerator-lowering-fma.f6499.4

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.0021164021164021165, 0\right)}, 0.022222222222222223\right), 0.3333333333333333\right) \]
  5. Simplified99.4%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.0021164021164021165, 0\right), 0.022222222222222223\right), 0.3333333333333333\right)} \]
  6. Step-by-step derivation
    1. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left(\left(x \cdot x + 0\right) \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right) \cdot x + \frac{1}{3} \cdot x} \]
    2. accelerator-lowering-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(x \cdot x + 0\right) \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right), x, \frac{1}{3} \cdot x\right)} \]
    3. +-rgt-identityN/A

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \color{blue}{\left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    7. +-rgt-identityN/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \left(x \cdot \color{blue}{\left(x \cdot \frac{2}{945}\right)} + \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    8. associate-*r*N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \left(\color{blue}{\left(x \cdot x\right) \cdot \frac{2}{945}} + \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    9. accelerator-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \frac{2}{945}, \frac{1}{45}\right)}\right), x, \frac{1}{3} \cdot x\right) \]
    10. +-rgt-identityN/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x + 0}, \frac{2}{945}, \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    11. accelerator-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, x, 0\right)}, \frac{2}{945}, \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    12. *-lowering-*.f6499.4

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), 0.0021164021164021165, 0.022222222222222223\right)\right), x, \color{blue}{0.3333333333333333 \cdot x}\right) \]
  7. Applied egg-rr99.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), 0.0021164021164021165, 0.022222222222222223\right)\right), x, 0.3333333333333333 \cdot x\right)} \]
  8. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)}, x, \frac{1}{3} \cdot x\right) \]
  9. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)}, x, \frac{1}{3} \cdot x\right) \]
    2. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot x\right)} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right), x, \frac{1}{3} \cdot x\right) \]
    3. *-lowering-*.f64N/A

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

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \color{blue}{\left(\frac{2}{945} \cdot {x}^{2} + \frac{1}{45}\right)}, x, \frac{1}{3} \cdot x\right) \]
    5. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \left(\color{blue}{{x}^{2} \cdot \frac{2}{945}} + \frac{1}{45}\right), x, \frac{1}{3} \cdot x\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot \frac{2}{945} + \frac{1}{45}\right), x, \frac{1}{3} \cdot x\right) \]
    7. associate-*l*N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \left(\color{blue}{x \cdot \left(x \cdot \frac{2}{945}\right)} + \frac{1}{45}\right), x, \frac{1}{3} \cdot x\right) \]
    8. accelerator-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \color{blue}{\mathsf{fma}\left(x, x \cdot \frac{2}{945}, \frac{1}{45}\right)}, x, \frac{1}{3} \cdot x\right) \]
    9. *-lowering-*.f6499.4

      \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot 0.0021164021164021165}, 0.022222222222222223\right), x, 0.3333333333333333 \cdot x\right) \]
  10. Simplified99.4%

    \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right)}, x, 0.3333333333333333 \cdot x\right) \]
  11. Final simplification99.4%

    \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), x, x \cdot 0.3333333333333333\right) \]
  12. Add Preprocessing

Alternative 4: 99.5% accurate, 4.5× speedup?

\[\begin{array}{l} \\ x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), 0.3333333333333333\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  x
  (fma
   (* x x)
   (fma x (* x 0.0021164021164021165) 0.022222222222222223)
   0.3333333333333333)))
double code(double x) {
	return x * fma((x * x), fma(x, (x * 0.0021164021164021165), 0.022222222222222223), 0.3333333333333333);
}
function code(x)
	return Float64(x * fma(Float64(x * x), fma(x, Float64(x * 0.0021164021164021165), 0.022222222222222223), 0.3333333333333333))
end
code[x_] := N[(x * N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * 0.0021164021164021165), $MachinePrecision] + 0.022222222222222223), $MachinePrecision] + 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right) + \frac{1}{3}\right)} \]
    3. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right)} \]
    4. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    5. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{neg}\left(\color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) + 0\right)}\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    6. distribute-neg-inN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)\right) + \left(\mathsf{neg}\left(0\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    7. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{{x}^{2}} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    8. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    9. metadata-evalN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x + \color{blue}{0}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    10. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, x, 0\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    11. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\frac{2}{945} \cdot {x}^{2} + \frac{1}{45}}, \frac{1}{3}\right) \]
    12. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \frac{2}{945} \cdot \color{blue}{\left(x \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    13. associate-*r*N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\left(\frac{2}{945} \cdot x\right) \cdot x} + \frac{1}{45}, \frac{1}{3}\right) \]
    14. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{x \cdot \left(\frac{2}{945} \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    15. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\mathsf{fma}\left(x, \frac{2}{945} \cdot x, \frac{1}{45}\right)}, \frac{1}{3}\right) \]
    16. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\frac{2}{945} \cdot x + 0}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    17. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{2}{945}} + 0, \frac{1}{45}\right), \frac{1}{3}\right) \]
    18. accelerator-lowering-fma.f6499.4

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.0021164021164021165, 0\right)}, 0.022222222222222223\right), 0.3333333333333333\right) \]
  5. Simplified99.4%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.0021164021164021165, 0\right), 0.022222222222222223\right), 0.3333333333333333\right)} \]
  6. Taylor expanded in x around 0

    \[\leadsto x \cdot \color{blue}{\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
  7. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right) + \frac{1}{3}\right)} \]
    2. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right)} \]
    3. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    5. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{\frac{2}{945} \cdot {x}^{2} + \frac{1}{45}}, \frac{1}{3}\right) \]
    6. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{{x}^{2} \cdot \frac{2}{945}} + \frac{1}{45}, \frac{1}{3}\right) \]
    7. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{\left(x \cdot x\right)} \cdot \frac{2}{945} + \frac{1}{45}, \frac{1}{3}\right) \]
    8. associate-*l*N/A

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

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, x \cdot \color{blue}{\left(\frac{2}{945} \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    10. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \color{blue}{\mathsf{fma}\left(x, \frac{2}{945} \cdot x, \frac{1}{45}\right)}, \frac{1}{3}\right) \]
    11. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{2}{945}}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    12. *-lowering-*.f6499.4

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, \color{blue}{x \cdot 0.0021164021164021165}, 0.022222222222222223\right), 0.3333333333333333\right) \]
  8. Simplified99.4%

    \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot 0.0021164021164021165, 0.022222222222222223\right), 0.3333333333333333\right)} \]
  9. Add Preprocessing

Alternative 5: 99.4% accurate, 5.7× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.022222222222222223, x, x \cdot 0.3333333333333333\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (fma (* (* x x) 0.022222222222222223) x (* x 0.3333333333333333)))
double code(double x) {
	return fma(((x * x) * 0.022222222222222223), x, (x * 0.3333333333333333));
}
function code(x)
	return fma(Float64(Float64(x * x) * 0.022222222222222223), x, Float64(x * 0.3333333333333333))
end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] * 0.022222222222222223), $MachinePrecision] * x + N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.022222222222222223, x, x \cdot 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right) + \frac{1}{3}\right)} \]
    3. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right)} \]
    4. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    5. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{neg}\left(\color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) + 0\right)}\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    6. distribute-neg-inN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left({x}^{2}\right)\right)\right)\right) + \left(\mathsf{neg}\left(0\right)\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    7. remove-double-negN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{{x}^{2}} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    8. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x} + \left(\mathsf{neg}\left(0\right)\right), \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    9. metadata-evalN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x \cdot x + \color{blue}{0}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    10. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, x, 0\right)}, \frac{1}{45} + \frac{2}{945} \cdot {x}^{2}, \frac{1}{3}\right) \]
    11. +-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\frac{2}{945} \cdot {x}^{2} + \frac{1}{45}}, \frac{1}{3}\right) \]
    12. unpow2N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \frac{2}{945} \cdot \color{blue}{\left(x \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    13. associate-*r*N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\left(\frac{2}{945} \cdot x\right) \cdot x} + \frac{1}{45}, \frac{1}{3}\right) \]
    14. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{x \cdot \left(\frac{2}{945} \cdot x\right)} + \frac{1}{45}, \frac{1}{3}\right) \]
    15. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \color{blue}{\mathsf{fma}\left(x, \frac{2}{945} \cdot x, \frac{1}{45}\right)}, \frac{1}{3}\right) \]
    16. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\frac{2}{945} \cdot x + 0}, \frac{1}{45}\right), \frac{1}{3}\right) \]
    17. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{2}{945}} + 0, \frac{1}{45}\right), \frac{1}{3}\right) \]
    18. accelerator-lowering-fma.f6499.4

      \[\leadsto x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.0021164021164021165, 0\right)}, 0.022222222222222223\right), 0.3333333333333333\right) \]
  5. Simplified99.4%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.0021164021164021165, 0\right), 0.022222222222222223\right), 0.3333333333333333\right)} \]
  6. Step-by-step derivation
    1. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\left(\left(x \cdot x + 0\right) \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right) \cdot x + \frac{1}{3} \cdot x} \]
    2. accelerator-lowering-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\left(x \cdot x + 0\right) \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right), x, \frac{1}{3} \cdot x\right)} \]
    3. +-rgt-identityN/A

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot \left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    6. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \color{blue}{\left(x \cdot \left(x \cdot \left(x \cdot \frac{2}{945} + 0\right) + \frac{1}{45}\right)\right)}, x, \frac{1}{3} \cdot x\right) \]
    7. +-rgt-identityN/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \left(x \cdot \color{blue}{\left(x \cdot \frac{2}{945}\right)} + \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    8. associate-*r*N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \left(\color{blue}{\left(x \cdot x\right) \cdot \frac{2}{945}} + \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    9. accelerator-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \color{blue}{\mathsf{fma}\left(x \cdot x, \frac{2}{945}, \frac{1}{45}\right)}\right), x, \frac{1}{3} \cdot x\right) \]
    10. +-rgt-identityN/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\color{blue}{x \cdot x + 0}, \frac{2}{945}, \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    11. accelerator-lowering-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(x, x, 0\right)}, \frac{2}{945}, \frac{1}{45}\right)\right), x, \frac{1}{3} \cdot x\right) \]
    12. *-lowering-*.f6499.4

      \[\leadsto \mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), 0.0021164021164021165, 0.022222222222222223\right)\right), x, \color{blue}{0.3333333333333333 \cdot x}\right) \]
  7. Applied egg-rr99.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot \left(x \cdot \mathsf{fma}\left(\mathsf{fma}\left(x, x, 0\right), 0.0021164021164021165, 0.022222222222222223\right)\right), x, 0.3333333333333333 \cdot x\right)} \]
  8. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{1}{45} \cdot {x}^{2}}, x, \frac{1}{3} \cdot x\right) \]
  9. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \frac{1}{45}}, x, \frac{1}{3} \cdot x\right) \]
    2. *-lowering-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{{x}^{2} \cdot \frac{1}{45}}, x, \frac{1}{3} \cdot x\right) \]
    3. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot x\right)} \cdot \frac{1}{45}, x, \frac{1}{3} \cdot x\right) \]
    4. *-lowering-*.f6499.3

      \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot x\right)} \cdot 0.022222222222222223, x, 0.3333333333333333 \cdot x\right) \]
  10. Simplified99.3%

    \[\leadsto \mathsf{fma}\left(\color{blue}{\left(x \cdot x\right) \cdot 0.022222222222222223}, x, 0.3333333333333333 \cdot x\right) \]
  11. Final simplification99.3%

    \[\leadsto \mathsf{fma}\left(\left(x \cdot x\right) \cdot 0.022222222222222223, x, x \cdot 0.3333333333333333\right) \]
  12. Add Preprocessing

Alternative 6: 99.4% accurate, 7.4× speedup?

\[\begin{array}{l} \\ x \cdot \mathsf{fma}\left(x, x \cdot 0.022222222222222223, 0.3333333333333333\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (* x (fma x (* x 0.022222222222222223) 0.3333333333333333)))
double code(double x) {
	return x * fma(x, (x * 0.022222222222222223), 0.3333333333333333);
}
function code(x)
	return Float64(x * fma(x, Float64(x * 0.022222222222222223), 0.3333333333333333))
end
code[x_] := N[(x * N[(x * N[(x * 0.022222222222222223), $MachinePrecision] + 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x \cdot \mathsf{fma}\left(x, x \cdot 0.022222222222222223, 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
    2. +-commutativeN/A

      \[\leadsto x \cdot \color{blue}{\left(\frac{1}{45} \cdot {x}^{2} + \frac{1}{3}\right)} \]
    3. unpow2N/A

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

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

      \[\leadsto x \cdot \left(\color{blue}{x \cdot \left(\frac{1}{45} \cdot x\right)} + \frac{1}{3}\right) \]
    6. accelerator-lowering-fma.f64N/A

      \[\leadsto x \cdot \color{blue}{\mathsf{fma}\left(x, \frac{1}{45} \cdot x, \frac{1}{3}\right)} \]
    7. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{\frac{1}{45} \cdot x + 0}, \frac{1}{3}\right) \]
    8. *-commutativeN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{1}{45}} + 0, \frac{1}{3}\right) \]
    9. accelerator-lowering-fma.f6499.3

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, 0.022222222222222223, 0\right)}, 0.3333333333333333\right) \]
  5. Simplified99.3%

    \[\leadsto \color{blue}{x \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.022222222222222223, 0\right), 0.3333333333333333\right)} \]
  6. Step-by-step derivation
    1. +-rgt-identityN/A

      \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot \frac{1}{45}}, \frac{1}{3}\right) \]
    2. *-lowering-*.f6499.3

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

    \[\leadsto x \cdot \mathsf{fma}\left(x, \color{blue}{x \cdot 0.022222222222222223}, 0.3333333333333333\right) \]
  8. Add Preprocessing

Alternative 7: 98.9% accurate, 21.0× speedup?

\[\begin{array}{l} \\ x \cdot 0.3333333333333333 \end{array} \]
(FPCore (x) :precision binary64 (* x 0.3333333333333333))
double code(double x) {
	return x * 0.3333333333333333;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * 0.3333333333333333d0
end function
public static double code(double x) {
	return x * 0.3333333333333333;
}
def code(x):
	return x * 0.3333333333333333
function code(x)
	return Float64(x * 0.3333333333333333)
end
function tmp = code(x)
	tmp = x * 0.3333333333333333;
end
code[x_] := N[(x * 0.3333333333333333), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 0.3333333333333333
\end{array}
Derivation
  1. Initial program 7.1%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\frac{1}{3} \cdot x} \]
  4. Step-by-step derivation
    1. *-lowering-*.f6498.4

      \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  5. Simplified98.4%

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  6. Final simplification98.4%

    \[\leadsto x \cdot 0.3333333333333333 \]
  7. Add Preprocessing

Developer Target 1: 99.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|x\right| < 0.026:\\ \;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (< (fabs x) 0.026)
   (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0)))
   (- (/ 1.0 x) (/ 1.0 (tan x)))))
double code(double x) {
	double tmp;
	if (fabs(x) < 0.026) {
		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
	} else {
		tmp = (1.0 / x) - (1.0 / tan(x));
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (abs(x) < 0.026d0) then
        tmp = (x / 3.0d0) * (1.0d0 + ((x * x) / 15.0d0))
    else
        tmp = (1.0d0 / x) - (1.0d0 / tan(x))
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (Math.abs(x) < 0.026) {
		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
	} else {
		tmp = (1.0 / x) - (1.0 / Math.tan(x));
	}
	return tmp;
}
def code(x):
	tmp = 0
	if math.fabs(x) < 0.026:
		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0))
	else:
		tmp = (1.0 / x) - (1.0 / math.tan(x))
	return tmp
function code(x)
	tmp = 0.0
	if (abs(x) < 0.026)
		tmp = Float64(Float64(x / 3.0) * Float64(1.0 + Float64(Float64(x * x) / 15.0)));
	else
		tmp = Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (abs(x) < 0.026)
		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
	else
		tmp = (1.0 / x) - (1.0 / tan(x));
	end
	tmp_2 = tmp;
end
code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.026], N[(N[(x / 3.0), $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] / 15.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| < 0.026:\\
\;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\


\end{array}
\end{array}

Reproduce

?
herbie shell --seed 2024194 
(FPCore (x)
  :name "invcot (example 3.9)"
  :precision binary64
  :pre (and (< -0.026 x) (< x 0.026))

  :alt
  (! :herbie-platform default (if (< (fabs x) 13/500) (* (/ x 3) (+ 1 (/ (* x x) 15))) (- (/ 1 x) (/ 1 (tan x)))))

  (- (/ 1.0 x) (/ 1.0 (tan x))))