Average Error: 59.9 → 0.4
Time: 26.7s
Precision: 64
\[-0.026 \lt x \land x \lt 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[\frac{2}{945} \cdot {x}^{5} + x \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(x \cdot \frac{1}{45}, x, \frac{1}{3}\right)\right)\right)\]
\frac{1}{x} - \frac{1}{\tan x}
\frac{2}{945} \cdot {x}^{5} + x \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(x \cdot \frac{1}{45}, x, \frac{1}{3}\right)\right)\right)
double f(double x) {
        double r1370428 = 1.0;
        double r1370429 = x;
        double r1370430 = r1370428 / r1370429;
        double r1370431 = tan(r1370429);
        double r1370432 = r1370428 / r1370431;
        double r1370433 = r1370430 - r1370432;
        return r1370433;
}

double f(double x) {
        double r1370434 = 0.0021164021164021165;
        double r1370435 = x;
        double r1370436 = 5.0;
        double r1370437 = pow(r1370435, r1370436);
        double r1370438 = r1370434 * r1370437;
        double r1370439 = 0.022222222222222223;
        double r1370440 = r1370435 * r1370439;
        double r1370441 = 0.3333333333333333;
        double r1370442 = fma(r1370440, r1370435, r1370441);
        double r1370443 = log1p(r1370442);
        double r1370444 = expm1(r1370443);
        double r1370445 = r1370435 * r1370444;
        double r1370446 = r1370438 + r1370445;
        return r1370446;
}

Error

Bits error versus x

Target

Original59.9
Target0.1
Herbie0.4
\[\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.9

    \[\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}{\mathsf{fma}\left(x \cdot x, x \cdot \frac{1}{45}, \mathsf{fma}\left(\frac{1}{3}, x, {x}^{5} \cdot \frac{2}{945}\right)\right)}\]
  4. Taylor expanded around -inf 0.3

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

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

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot \frac{1}{45}, x, \frac{1}{3}\right) \cdot x} + \frac{2}{945} \cdot {x}^{5}\]
  9. Using strategy rm
  10. Applied expm1-log1p-u0.4

    \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(x \cdot \frac{1}{45}, x, \frac{1}{3}\right)\right)\right)} \cdot x + \frac{2}{945} \cdot {x}^{5}\]
  11. Final simplification0.4

    \[\leadsto \frac{2}{945} \cdot {x}^{5} + x \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(x \cdot \frac{1}{45}, x, \frac{1}{3}\right)\right)\right)\]

Reproduce

herbie shell --seed 2019151 +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))))