Average Error: 60.0 → 0.3
Time: 27.2s
Precision: 64
\[-0.0259999999999999988065102485279567190446 \lt x \land x \lt 0.0259999999999999988065102485279567190446\]
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, \mathsf{fma}\left(0.3333333333333333148296162562473909929395, x, 0.002116402116402116544841005563171165704262 \cdot {x}^{5}\right)\right)\right)\right)\]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, \mathsf{fma}\left(0.3333333333333333148296162562473909929395, x, 0.002116402116402116544841005563171165704262 \cdot {x}^{5}\right)\right)\right)\right)
double f(double x) {
        double r130621 = 1.0;
        double r130622 = x;
        double r130623 = r130621 / r130622;
        double r130624 = tan(r130622);
        double r130625 = r130621 / r130624;
        double r130626 = r130623 - r130625;
        return r130626;
}

double f(double x) {
        double r130627 = 0.022222222222222223;
        double r130628 = x;
        double r130629 = 3.0;
        double r130630 = pow(r130628, r130629);
        double r130631 = 0.3333333333333333;
        double r130632 = 0.0021164021164021165;
        double r130633 = 5.0;
        double r130634 = pow(r130628, r130633);
        double r130635 = r130632 * r130634;
        double r130636 = fma(r130631, r130628, r130635);
        double r130637 = fma(r130627, r130630, r130636);
        double r130638 = log1p(r130637);
        double r130639 = expm1(r130638);
        return r130639;
}

Error

Bits error versus x

Target

Original60.0
Target0.1
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;\left|x\right| \lt 0.0259999999999999988065102485279567190446:\\ \;\;\;\;\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 60.0

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

    \[\leadsto \color{blue}{0.3333333333333333148296162562473909929395 \cdot x + \left(0.02222222222222222307030925492199457949027 \cdot {x}^{3} + 0.002116402116402116544841005563171165704262 \cdot {x}^{5}\right)}\]
  3. Simplified0.3

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.002116402116402116544841005563171165704262, {x}^{5}, \mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, 0.3333333333333333148296162562473909929395 \cdot x\right)\right)}\]
  4. Using strategy rm
  5. Applied expm1-log1p-u0.3

    \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.002116402116402116544841005563171165704262, {x}^{5}, \mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, 0.3333333333333333148296162562473909929395 \cdot x\right)\right)\right)\right)}\]
  6. Simplified0.3

    \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(\mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, \mathsf{fma}\left(0.3333333333333333148296162562473909929395, x, {x}^{5} \cdot 0.002116402116402116544841005563171165704262\right)\right)\right)}\right)\]
  7. Final simplification0.3

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{fma}\left(0.02222222222222222307030925492199457949027, {x}^{3}, \mathsf{fma}\left(0.3333333333333333148296162562473909929395, x, 0.002116402116402116544841005563171165704262 \cdot {x}^{5}\right)\right)\right)\right)\]

Reproduce

herbie shell --seed 2019195 +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.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x))))

  (- (/ 1.0 x) (/ 1.0 (tan x))))