?

Average Accuracy: 6.3% → 99.5%
Time: 13.1s
Precision: binary64
Cost: 13504

?

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

Error?

Target

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

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

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
  3. Applied egg-rr7.6%

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

    [Start]99.0

    \[ 0.3333333333333333 \cdot x \]

    expm1-log1p-u [=>]99.0

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(0.3333333333333333 \cdot x\right)\right)} \]

    expm1-udef [=>]7.6

    \[ \color{blue}{e^{\mathsf{log1p}\left(0.3333333333333333 \cdot x\right)} - 1} \]

    log1p-udef [=>]7.6

    \[ e^{\color{blue}{\log \left(1 + 0.3333333333333333 \cdot x\right)}} - 1 \]

    add-exp-log [<=]7.6

    \[ \color{blue}{\left(1 + 0.3333333333333333 \cdot x\right)} - 1 \]
  4. Applied egg-rr7.5%

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

    [Start]7.6

    \[ \left(1 + 0.3333333333333333 \cdot x\right) - 1 \]

    flip-- [=>]7.5

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

    div-inv [=>]7.5

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

    metadata-eval [=>]7.5

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

    sub-neg [=>]7.5

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

    pow2 [=>]7.5

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

    +-commutative [=>]7.5

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

    fma-def [=>]7.5

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

    metadata-eval [=>]7.5

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

    +-commutative [=>]7.5

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

    associate-+l+ [=>]7.5

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

    metadata-eval [=>]7.5

    \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{0.3333333333333333 \cdot x + \color{blue}{2}} \]
  5. Simplified98.8%

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

    [Start]7.5

    \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{0.3333333333333333 \cdot x + 2} \]

    associate-*r/ [=>]7.5

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

    *-rgt-identity [=>]7.5

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

    unpow2 [=>]7.5

    \[ \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 1\right) \cdot \mathsf{fma}\left(0.3333333333333333, x, 1\right)} + -1}{0.3333333333333333 \cdot x + 2} \]

    difference-of-sqr--1 [=>]7.6

    \[ \frac{\color{blue}{\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) + 1\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}}{0.3333333333333333 \cdot x + 2} \]

    fma-udef [=>]7.6

    \[ \frac{\left(\color{blue}{\left(0.3333333333333333 \cdot x + 1\right)} + 1\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2} \]

    associate-+l+ [=>]7.6

    \[ \frac{\color{blue}{\left(0.3333333333333333 \cdot x + \left(1 + 1\right)\right)} \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2} \]

    metadata-eval [=>]7.6

    \[ \frac{\left(0.3333333333333333 \cdot x + \color{blue}{2}\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2} \]

    fma-udef [=>]7.6

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

    associate--l+ [=>]99.0

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

    metadata-eval [=>]99.0

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

    +-rgt-identity [=>]99.0

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

    associate-/l* [=>]98.8

    \[ \color{blue}{\frac{0.3333333333333333 \cdot x + 2}{\frac{0.3333333333333333 \cdot x + 2}{0.3333333333333333 \cdot x}}} \]

    fma-def [=>]98.8

    \[ \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}}{\frac{0.3333333333333333 \cdot x + 2}{0.3333333333333333 \cdot x}} \]

    fma-def [=>]98.8

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

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

    [Start]98.8

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

    div-inv [=>]98.8

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

    clear-num [<=]99.0

    \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{\frac{0.3333333333333333 \cdot x}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}} \]

    *-commutative [=>]99.0

    \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{\color{blue}{x \cdot 0.3333333333333333}}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)} \]

    associate-/l* [=>]99.5

    \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{\frac{x}{\frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{0.3333333333333333}}} \]

    div-inv [=>]99.5

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

    metadata-eval [=>]99.5

    \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{3}} \]
  7. Final simplification99.5%

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

Alternatives

Alternative 1
Accuracy99.4%
Cost6912
\[0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3} \]
Alternative 2
Accuracy99.1%
Cost6656
\[{\left(\frac{3}{x}\right)}^{-1} \]
Alternative 3
Accuracy99.0%
Cost192
\[0.3333333333333333 \cdot x \]

Error

Reproduce?

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