invcot (example 3.9)

Percentage Accurate: 6.3% → 99.6%
Time: 15.7s
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.3% 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.5× speedup?

\[\begin{array}{l} \\ 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{x \cdot 0.1111111111111111}{0.3333333333333333}\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (+
  (* 0.0021164021164021165 (pow x 5.0))
  (+
   (* 0.022222222222222223 (pow x 3.0))
   (/ (* x 0.1111111111111111) 0.3333333333333333))))
double code(double x) {
	return (0.0021164021164021165 * pow(x, 5.0)) + ((0.022222222222222223 * pow(x, 3.0)) + ((x * 0.1111111111111111) / 0.3333333333333333));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (0.0021164021164021165d0 * (x ** 5.0d0)) + ((0.022222222222222223d0 * (x ** 3.0d0)) + ((x * 0.1111111111111111d0) / 0.3333333333333333d0))
end function
public static double code(double x) {
	return (0.0021164021164021165 * Math.pow(x, 5.0)) + ((0.022222222222222223 * Math.pow(x, 3.0)) + ((x * 0.1111111111111111) / 0.3333333333333333));
}
def code(x):
	return (0.0021164021164021165 * math.pow(x, 5.0)) + ((0.022222222222222223 * math.pow(x, 3.0)) + ((x * 0.1111111111111111) / 0.3333333333333333))
function code(x)
	return Float64(Float64(0.0021164021164021165 * (x ^ 5.0)) + Float64(Float64(0.022222222222222223 * (x ^ 3.0)) + Float64(Float64(x * 0.1111111111111111) / 0.3333333333333333)))
end
function tmp = code(x)
	tmp = (0.0021164021164021165 * (x ^ 5.0)) + ((0.022222222222222223 * (x ^ 3.0)) + ((x * 0.1111111111111111) / 0.3333333333333333));
end
code[x_] := N[(N[(0.0021164021164021165 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.022222222222222223 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(x * 0.1111111111111111), $MachinePrecision] / 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{x \cdot 0.1111111111111111}{0.3333333333333333}\right)
\end{array}
Derivation
  1. Initial program 6.4%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. 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)} \]
  4. Step-by-step derivation
    1. add099.5%

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

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

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \left(\color{blue}{x \cdot 0.3333333333333333} - 0\right)\right) \]
    6. add-sqr-sqrt51.1%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \left(\color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)} \cdot 0.3333333333333333 - 0\right)\right) \]
    7. associate-*l*51.1%

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\mathsf{fma}\left(\sqrt{x}, \sqrt{x} \cdot 0.3333333333333333, -0\right)}\right) \]
    9. metadata-eval51.1%

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\left(\sqrt{x} \cdot \left(\sqrt{x} \cdot 0.3333333333333333\right) + 0\right)}\right) \]
    2. +-rgt-identity51.1%

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

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{0.3333333333333333 \cdot \left(\sqrt{x} \cdot \sqrt{x}\right)}\right) \]
  7. Simplified51.1%

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \sqrt{0.1111111111111111} \cdot \color{blue}{\sqrt{x \cdot x}}\right) \]
    3. unpow229.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \sqrt{0.1111111111111111} \cdot \sqrt{\color{blue}{{x}^{2}}}\right) \]
    4. sqrt-prod29.3%

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \sqrt{\color{blue}{{x}^{2} \cdot 0.1111111111111111}}\right) \]
    6. add029.3%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\left(\sqrt{{x}^{2} \cdot 0.1111111111111111} + 0\right)}\right) \]
    7. flip-+25.9%

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\sqrt{{x}^{2} \cdot 0.1111111111111111} \cdot \sqrt{{x}^{2} \cdot 0.1111111111111111} - \color{blue}{0}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    9. fma-neg25.9%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\color{blue}{\mathsf{fma}\left(\sqrt{{x}^{2} \cdot 0.1111111111111111}, \sqrt{{x}^{2} \cdot 0.1111111111111111}, -0\right)}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    10. metadata-eval25.9%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\mathsf{fma}\left(\sqrt{{x}^{2} \cdot 0.1111111111111111}, \sqrt{{x}^{2} \cdot 0.1111111111111111}, \color{blue}{0}\right)}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    11. fma-define25.9%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\color{blue}{\sqrt{{x}^{2} \cdot 0.1111111111111111} \cdot \sqrt{{x}^{2} \cdot 0.1111111111111111} + 0}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    12. add025.9%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\color{blue}{\sqrt{{x}^{2} \cdot 0.1111111111111111} \cdot \sqrt{{x}^{2} \cdot 0.1111111111111111}}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    13. pow225.9%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\color{blue}{{\left(\sqrt{{x}^{2} \cdot 0.1111111111111111}\right)}^{2}}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    14. sqrt-prod25.8%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{{\color{blue}{\left(\sqrt{{x}^{2}} \cdot \sqrt{0.1111111111111111}\right)}}^{2}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    15. unpow225.8%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{{\left(\sqrt{\color{blue}{x \cdot x}} \cdot \sqrt{0.1111111111111111}\right)}^{2}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    16. sqrt-unprod25.1%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{{\left(\color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)} \cdot \sqrt{0.1111111111111111}\right)}^{2}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    17. add-sqr-sqrt25.8%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{{\left(\color{blue}{x} \cdot \sqrt{0.1111111111111111}\right)}^{2}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
    18. metadata-eval25.8%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{{\left(x \cdot \color{blue}{0.3333333333333333}\right)}^{2}}{\sqrt{{x}^{2} \cdot 0.1111111111111111} - 0}\right) \]
  9. Applied egg-rr53.7%

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\frac{{\left(x \cdot 0.3333333333333333\right)}^{2}}{x \cdot 0.3333333333333333}}\right) \]
  10. Step-by-step derivation
    1. associate-/r*53.5%

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

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

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

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

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{\frac{\color{blue}{x \cdot x}}{x} \cdot 0.1111111111111111}{0.3333333333333333}\right) \]
    8. associate-*r/99.7%

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

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

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

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

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \frac{x \cdot 0.1111111111111111}{0.3333333333333333}\right) \]
  13. Add Preprocessing

Alternative 2: 99.5% accurate, 0.5× speedup?

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

\\
0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + x \cdot 0.3333333333333333\right)
\end{array}
Derivation
  1. Initial program 6.4%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. 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)} \]
  4. Final simplification99.5%

    \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + x \cdot 0.3333333333333333\right) \]
  5. Add Preprocessing

Alternative 3: 99.0% 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.4%

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

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

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

Alternative 4: 99.5% accurate, 35.7× speedup?

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

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

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  4. Step-by-step derivation
    1. add099.5%

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

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

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \left(\color{blue}{x \cdot 0.3333333333333333} - 0\right)\right) \]
    6. add-sqr-sqrt51.1%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \left(\color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)} \cdot 0.3333333333333333 - 0\right)\right) \]
    7. associate-*l*51.1%

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\mathsf{fma}\left(\sqrt{x}, \sqrt{x} \cdot 0.3333333333333333, -0\right)}\right) \]
    9. metadata-eval51.1%

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{x}, \sqrt{x} \cdot 0.3333333333333333, 0\right)} \]
  6. Step-by-step derivation
    1. fma-undefine51.1%

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{\left(\sqrt{x} \cdot \left(\sqrt{x} \cdot 0.3333333333333333\right) + 0\right)}\right) \]
    2. +-rgt-identity51.1%

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

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

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

      \[\leadsto 0.0021164021164021165 \cdot {x}^{5} + \left(0.022222222222222223 \cdot {x}^{3} + \color{blue}{0.3333333333333333 \cdot \left(\sqrt{x} \cdot \sqrt{x}\right)}\right) \]
  7. Simplified51.0%

    \[\leadsto \color{blue}{0.3333333333333333 \cdot \left(\sqrt{x} \cdot \sqrt{x}\right)} \]
  8. Step-by-step derivation
    1. metadata-eval51.0%

      \[\leadsto \color{blue}{\sqrt{0.1111111111111111}} \cdot \left(\sqrt{x} \cdot \sqrt{x}\right) \]
    2. sqrt-unprod29.1%

      \[\leadsto \sqrt{0.1111111111111111} \cdot \color{blue}{\sqrt{x \cdot x}} \]
    3. unpow229.1%

      \[\leadsto \sqrt{0.1111111111111111} \cdot \sqrt{\color{blue}{{x}^{2}}} \]
    4. sqrt-prod29.1%

      \[\leadsto \color{blue}{\sqrt{0.1111111111111111 \cdot {x}^{2}}} \]
    5. *-commutative29.1%

      \[\leadsto \sqrt{\color{blue}{{x}^{2} \cdot 0.1111111111111111}} \]
    6. add-cube-cbrt28.6%

      \[\leadsto \color{blue}{\left(\sqrt[3]{\sqrt{{x}^{2} \cdot 0.1111111111111111}} \cdot \sqrt[3]{\sqrt{{x}^{2} \cdot 0.1111111111111111}}\right) \cdot \sqrt[3]{\sqrt{{x}^{2} \cdot 0.1111111111111111}}} \]
    7. pow328.6%

      \[\leadsto \color{blue}{{\left(\sqrt[3]{\sqrt{{x}^{2} \cdot 0.1111111111111111}}\right)}^{3}} \]
    8. sqrt-prod28.6%

      \[\leadsto {\left(\sqrt[3]{\color{blue}{\sqrt{{x}^{2}} \cdot \sqrt{0.1111111111111111}}}\right)}^{3} \]
    9. unpow228.6%

      \[\leadsto {\left(\sqrt[3]{\sqrt{\color{blue}{x \cdot x}} \cdot \sqrt{0.1111111111111111}}\right)}^{3} \]
    10. sqrt-unprod50.2%

      \[\leadsto {\left(\sqrt[3]{\color{blue}{\left(\sqrt{x} \cdot \sqrt{x}\right)} \cdot \sqrt{0.1111111111111111}}\right)}^{3} \]
    11. add-sqr-sqrt97.2%

      \[\leadsto {\left(\sqrt[3]{\color{blue}{x} \cdot \sqrt{0.1111111111111111}}\right)}^{3} \]
    12. metadata-eval97.2%

      \[\leadsto {\left(\sqrt[3]{x \cdot \color{blue}{0.3333333333333333}}\right)}^{3} \]
  9. Applied egg-rr97.2%

    \[\leadsto \color{blue}{{\left(\sqrt[3]{x \cdot 0.3333333333333333}\right)}^{3}} \]
  10. Step-by-step derivation
    1. rem-cube-cbrt99.0%

      \[\leadsto \color{blue}{x \cdot 0.3333333333333333} \]
    2. metadata-eval99.0%

      \[\leadsto x \cdot \color{blue}{\frac{1}{3}} \]
    3. div-inv99.5%

      \[\leadsto \color{blue}{\frac{x}{3}} \]
  11. Applied egg-rr99.5%

    \[\leadsto \color{blue}{\frac{x}{3}} \]
  12. Final simplification99.5%

    \[\leadsto \frac{x}{3} \]
  13. Add Preprocessing

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 2024034 
(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))))