invcot (example 3.9)

?

Percentage Accurate: 6.3% → 99.6%
Time: 16.7s
Precision: binary64
Cost: 39872

?

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

Local Percentage Accuracy?

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.

Target

Original6.3%
Target99.9%
Herbie99.6%
\[\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} \]

Derivation?

  1. Initial program 7.0%

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}} \]
  3. Applied egg-rr50.2%

    \[\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)}} \]
    Step-by-step derivation

    [Start]99.3

    \[ 0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3} \]

    flip-+ [=>]50.1

    \[ \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}}} \]

    div-sub [=>]50.1

    \[ \color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} - \frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}} \]

    swap-sqr [=>]50.2

    \[ \frac{\color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} - \frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]

    metadata-eval [=>]50.2

    \[ \frac{\color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} - \frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]

    cancel-sign-sub-inv [=>]50.2

    \[ \frac{0.1111111111111111 \cdot \left(x \cdot x\right)}{\color{blue}{0.3333333333333333 \cdot x + \left(-0.022222222222222223\right) \cdot {x}^{3}}} - \frac{\left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]

    fma-def [=>]50.2

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

    metadata-eval [=>]50.2

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

    *-commutative [=>]50.2

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

    *-commutative [=>]50.2

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

    swap-sqr [=>]50.2

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

    pow-prod-up [=>]50.2

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

    metadata-eval [=>]50.2

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

    metadata-eval [=>]50.2

    \[ \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 \color{blue}{0.0004938271604938272}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]

    cancel-sign-sub-inv [=>]50.2

    \[ \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}{\color{blue}{0.3333333333333333 \cdot x + \left(-0.022222222222222223\right) \cdot {x}^{3}}} \]

    fma-def [=>]50.2

    \[ \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}{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, \left(-0.022222222222222223\right) \cdot {x}^{3}\right)}} \]

    metadata-eval [=>]50.2

    \[ \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, \color{blue}{-0.022222222222222223} \cdot {x}^{3}\right)} \]
  4. Simplified50.2%

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

    [Start]50.2

    \[ \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)} \]

    div-sub [<=]50.2

    \[ \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)}} \]

    *-commutative [=>]50.2

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

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

    [Start]50.2

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

    div-sub [=>]50.2

    \[ \color{blue}{\frac{0.1111111111111111 \cdot \left(x \cdot x\right)}{\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)}} \]

    associate-*r* [=>]50.2

    \[ \frac{\color{blue}{\left(0.1111111111111111 \cdot x\right) \cdot 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)} \]

    *-un-lft-identity [=>]50.2

    \[ \frac{\left(0.1111111111111111 \cdot x\right) \cdot x}{\color{blue}{1 \cdot \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)} \]

    times-frac [=>]99.5

    \[ \color{blue}{\frac{0.1111111111111111 \cdot x}{1} \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)} \]

    fma-neg [=>]99.5

    \[ \color{blue}{\mathsf{fma}\left(\frac{0.1111111111111111 \cdot x}{1}, \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)} \]

    fma-udef [=>]99.5

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

    *-commutative [=>]99.5

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

    fma-def [=>]99.5

    \[ \mathsf{fma}\left(\frac{0.1111111111111111 \cdot x}{1}, \frac{x}{\color{blue}{\mathsf{fma}\left(x, 0.3333333333333333, {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) \]

    *-commutative [=>]99.5

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

    *-un-lft-identity [=>]99.5

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

    times-frac [=>]99.5

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

    metadata-eval [=>]99.5

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

    fma-udef [=>]99.5

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

    *-commutative [=>]99.5

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

    fma-def [=>]99.5

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

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

Alternatives

Alternative 1
Accuracy99.6%
Cost39808
\[\begin{array}{l} t_0 := \mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)\\ \mathsf{fma}\left(0.1111111111111111, \frac{x}{\frac{t_0}{x}}, \frac{{x}^{6}}{t_0} \cdot -0.0004938271604938272\right) \end{array} \]
Alternative 2
Accuracy99.5%
Cost13632
\[x \cdot 0.3333333333333333 + \left(0.0021164021164021165 \cdot {x}^{5} + {x}^{3} \cdot 0.022222222222222223\right) \]
Alternative 3
Accuracy99.4%
Cost704
\[x \cdot 0.3333333333333333 + x \cdot \left(x \cdot \left(x \cdot 0.022222222222222223\right)\right) \]
Alternative 4
Accuracy99.4%
Cost576
\[x \cdot \left(0.3333333333333333 + x \cdot \left(x \cdot 0.022222222222222223\right)\right) \]
Alternative 5
Accuracy99.0%
Cost192
\[x \cdot 0.3333333333333333 \]

Error

Reproduce?

herbie shell --seed 2023159 
(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))))