Average Error: 60.0 → 0.3
Time: 28.6s
Precision: 64
\[-0.026 \lt x \land x \lt 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[\mathsf{fma}\left(\frac{1}{3}, x, \left(\mathsf{fma}\left(\frac{1}{45}, \left(\left(x \cdot x\right) \cdot x\right), \left(\frac{2}{945} \cdot {x}^{5}\right)\right)\right)\right)\]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{fma}\left(\frac{1}{3}, x, \left(\mathsf{fma}\left(\frac{1}{45}, \left(\left(x \cdot x\right) \cdot x\right), \left(\frac{2}{945} \cdot {x}^{5}\right)\right)\right)\right)
double f(double x) {
        double r1754032 = 1.0;
        double r1754033 = x;
        double r1754034 = r1754032 / r1754033;
        double r1754035 = tan(r1754033);
        double r1754036 = r1754032 / r1754035;
        double r1754037 = r1754034 - r1754036;
        return r1754037;
}

double f(double x) {
        double r1754038 = 0.3333333333333333;
        double r1754039 = x;
        double r1754040 = 0.022222222222222223;
        double r1754041 = r1754039 * r1754039;
        double r1754042 = r1754041 * r1754039;
        double r1754043 = 0.0021164021164021165;
        double r1754044 = 5.0;
        double r1754045 = pow(r1754039, r1754044);
        double r1754046 = r1754043 * r1754045;
        double r1754047 = fma(r1754040, r1754042, r1754046);
        double r1754048 = fma(r1754038, r1754039, r1754047);
        return r1754048;
}

Error

Bits error versus x

Target

Original60.0
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 60.0

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

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

Reproduce

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