Average Error: 59.8 → 0.3
Time: 1.4m
Precision: 64
\[-0.026 \lt x \land x \lt 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[(\left({x}^{5}\right) \cdot \frac{2}{945} + \left(x \cdot \frac{1}{3} + x \cdot \left(\left(\frac{1}{45} \cdot x\right) \cdot x\right)\right))_*\]
\frac{1}{x} - \frac{1}{\tan x}
(\left({x}^{5}\right) \cdot \frac{2}{945} + \left(x \cdot \frac{1}{3} + x \cdot \left(\left(\frac{1}{45} \cdot x\right) \cdot x\right)\right))_*
double f(double x) {
        double r10689136 = 1.0;
        double r10689137 = x;
        double r10689138 = r10689136 / r10689137;
        double r10689139 = tan(r10689137);
        double r10689140 = r10689136 / r10689139;
        double r10689141 = r10689138 - r10689140;
        return r10689141;
}

double f(double x) {
        double r10689142 = x;
        double r10689143 = 5.0;
        double r10689144 = pow(r10689142, r10689143);
        double r10689145 = 0.0021164021164021165;
        double r10689146 = 0.3333333333333333;
        double r10689147 = r10689142 * r10689146;
        double r10689148 = 0.022222222222222223;
        double r10689149 = r10689148 * r10689142;
        double r10689150 = r10689149 * r10689142;
        double r10689151 = r10689142 * r10689150;
        double r10689152 = r10689147 + r10689151;
        double r10689153 = fma(r10689144, r10689145, r10689152);
        return r10689153;
}

Error

Bits error versus x

Target

Original59.8
Target0.1
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;\left|x\right| \lt 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 59.8

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

    \[\leadsto \color{blue}{\frac{1}{3} \cdot x + \left(\frac{1}{45} \cdot {x}^{3} + \frac{2}{945} \cdot {x}^{5}\right)}\]
  3. Simplified0.3

    \[\leadsto \color{blue}{(\left({x}^{5}\right) \cdot \frac{2}{945} + \left(x \cdot (x \cdot \left(\frac{1}{45} \cdot x\right) + \frac{1}{3})_*\right))_*}\]
  4. Using strategy rm
  5. Applied fma-udef0.3

    \[\leadsto (\left({x}^{5}\right) \cdot \frac{2}{945} + \left(x \cdot \color{blue}{\left(x \cdot \left(\frac{1}{45} \cdot x\right) + \frac{1}{3}\right)}\right))_*\]
  6. Applied distribute-rgt-in0.3

    \[\leadsto (\left({x}^{5}\right) \cdot \frac{2}{945} + \color{blue}{\left(\left(x \cdot \left(\frac{1}{45} \cdot x\right)\right) \cdot x + \frac{1}{3} \cdot x\right)})_*\]
  7. Final simplification0.3

    \[\leadsto (\left({x}^{5}\right) \cdot \frac{2}{945} + \left(x \cdot \frac{1}{3} + x \cdot \left(\left(\frac{1}{45} \cdot x\right) \cdot x\right)\right))_*\]

Reproduce

herbie shell --seed 2019112 +o rules:numerics
(FPCore (x)
  :name "invcot (example 3.9)"
  :pre (and (< -0.026 x) (< x 0.026))

  :herbie-target
  (if (< (fabs x) 0.026) (* (/ x 3) (+ 1 (/ (* x x) 15))) (- (/ 1 x) (/ 1 (tan x))))

  (- (/ 1 x) (/ 1 (tan x))))