invcot (example 3.9)

Percentage Accurate: 6.4% → 99.2%
Time: 13.8s
Alternatives: 4
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 4 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.2% accurate, 0.3× speedup?

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

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

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

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

      \[\leadsto \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}}} \]
    2. div-inv56.5%

      \[\leadsto \color{blue}{\left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}} \]
    3. swap-sqr56.6%

      \[\leadsto \left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    4. metadata-eval56.6%

      \[\leadsto \left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    5. *-commutative56.6%

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

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)}\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    7. swap-sqr56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)}\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    8. pow-prod-up56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    9. metadata-eval56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - {x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    10. metadata-eval56.6%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{x \cdot \left(x \cdot 0.1111111111111111\right)}{\mathsf{fma}\left(x, 0.3333333333333333, 0.022222222222222223 \cdot {x}^{3}\right)} - \frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, 0.022222222222222223 \cdot {x}^{3}\right)}} \]
  7. Step-by-step derivation
    1. associate-/l*99.7%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{x \cdot 0.3333333333333333 + 0.022222222222222223 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}}{0.0004938271604938272}} \]
    12. associate-*r*99.7%

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

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

      \[\leadsto \frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{\color{blue}{x \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) + x \cdot 0.3333333333333333}}{0.0004938271604938272}} \]
    15. distribute-lft-out99.7%

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

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

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

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

Alternative 2: 99.2% accurate, 0.5× speedup?

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

\\
\frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - 0.022222222222222223 \cdot {x}^{3}
\end{array}
Derivation
  1. Initial program 5.4%

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

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

      \[\leadsto \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}}} \]
    2. div-inv56.5%

      \[\leadsto \color{blue}{\left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}} \]
    3. swap-sqr56.6%

      \[\leadsto \left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    4. metadata-eval56.6%

      \[\leadsto \left(\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)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    5. *-commutative56.6%

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

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)}\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    7. swap-sqr56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)}\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    8. pow-prod-up56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    9. metadata-eval56.6%

      \[\leadsto \left(0.1111111111111111 \cdot \left(x \cdot x\right) - {x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)\right) \cdot \frac{1}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
    10. metadata-eval56.6%

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{x \cdot \left(x \cdot 0.1111111111111111\right)}{\mathsf{fma}\left(x, 0.3333333333333333, 0.022222222222222223 \cdot {x}^{3}\right)} - \frac{{x}^{6} \cdot 0.0004938271604938272}{\mathsf{fma}\left(x, 0.3333333333333333, 0.022222222222222223 \cdot {x}^{3}\right)}} \]
  7. Step-by-step derivation
    1. associate-/l*99.7%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{x \cdot 0.3333333333333333 + 0.022222222222222223 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}}{0.0004938271604938272}} \]
    12. associate-*r*99.7%

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

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

      \[\leadsto \frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{\color{blue}{x \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) + x \cdot 0.3333333333333333}}{0.0004938271604938272}} \]
    15. distribute-lft-out99.7%

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

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

    \[\leadsto \color{blue}{\frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{0.0004938271604938272}}} \]
  9. Taylor expanded in x around inf 99.7%

    \[\leadsto \frac{x}{\frac{x \cdot \mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}{x \cdot 0.1111111111111111}} - \color{blue}{0.022222222222222223 \cdot {x}^{3}} \]
  10. Final simplification99.7%

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

Alternative 3: 99.4% accurate, 11.9× speedup?

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

\\
x \cdot \left(0.3333333333333333 + 0.022222222222222223 \cdot \left(x \cdot x\right)\right)
\end{array}
Derivation
  1. Initial program 5.4%

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}} \]
  3. Step-by-step derivation
    1. expm1-log1p-u99.5%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}\right)\right)} \]
    2. expm1-udef7.2%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}\right)} - 1} \]
    3. +-commutative7.2%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x}\right)} - 1 \]
    4. fma-def7.2%

      \[\leadsto e^{\mathsf{log1p}\left(\color{blue}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, 0.3333333333333333 \cdot x\right)}\right)} - 1 \]
  4. Applied egg-rr7.2%

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

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.022222222222222223, {x}^{3}, 0.3333333333333333 \cdot x\right)\right)\right)} \]
    2. expm1-log1p99.5%

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

      \[\leadsto \color{blue}{0.022222222222222223 \cdot {x}^{3} + 0.3333333333333333 \cdot x} \]
    4. cube-mult99.5%

      \[\leadsto 0.022222222222222223 \cdot \color{blue}{\left(x \cdot \left(x \cdot x\right)\right)} + 0.3333333333333333 \cdot x \]
    5. associate-*r*99.5%

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

      \[\leadsto \color{blue}{\left(x \cdot 0.022222222222222223\right)} \cdot \left(x \cdot x\right) + 0.3333333333333333 \cdot x \]
    7. associate-*r*99.5%

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

      \[\leadsto x \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) + \color{blue}{x \cdot 0.3333333333333333} \]
    9. distribute-lft-out99.5%

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

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

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

      \[\leadsto x \cdot \color{blue}{\left(0.022222222222222223 \cdot \left(x \cdot x\right) + 0.3333333333333333\right)} \]
  8. Applied egg-rr99.5%

    \[\leadsto x \cdot \color{blue}{\left(0.022222222222222223 \cdot \left(x \cdot x\right) + 0.3333333333333333\right)} \]
  9. Final simplification99.5%

    \[\leadsto x \cdot \left(0.3333333333333333 + 0.022222222222222223 \cdot \left(x \cdot x\right)\right) \]

Alternative 4: 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 5.4%

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  3. Final simplification99.4%

    \[\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 2023230 
(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))))