Average Error: 59.9 → 0.3
Time: 15.2s
Precision: binary64
Cost: 13568
\[-0.026 < x \land x < 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x} \]
\[\frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot \left(x \cdot 0.1111111111111111\right) \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x)
 :precision binary64
 (*
  (/ x (fma 0.3333333333333333 x (* (pow x 3.0) -0.022222222222222223)))
  (* x 0.1111111111111111)))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
	return (x / fma(0.3333333333333333, x, (pow(x, 3.0) * -0.022222222222222223))) * (x * 0.1111111111111111);
}
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function code(x)
	return Float64(Float64(x / fma(0.3333333333333333, x, Float64((x ^ 3.0) * -0.022222222222222223))) * Float64(x * 0.1111111111111111))
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := N[(N[(x / N[(0.3333333333333333 * x + N[(N[Power[x, 3.0], $MachinePrecision] * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(x * 0.1111111111111111), $MachinePrecision]), $MachinePrecision]
\frac{1}{x} - \frac{1}{\tan x}
\frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot \left(x \cdot 0.1111111111111111\right)

Error

Target

Original59.9
Target0.1
Herbie0.3
\[\begin{array}{l} \mathbf{if}\;\left|x\right| < 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 in x around 0 0.4

    \[\leadsto \color{blue}{0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}} \]
  3. Simplified0.4

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 0.022222222222222223 \cdot {x}^{3}\right)} \]
    Proof
    (fma.f64 1/3 x (*.f64 1/45 (pow.f64 x 3))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= fma-def_binary64 (+.f64 (*.f64 1/3 x) (*.f64 1/45 (pow.f64 x 3)))): 0 points increase in error, 0 points decrease in error
  4. Applied egg-rr28.5

    \[\leadsto \color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - {x}^{6} \cdot 0.0004938271604938272}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}} \]
  5. Simplified28.5

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(x \cdot 0.1111111111111111, x, {x}^{6} \cdot -0.0004938271604938272\right)}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)}} \]
    Proof
    (/.f64 (fma.f64 (*.f64 x 1/9) x (*.f64 (pow.f64 x 6) -1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 0 points decrease in error
    (/.f64 (fma.f64 (*.f64 x (Rewrite<= metadata-eval (*.f64 1/3 1/3))) x (*.f64 (pow.f64 x 6) -1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 12 points decrease in error
    (/.f64 (fma.f64 (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 x 1/3) 1/3)) x (*.f64 (pow.f64 x 6) -1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 12 points increase in error, 0 points decrease in error
    (/.f64 (fma.f64 (*.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/3 x)) 1/3) x (*.f64 (pow.f64 x 6) -1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 2 points decrease in error
    (/.f64 (fma.f64 (*.f64 (*.f64 1/3 x) 1/3) x (*.f64 (pow.f64 x 6) (Rewrite<= metadata-eval (neg.f64 1/2025)))) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 12 points increase in error, 0 points decrease in error
    (/.f64 (fma.f64 (*.f64 (*.f64 1/3 x) 1/3) x (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (pow.f64 x 6) 1/2025)))) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 12 points increase in error, 0 points decrease in error
    (/.f64 (Rewrite<= fma-neg_binary64 (-.f64 (*.f64 (*.f64 (*.f64 1/3 x) 1/3) x) (*.f64 (pow.f64 x 6) 1/2025))) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 12 points decrease in error
    (/.f64 (-.f64 (Rewrite<= associate-*r*_binary64 (*.f64 (*.f64 1/3 x) (*.f64 1/3 x))) (*.f64 (pow.f64 x 6) 1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 12 points increase in error, 0 points decrease in error
    (/.f64 (-.f64 (*.f64 (*.f64 1/3 x) (*.f64 1/3 x)) (*.f64 (pow.f64 x 6) 1/2025)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) (Rewrite<= metadata-eval (neg.f64 1/45))))): 0 points increase in error, 12 points decrease in error
    (/.f64 (-.f64 (*.f64 (*.f64 1/3 x) (*.f64 1/3 x)) (*.f64 (pow.f64 x 6) 1/2025)) (fma.f64 1/3 x (Rewrite<= *-commutative_binary64 (*.f64 (neg.f64 1/45) (pow.f64 x 3))))): 0 points increase in error, 12 points decrease in error
    (/.f64 (-.f64 (*.f64 (*.f64 1/3 x) (*.f64 1/3 x)) (*.f64 (pow.f64 x 6) 1/2025)) (fma.f64 1/3 x (Rewrite<= distribute-lft-neg-in_binary64 (neg.f64 (*.f64 1/45 (pow.f64 x 3)))))): 0 points increase in error, 5 points decrease in error
    (/.f64 (-.f64 (*.f64 (*.f64 1/3 x) (*.f64 1/3 x)) (*.f64 (pow.f64 x 6) 1/2025)) (Rewrite<= fma-neg_binary64 (-.f64 (*.f64 1/3 x) (*.f64 1/45 (pow.f64 x 3))))): 12 points increase in error, 0 points decrease in error
  6. Taylor expanded in x around 0 28.5

    \[\leadsto \frac{\color{blue}{0.1111111111111111 \cdot {x}^{2}}}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \]
  7. Simplified28.5

    \[\leadsto \frac{\color{blue}{x \cdot \left(x \cdot 0.1111111111111111\right)}}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \]
    Proof
    (/.f64 (*.f64 x (*.f64 x 1/9)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 0 points decrease in error
    (/.f64 (Rewrite=> associate-*r*_binary64 (*.f64 (*.f64 x x) 1/9)) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 4 points decrease in error
    (/.f64 (*.f64 (Rewrite<= unpow2_binary64 (pow.f64 x 2)) 1/9) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 4 points increase in error, 0 points decrease in error
    (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 1/9 (pow.f64 x 2))) (fma.f64 1/3 x (*.f64 (pow.f64 x 3) -1/45))): 0 points increase in error, 1 points decrease in error
  8. Applied egg-rr0.3

    \[\leadsto \color{blue}{\frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot \left(x \cdot 0.1111111111111111\right)} \]
  9. Final simplification0.3

    \[\leadsto \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, {x}^{3} \cdot -0.022222222222222223\right)} \cdot \left(x \cdot 0.1111111111111111\right) \]

Alternatives

Alternative 1
Error0.4
Cost576
\[\frac{1}{\frac{3}{x} + x \cdot -0.2} \]
Alternative 2
Error0.7
Cost320
\[\frac{1}{\frac{3}{x}} \]
Alternative 3
Error0.7
Cost192
\[x \cdot 0.3333333333333333 \]

Error

Reproduce

herbie shell --seed 2022343 
(FPCore (x)
  :name "invcot (example 3.9)"
  :precision binary64
  :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))))