Average Error: 60.0 → 0.3
Time: 1.3m
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 r5597926 = 1.0;
        double r5597927 = x;
        double r5597928 = r5597926 / r5597927;
        double r5597929 = tan(r5597927);
        double r5597930 = r5597926 / r5597929;
        double r5597931 = r5597928 - r5597930;
        return r5597931;
}

double f(double x) {
        double r5597932 = x;
        double r5597933 = 5.0;
        double r5597934 = pow(r5597932, r5597933);
        double r5597935 = 0.0021164021164021165;
        double r5597936 = 0.3333333333333333;
        double r5597937 = r5597932 * r5597936;
        double r5597938 = 0.022222222222222223;
        double r5597939 = r5597938 * r5597932;
        double r5597940 = r5597939 * r5597932;
        double r5597941 = r5597932 * r5597940;
        double r5597942 = r5597937 + r5597941;
        double r5597943 = fma(r5597934, r5597935, r5597942);
        return r5597943;
}

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}{(\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-lft-in0.3

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