?

Average Accuracy: 6.5% → 99.6%
Time: 15.5s
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(-0.022222222222222223, {x}^{3}, x \cdot 0.3333333333333333\right)\\ \mathsf{fma}\left(0.1111111111111111 \cdot x, \frac{x}{t_0}, \frac{-{x}^{6}}{\frac{t_0}{0.0004938271604938272}}\right) \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (fma -0.022222222222222223 (pow x 3.0) (* x 0.3333333333333333))))
   (fma
    (* 0.1111111111111111 x)
    (/ x t_0)
    (/ (- (pow x 6.0)) (/ t_0 0.0004938271604938272)))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
	double t_0 = fma(-0.022222222222222223, pow(x, 3.0), (x * 0.3333333333333333));
	return fma((0.1111111111111111 * x), (x / t_0), (-pow(x, 6.0) / (t_0 / 0.0004938271604938272)));
}
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function code(x)
	t_0 = fma(-0.022222222222222223, (x ^ 3.0), Float64(x * 0.3333333333333333))
	return fma(Float64(0.1111111111111111 * x), Float64(x / t_0), Float64(Float64(-(x ^ 6.0)) / Float64(t_0 / 0.0004938271604938272)))
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[(-0.022222222222222223 * N[Power[x, 3.0], $MachinePrecision] + N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]}, N[(N[(0.1111111111111111 * x), $MachinePrecision] * N[(x / t$95$0), $MachinePrecision] + N[((-N[Power[x, 6.0], $MachinePrecision]) / N[(t$95$0 / 0.0004938271604938272), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{1}{x} - \frac{1}{\tan x}
\begin{array}{l}
t_0 := \mathsf{fma}\left(-0.022222222222222223, {x}^{3}, x \cdot 0.3333333333333333\right)\\
\mathsf{fma}\left(0.1111111111111111 \cdot x, \frac{x}{t_0}, \frac{-{x}^{6}}{\frac{t_0}{0.0004938271604938272}}\right)
\end{array}

Error?

Target

Original6.5%
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 6.5%

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}} \]
  3. Simplified99.4%

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

    [Start]99.4

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

    fma-def [=>]99.4

    \[ \color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 0.022222222222222223 \cdot {x}^{3}\right)} \]
  4. Applied egg-rr54.0%

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

    [Start]99.4

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

    fma-udef [=>]99.4

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

    flip-+ [=>]54.0

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

    *-commutative [=>]54.0

    \[ \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \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 [=>]54.0

    \[ \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \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 [=>]54.0

    \[ \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \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 [=>]54.0

    \[ \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \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 [=>]54.0

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

    metadata-eval [=>]54.0

    \[ \frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - {x}^{6} \cdot \color{blue}{0.0004938271604938272}}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}} \]
  5. Simplified54.0%

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

    [Start]54.0

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

    swap-sqr [=>]54.0

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

    metadata-eval [=>]54.0

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

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

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

    metadata-eval [=>]54.0

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

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

    [Start]54.0

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

    div-sub [=>]54.0

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

    associate-*r* [=>]54.0

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

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

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

    times-frac [=>]99.6

    \[ \color{blue}{\frac{0.1111111111111111 \cdot x}{1} \cdot \frac{x}{0.3333333333333333 \cdot x + -0.022222222222222223 \cdot {x}^{3}}} - \frac{{x}^{6} \cdot 0.0004938271604938272}{0.3333333333333333 \cdot x + -0.022222222222222223 \cdot {x}^{3}} \]

    fma-neg [=>]99.6

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

    +-commutative [=>]99.6

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

    fma-def [=>]99.6

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

    *-commutative [=>]99.6

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

    associate-/l* [=>]99.6

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

    +-commutative [=>]99.6

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

    fma-def [=>]99.6

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

    *-commutative [=>]99.6

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

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

Alternatives

Alternative 1
Accuracy99.5%
Cost20352
\[x \cdot 0.3333333333333333 + \left(0.0021164021164021165 \cdot {x}^{5} + \left({x}^{3} \cdot 0.022222222222222223 + 0.00021164021164021165 \cdot {x}^{7}\right)\right) \]
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%
Cost576
\[x \cdot \left(0.3333333333333333 + x \cdot \left(x \cdot 0.022222222222222223\right)\right) \]
Alternative 4
Accuracy98.9%
Cost192
\[x \cdot 0.3333333333333333 \]

Error

Reproduce?

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