Average Error: 60.0 → 0.3
Time: 1.6m
Precision: 64
\[-0.026 \lt x \land x \lt 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[\mathsf{fma}\left(\left({x}^{5}\right), \frac{2}{945}, \left(x \cdot \frac{1}{3} + x \cdot \left(\frac{1}{45} \cdot \left(x \cdot x\right)\right)\right)\right)\]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{fma}\left(\left({x}^{5}\right), \frac{2}{945}, \left(x \cdot \frac{1}{3} + x \cdot \left(\frac{1}{45} \cdot \left(x \cdot x\right)\right)\right)\right)
double f(double x) {
        double r11318798 = 1.0;
        double r11318799 = x;
        double r11318800 = r11318798 / r11318799;
        double r11318801 = tan(r11318799);
        double r11318802 = r11318798 / r11318801;
        double r11318803 = r11318800 - r11318802;
        return r11318803;
}

double f(double x) {
        double r11318804 = x;
        double r11318805 = 5.0;
        double r11318806 = pow(r11318804, r11318805);
        double r11318807 = 0.0021164021164021165;
        double r11318808 = 0.3333333333333333;
        double r11318809 = r11318804 * r11318808;
        double r11318810 = 0.022222222222222223;
        double r11318811 = r11318804 * r11318804;
        double r11318812 = r11318810 * r11318811;
        double r11318813 = r11318804 * r11318812;
        double r11318814 = r11318809 + r11318813;
        double r11318815 = fma(r11318806, r11318807, r11318814);
        return r11318815;
}

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(\left({x}^{5}\right), \frac{2}{945}, \left(x \cdot \mathsf{fma}\left(\frac{1}{45}, \left(x \cdot x\right), \frac{1}{3}\right)\right)\right)}\]
  4. Using strategy rm
  5. Applied fma-udef0.3

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

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

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