?

Average Accuracy: 6.6% → 99.8%
Time: 13.8s
Precision: binary64
Cost: 6976

?

\[-0.026 < x \land x < 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x} \]
\[\frac{x}{\frac{1}{\mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x)
 :precision binary64
 (/ x (/ 1.0 (fma 0.022222222222222223 (* x x) 0.3333333333333333))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
	return x / (1.0 / fma(0.022222222222222223, (x * x), 0.3333333333333333));
}
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function code(x)
	return Float64(x / Float64(1.0 / fma(0.022222222222222223, Float64(x * x), 0.3333333333333333)))
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := N[(x / N[(1.0 / N[(0.022222222222222223 * N[(x * x), $MachinePrecision] + 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{x} - \frac{1}{\tan x}
\frac{x}{\frac{1}{\mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}}

Error?

Target

Original6.6%
Target99.8%
Herbie99.8%
\[\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.6%

    \[\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-rr99.3%

    \[\leadsto \color{blue}{x \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right) + 0.3333333333333333\right)} \]
    Proof

    [Start]99.3

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

    +-commutative [=>]99.3

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

    *-commutative [=>]99.3

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

    cube-mult [=>]99.3

    \[ \color{blue}{\left(x \cdot \left(x \cdot x\right)\right)} \cdot 0.022222222222222223 + 0.3333333333333333 \cdot x \]

    associate-*l* [=>]99.3

    \[ \color{blue}{x \cdot \left(\left(x \cdot x\right) \cdot 0.022222222222222223\right)} + 0.3333333333333333 \cdot x \]

    *-commutative [=>]99.3

    \[ x \cdot \left(\left(x \cdot x\right) \cdot 0.022222222222222223\right) + \color{blue}{x \cdot 0.3333333333333333} \]

    distribute-lft-out [=>]99.3

    \[ \color{blue}{x \cdot \left(\left(x \cdot x\right) \cdot 0.022222222222222223 + 0.3333333333333333\right)} \]

    *-commutative [=>]99.3

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

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

    [Start]99.3

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

    flip-+ [=>]99.3

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

    associate-*r/ [=>]99.4

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

    associate-/l* [=>]99.9

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

    clear-num [=>]99.9

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

    flip-+ [<=]99.8

    \[ \frac{x}{\frac{1}{\color{blue}{0.022222222222222223 \cdot \left(x \cdot x\right) + 0.3333333333333333}}} \]

    fma-def [=>]99.8

    \[ \frac{x}{\frac{1}{\color{blue}{\mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}}} \]
  5. Final simplification99.8%

    \[\leadsto \frac{x}{\frac{1}{\mathsf{fma}\left(0.022222222222222223, x \cdot x, 0.3333333333333333\right)}} \]

Alternatives

Alternative 1
Accuracy99.6%
Cost704
\[\frac{1}{\frac{1}{\frac{x}{3}} + x \cdot -0.2} \]
Alternative 2
Accuracy99.3%
Cost576
\[x \cdot \left(0.3333333333333333 + 0.022222222222222223 \cdot \left(x \cdot x\right)\right) \]
Alternative 3
Accuracy98.9%
Cost320
\[\frac{1}{\frac{3}{x}} \]
Alternative 4
Accuracy98.8%
Cost192
\[x \cdot 0.3333333333333333 \]

Error

Reproduce?

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