Average Error: 59.9 → 0.4
Time: 1.4m
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(\log \left(e^{\mathsf{fma}\left(x, \left(\frac{1}{45} \cdot x\right), \frac{1}{3}\right)}\right) \cdot x\right)\right)\]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{fma}\left(\left({x}^{5}\right), \frac{2}{945}, \left(\log \left(e^{\mathsf{fma}\left(x, \left(\frac{1}{45} \cdot x\right), \frac{1}{3}\right)}\right) \cdot x\right)\right)
double f(double x) {
        double r6557478 = 1.0;
        double r6557479 = x;
        double r6557480 = r6557478 / r6557479;
        double r6557481 = tan(r6557479);
        double r6557482 = r6557478 / r6557481;
        double r6557483 = r6557480 - r6557482;
        return r6557483;
}

double f(double x) {
        double r6557484 = x;
        double r6557485 = 5.0;
        double r6557486 = pow(r6557484, r6557485);
        double r6557487 = 0.0021164021164021165;
        double r6557488 = 0.022222222222222223;
        double r6557489 = r6557488 * r6557484;
        double r6557490 = 0.3333333333333333;
        double r6557491 = fma(r6557484, r6557489, r6557490);
        double r6557492 = exp(r6557491);
        double r6557493 = log(r6557492);
        double r6557494 = r6557493 * r6557484;
        double r6557495 = fma(r6557486, r6557487, r6557494);
        return r6557495;
}

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

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

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

Reproduce

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