Average Error: 59.9 → 0.3
Time: 13.5s
Precision: binary64
Cost: 39744
\[-0.026 < x \land x < 0.026\]
\[\frac{1}{x} - \frac{1}{\tan x} \]
\[\mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \sqrt[3]{0.0004938271604938272} \cdot \left(x \cdot -3\right), \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\right) \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x)
 :precision binary64
 (fma
  (cbrt (* 2.438652644413961e-7 (pow x 12.0)))
  (* (cbrt 0.0004938271604938272) (* x -3.0))
  (*
   (/ x (fma 0.022222222222222223 (pow x 3.0) (* x -0.3333333333333333)))
   (* x -0.1111111111111111))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
	return fma(cbrt((2.438652644413961e-7 * pow(x, 12.0))), (cbrt(0.0004938271604938272) * (x * -3.0)), ((x / fma(0.022222222222222223, pow(x, 3.0), (x * -0.3333333333333333))) * (x * -0.1111111111111111)));
}
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function code(x)
	return fma(cbrt(Float64(2.438652644413961e-7 * (x ^ 12.0))), Float64(cbrt(0.0004938271604938272) * Float64(x * -3.0)), Float64(Float64(x / fma(0.022222222222222223, (x ^ 3.0), Float64(x * -0.3333333333333333))) * 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[Power[N[(2.438652644413961e-7 * N[Power[x, 12.0], $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] * N[(N[Power[0.0004938271604938272, 1/3], $MachinePrecision] * N[(x * -3.0), $MachinePrecision]), $MachinePrecision] + N[(N[(x / N[(0.022222222222222223 * N[Power[x, 3.0], $MachinePrecision] + N[(x * -0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(x * -0.1111111111111111), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \sqrt[3]{0.0004938271604938272} \cdot \left(x \cdot -3\right), \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\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. Applied egg-rr29.2

    \[\leadsto \color{blue}{\frac{{x}^{6} \cdot 0.0004938271604938272 - \left(x \cdot x\right) \cdot 0.1111111111111111}{0.022222222222222223 \cdot {x}^{3} - x \cdot 0.3333333333333333}} \]
  4. Applied egg-rr0.2

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}}{1}, \frac{\left(x \cdot x\right) \cdot \sqrt[3]{0.0004938271604938272}}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)}, -\frac{x}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)}{x \cdot 0.1111111111111111}}\right)} \]
  5. Simplified0.3

    \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \frac{x}{\frac{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)}{x}} \cdot \sqrt[3]{0.0004938271604938272}, \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\right)} \]
    Proof
    (fma.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) (*.f64 (/.f64 x (/.f64 (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3)) x)) (cbrt.f64 1/2025)) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x -1/9))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (Rewrite<= /-rgt-identity_binary64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1)) (*.f64 (/.f64 x (/.f64 (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3)) x)) (cbrt.f64 1/2025)) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x -1/9))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (*.f64 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 x x) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3)))) (cbrt.f64 1/2025)) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x -1/9))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 (*.f64 x x) (cbrt.f64 1/2025)) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3)))) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x -1/9))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (/.f64 (*.f64 (*.f64 x x) (cbrt.f64 1/2025)) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x (Rewrite<= metadata-eval (neg.f64 1/9))))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (/.f64 (*.f64 (*.f64 x x) (cbrt.f64 1/2025)) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 x 1/9))))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (/.f64 (*.f64 (*.f64 x x) (cbrt.f64 1/2025)) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 (/.f64 x (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (*.f64 x 1/9))))): 0 points increase in error, 0 points decrease in error
    (fma.f64 (/.f64 (cbrt.f64 (*.f64 1/4100625 (pow.f64 x 12))) 1) (/.f64 (*.f64 (*.f64 x x) (cbrt.f64 1/2025)) (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3))) (neg.f64 (Rewrite<= associate-/r/_binary64 (/.f64 x (/.f64 (fma.f64 1/45 (pow.f64 x 3) (*.f64 x -1/3)) (*.f64 x 1/9)))))): 32 points increase in error, 30 points decrease in error
  6. Taylor expanded in x around 0 0.3

    \[\leadsto \mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \color{blue}{-3 \cdot \left(\sqrt[3]{0.0004938271604938272} \cdot x\right)}, \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\right) \]
  7. Simplified0.3

    \[\leadsto \mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \color{blue}{\sqrt[3]{0.0004938271604938272} \cdot \left(x \cdot -3\right)}, \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\right) \]
    Proof
    (*.f64 (cbrt.f64 1/2025) (*.f64 x -3)): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 (cbrt.f64 1/2025) x) -3)): 48 points increase in error, 61 points decrease in error
    (Rewrite<= *-commutative_binary64 (*.f64 -3 (*.f64 (cbrt.f64 1/2025) x))): 0 points increase in error, 0 points decrease in error
  8. Final simplification0.3

    \[\leadsto \mathsf{fma}\left(\sqrt[3]{2.438652644413961 \cdot 10^{-7} \cdot {x}^{12}}, \sqrt[3]{0.0004938271604938272} \cdot \left(x \cdot -3\right), \frac{x}{\mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)} \cdot \left(x \cdot -0.1111111111111111\right)\right) \]

Alternatives

Alternative 1
Error0.3
Cost33536
\[\begin{array}{l} t_0 := \mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)\\ 0.0004938271604938272 \cdot \frac{{x}^{6}}{t_0} - 0.1111111111111111 \cdot \frac{x}{\frac{t_0}{x}} \end{array} \]
Alternative 2
Error0.2
Cost33536
\[\begin{array}{l} t_0 := \mathsf{fma}\left(0.022222222222222223, {x}^{3}, x \cdot -0.3333333333333333\right)\\ 0.0004938271604938272 \cdot \frac{{x}^{6}}{t_0} - \frac{x}{\frac{t_0}{x \cdot 0.1111111111111111}} \end{array} \]
Alternative 3
Error0.4
Cost6912
\[x \cdot 0.3333333333333333 + 0.022222222222222223 \cdot {x}^{3} \]
Alternative 4
Error0.7
Cost192
\[x \cdot 0.3333333333333333 \]

Error

Reproduce

herbie shell --seed 2022329 
(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))))