invcot (example 3.9)

Percentage Accurate: 6.4% → 99.6%
Time: 12.7s
Alternatives: 5
Speedup: 35.7×

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 5 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.4% 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.6% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)\\ 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, x \cdot \frac{x}{t_0}, \frac{{x}^{6} \cdot -0.0004938271604938272}{t_0}\right) \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fma 0.3333333333333333 x (* (pow x 3.0) -0.022222222222222223))))
   (+
    (* 0.0021164021164021165 (pow x 5.0))
    (fma
     0.1111111111111111
     (* x (/ x t_0))
     (/ (* (pow x 6.0) -0.0004938271604938272) t_0)))))
double code(double x) {
	double t_0 = fma(0.3333333333333333, x, (pow(x, 3.0) * -0.022222222222222223));
	return (0.0021164021164021165 * pow(x, 5.0)) + fma(0.1111111111111111, (x * (x / t_0)), ((pow(x, 6.0) * -0.0004938271604938272) / t_0));
}
function code(x)
	t_0 = fma(0.3333333333333333, x, Float64((x ^ 3.0) * -0.022222222222222223))
	return Float64(Float64(0.0021164021164021165 * (x ^ 5.0)) + fma(0.1111111111111111, Float64(x * Float64(x / t_0)), Float64(Float64((x ^ 6.0) * -0.0004938271604938272) / t_0)))
end
code[x_] := Block[{t$95$0 = N[(0.3333333333333333 * x + N[(N[Power[x, 3.0], $MachinePrecision] * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]}, N[(N[(0.0021164021164021165 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision] + N[(0.1111111111111111 * N[(x * N[(x / t$95$0), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[x, 6.0], $MachinePrecision] * -0.0004938271604938272), $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)\\
0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, x \cdot \frac{x}{t_0}, \frac{{x}^{6} \cdot -0.0004938271604938272}{t_0}\right)
\end{array}
\end{array}
Derivation
  1. Initial program 6.6%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Taylor expanded in x around 0 99.5%

    \[\leadsto \color{blue}{0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x\right)} \]
  3. Step-by-step derivation
    1. +-commutative99.5%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\left(0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}\right)} \]
    2. *-commutative99.5%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(\color{blue}{x \cdot 0.3333333333333333} + 0.022222222222222223 \cdot {x}^{3}\right) \]
    3. fma-def99.5%

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

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\mathsf{fma}\left(x, 0.3333333333333333, 0.022222222222222223 \cdot {x}^{3}\right)} \]
  5. Step-by-step derivation
    1. fma-udef99.5%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\left(x \cdot 0.3333333333333333 + 0.022222222222222223 \cdot {x}^{3}\right)} \]
    2. *-commutative99.5%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(\color{blue}{0.3333333333333333 \cdot x} + 0.022222222222222223 \cdot {x}^{3}\right) \]
    3. flip-+50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}} \]
    4. swap-sqr50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{\color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)} - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    5. metadata-eval50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{\color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    6. swap-sqr50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{\left(0.022222222222222223 \cdot 0.022222222222222223\right) \cdot \left({x}^{3} \cdot {x}^{3}\right)}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    7. metadata-eval50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{0.0004938271604938272} \cdot \left({x}^{3} \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    8. pow-sqr50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{0.1111111111111111 \cdot \left(x \cdot x\right) - 0.0004938271604938272 \cdot \color{blue}{{x}^{\left(2 \cdot 3\right)}}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    9. metadata-eval50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{0.1111111111111111 \cdot \left(x \cdot x\right) - 0.0004938271604938272 \cdot {x}^{\color{blue}{6}}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    10. *-commutative50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \frac{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{{x}^{6} \cdot 0.0004938271604938272}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    11. div-sub50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\left(\frac{0.1111111111111111 \cdot \left(x \cdot x\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} - \frac{{x}^{6} \cdot 0.0004938271604938272}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}\right)} \]
    12. sub-neg50.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\left(\frac{0.1111111111111111 \cdot \left(x \cdot x\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} + \left(-\frac{{x}^{6} \cdot 0.0004938271604938272}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}\right)\right)} \]
  6. Applied egg-rr50.5%

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\left(0.1111111111111111 \cdot \frac{x \cdot x}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)} + \left(-\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right)\right)} \]
  7. Step-by-step derivation
    1. fma-def50.5%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\mathsf{fma}\left(0.1111111111111111, \frac{x \cdot x}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right)} \]
    2. associate-/l*99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \color{blue}{\frac{x}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}{x}}}, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    3. associate-/r/99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \color{blue}{\frac{x}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x}, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    4. fma-udef99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\color{blue}{x \cdot 0.3333333333333333 + {x}^{3} \cdot -0.022222222222222223}} \cdot x, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    5. *-commutative99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\color{blue}{0.3333333333333333 \cdot x} + {x}^{3} \cdot -0.022222222222222223} \cdot x, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    6. fma-def99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}} \cdot x, -\frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    7. distribute-neg-frac99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \color{blue}{\frac{-{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}}\right) \]
    8. distribute-rgt-neg-in99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{\color{blue}{{x}^{6} \cdot \left(-0.0004938271604938272\right)}}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    9. metadata-eval99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{{x}^{6} \cdot \color{blue}{-0.0004938271604938272}}{\mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]
    10. fma-udef99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{{x}^{6} \cdot -0.0004938271604938272}{\color{blue}{x \cdot 0.3333333333333333 + {x}^{3} \cdot -0.022222222222222223}}\right) \]
    11. *-commutative99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{{x}^{6} \cdot -0.0004938271604938272}{\color{blue}{0.3333333333333333 \cdot x} + {x}^{3} \cdot -0.022222222222222223}\right) \]
    12. fma-def99.6%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{{x}^{6} \cdot -0.0004938271604938272}{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}}\right) \]
  8. Simplified99.6%

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \color{blue}{\mathsf{fma}\left(0.1111111111111111, \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot x, \frac{{x}^{6} \cdot -0.0004938271604938272}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}\right)} \]
  9. Final simplification99.6%

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \mathsf{fma}\left(0.1111111111111111, x \cdot \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}, \frac{{x}^{6} \cdot -0.0004938271604938272}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}\right) \]

Alternative 2: 99.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{\mathsf{fma}\left(x, -0.2, \frac{3}{x}\right)} \end{array} \]
(FPCore (x) :precision binary64 (/ 1.0 (fma x -0.2 (/ 3.0 x))))
double code(double x) {
	return 1.0 / fma(x, -0.2, (3.0 / x));
}
function code(x)
	return Float64(1.0 / fma(x, -0.2, Float64(3.0 / x)))
end
code[x_] := N[(1.0 / N[(x * -0.2 + N[(3.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{\mathsf{fma}\left(x, -0.2, \frac{3}{x}\right)}
\end{array}
Derivation
  1. Initial program 6.6%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Taylor expanded in x around 0 99.3%

    \[\leadsto \color{blue}{0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x} \]
  3. Step-by-step derivation
    1. flip-+50.2%

      \[\leadsto \color{blue}{\frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}} \]
    2. clear-num50.1%

      \[\leadsto \color{blue}{\frac{1}{\frac{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}}} \]
    3. cancel-sign-sub-inv50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{0.022222222222222223 \cdot {x}^{3} + \left(-0.3333333333333333\right) \cdot x}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    4. fma-def50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \left(-0.3333333333333333\right) \cdot x\right)}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    5. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \color{blue}{-0.3333333333333333} \cdot x\right)}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    6. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    7. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    8. swap-sqr50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    9. pow-prod-up50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    10. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    11. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot \color{blue}{0.0004938271604938272} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    12. swap-sqr50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)}}} \]
    13. metadata-eval50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right)}} \]
  4. Applied egg-rr50.2%

    \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - 0.1111111111111111 \cdot \left(x \cdot x\right)}}} \]
  5. Taylor expanded in x around 0 99.3%

    \[\leadsto \frac{1}{\color{blue}{-0.2 \cdot x + 3 \cdot \frac{1}{x}}} \]
  6. Step-by-step derivation
    1. *-commutative99.3%

      \[\leadsto \frac{1}{\color{blue}{x \cdot -0.2} + 3 \cdot \frac{1}{x}} \]
    2. fma-def99.3%

      \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, -0.2, 3 \cdot \frac{1}{x}\right)}} \]
    3. associate-*r/99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(x, -0.2, \color{blue}{\frac{3 \cdot 1}{x}}\right)} \]
    4. metadata-eval99.5%

      \[\leadsto \frac{1}{\mathsf{fma}\left(x, -0.2, \frac{\color{blue}{3}}{x}\right)} \]
  7. Simplified99.5%

    \[\leadsto \frac{1}{\color{blue}{\mathsf{fma}\left(x, -0.2, \frac{3}{x}\right)}} \]
  8. Final simplification99.5%

    \[\leadsto \frac{1}{\mathsf{fma}\left(x, -0.2, \frac{3}{x}\right)} \]

Alternative 3: 99.3% accurate, 9.7× speedup?

\[\begin{array}{l} \\ \frac{1}{x \cdot -0.2 + 3 \cdot \frac{1}{x}} \end{array} \]
(FPCore (x) :precision binary64 (/ 1.0 (+ (* x -0.2) (* 3.0 (/ 1.0 x)))))
double code(double x) {
	return 1.0 / ((x * -0.2) + (3.0 * (1.0 / x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 / ((x * (-0.2d0)) + (3.0d0 * (1.0d0 / x)))
end function
public static double code(double x) {
	return 1.0 / ((x * -0.2) + (3.0 * (1.0 / x)));
}
def code(x):
	return 1.0 / ((x * -0.2) + (3.0 * (1.0 / x)))
function code(x)
	return Float64(1.0 / Float64(Float64(x * -0.2) + Float64(3.0 * Float64(1.0 / x))))
end
function tmp = code(x)
	tmp = 1.0 / ((x * -0.2) + (3.0 * (1.0 / x)));
end
code[x_] := N[(1.0 / N[(N[(x * -0.2), $MachinePrecision] + N[(3.0 * N[(1.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x \cdot -0.2 + 3 \cdot \frac{1}{x}}
\end{array}
Derivation
  1. Initial program 6.6%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Taylor expanded in x around 0 99.3%

    \[\leadsto \color{blue}{0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x} \]
  3. Step-by-step derivation
    1. flip-+50.2%

      \[\leadsto \color{blue}{\frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}} \]
    2. clear-num50.1%

      \[\leadsto \color{blue}{\frac{1}{\frac{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}}} \]
    3. cancel-sign-sub-inv50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{0.022222222222222223 \cdot {x}^{3} + \left(-0.3333333333333333\right) \cdot x}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    4. fma-def50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \left(-0.3333333333333333\right) \cdot x\right)}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    5. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \color{blue}{-0.3333333333333333} \cdot x\right)}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    6. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    7. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    8. swap-sqr50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    9. pow-prod-up50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    10. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    11. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot \color{blue}{0.0004938271604938272} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    12. swap-sqr50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)}}} \]
    13. metadata-eval50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right)}} \]
  4. Applied egg-rr50.2%

    \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - 0.1111111111111111 \cdot \left(x \cdot x\right)}}} \]
  5. Taylor expanded in x around 0 99.3%

    \[\leadsto \frac{1}{\color{blue}{-0.2 \cdot x + 3 \cdot \frac{1}{x}}} \]
  6. Final simplification99.3%

    \[\leadsto \frac{1}{x \cdot -0.2 + 3 \cdot \frac{1}{x}} \]

Alternative 4: 99.0% accurate, 21.4× speedup?

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

\\
\frac{1}{\frac{3}{x}}
\end{array}
Derivation
  1. Initial program 6.6%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Taylor expanded in x around 0 99.3%

    \[\leadsto \color{blue}{0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x} \]
  3. Step-by-step derivation
    1. flip-+50.2%

      \[\leadsto \color{blue}{\frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}} \]
    2. clear-num50.1%

      \[\leadsto \color{blue}{\frac{1}{\frac{0.022222222222222223 \cdot {x}^{3} - 0.3333333333333333 \cdot x}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}}} \]
    3. cancel-sign-sub-inv50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{0.022222222222222223 \cdot {x}^{3} + \left(-0.3333333333333333\right) \cdot x}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    4. fma-def50.1%

      \[\leadsto \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \left(-0.3333333333333333\right) \cdot x\right)}}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    5. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, \color{blue}{-0.3333333333333333} \cdot x\right)}{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    6. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} \cdot \left(0.022222222222222223 \cdot {x}^{3}\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    7. *-commutative50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    8. swap-sqr50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    9. pow-prod-up50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{\color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    10. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right) - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    11. metadata-eval50.1%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot \color{blue}{0.0004938271604938272} - \left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}} \]
    12. swap-sqr50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)}}} \]
    13. metadata-eval50.2%

      \[\leadsto \frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - \color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right)}} \]
  4. Applied egg-rr50.2%

    \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, -0.3333333333333333 \cdot x\right)}{{x}^{6} \cdot 0.0004938271604938272 - 0.1111111111111111 \cdot \left(x \cdot x\right)}}} \]
  5. Taylor expanded in x around 0 99.0%

    \[\leadsto \frac{1}{\color{blue}{\frac{3}{x}}} \]
  6. Final simplification99.0%

    \[\leadsto \frac{1}{\frac{3}{x}} \]

Alternative 5: 98.9% accurate, 35.7× 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 6.6%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Taylor expanded in x around 0 98.8%

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  3. Final simplification98.8%

    \[\leadsto x \cdot 0.3333333333333333 \]

Developer target: 99.9% accurate, 0.5× 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 2023293 
(FPCore (x)
  :name "invcot (example 3.9)"
  :precision binary64
  :pre (and (< -0.026 x) (< x 0.026))

  :herbie-target
  (if (< (fabs x) 0.026) (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x))))

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