Average Error: 59.9 → 0.3
Time: 33.7s
Precision: 64
Internal Precision: 128
\[\frac{1}{x} - \frac{1}{\tan x}\]
\[(\left((\frac{1}{45} \cdot \left(x \cdot x\right) + \frac{1}{3})_*\right) \cdot x + \left(\frac{2}{945} \cdot {x}^{5}\right))_*\]

Error

Bits error versus x

Target

Original59.9
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 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}{(\left((\frac{1}{45} \cdot \left(x \cdot x\right) + \frac{1}{3})_*\right) \cdot x + \left({x}^{5} \cdot \frac{2}{945}\right))_*}\]
  4. Final simplification0.3

    \[\leadsto (\left((\frac{1}{45} \cdot \left(x \cdot x\right) + \frac{1}{3})_*\right) \cdot x + \left(\frac{2}{945} \cdot {x}^{5}\right))_*\]

Reproduce

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

Details

Time bar (total: 31.9s)Debug log

sample466.0ms

Algorithm
intervals

simplify10.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
10.0ms
(- (/ 1 x) (/ 1 (tan x)))

prune36.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 60.2b

localize13.0ms

Local error

Found 1 expressions with local error:

2.1b
(- (/ 1 x) (/ 1 (tan x)))

rewrite5.0ms

Algorithm
rewrite-expression-head
Rules
20×prod-diff
15×*-un-lft-identity
14×div-inv
13×add-sqr-sqrt
11×add-cube-cbrt
distribute-lft-out--
associate-/r/
tan-quot
fma-neg
add-log-exp
difference-of-squares
flip--
log1p-expm1-u
frac-sub
add-exp-log
diff-log
add-cbrt-cube
flip3--
sub-neg
pow1
expm1-log1p-u
Counts
1 → 44
Calls
1 calls:
Slowest
4.0ms
(- (/ 1 x) (/ 1 (tan x)))

series90.0ms

Counts
1 → 3
Calls
1 calls:
Slowest
90.0ms
(- (/ 1 x) (/ 1 (tan x)))

simplify1.7s

Counts
54 → 47
Calls
54 calls:
Slowest
122.0ms
(fma (- (cos x)) (/ 1 (sin x)) (* (cos x) (/ 1 (sin x))))
114.0ms
(fma (- (cos x)) (/ 1 (sin x)) (* (cos x) (/ 1 (sin x))))
94.0ms
(fma (sqrt (/ 1 x)) (sqrt (/ 1 x)) (- (* (cos x) (/ 1 (sin x)))))
84.0ms
(fma 1 (/ 1 x) (- (* (cos x) (/ 1 (sin x)))))
84.0ms
(fma 1 (/ 1 x) (- (* (cos x) (/ 1 (sin x)))))

prune341.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 0.4b

localize18.0ms

Local error

Found 2 expressions with local error:

0.4b
(fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))
0.1b
(* (pow x 5) 2/945)

rewrite3.0ms

Algorithm
rewrite-expression-head
Rules
add-cube-cbrt
associate-*l*
*-un-lft-identity
add-sqr-sqrt
add-log-exp
log1p-expm1-u
add-exp-log
add-cbrt-cube
pow1
expm1-log1p-u
fma-udef
*-commutative
Counts
2 → 23
Calls
2 calls:
Slowest
2.0ms
(* (pow x 5) 2/945)
0.0ms
(fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))

series46.0ms

Counts
2 → 6
Calls
2 calls:
Slowest
23.0ms
(* (pow x 5) 2/945)
23.0ms
(fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))

simplify260.0ms

Counts
8 → 29
Calls
8 calls:
Slowest
93.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
84.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
65.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
4.0ms
(* 2/945 (pow x 5))
4.0ms
(* 2/945 (pow x 5))

prune266.0ms

Pruning

4 alts after pruning (3 fresh and 1 done)

Merged error: 0.3b

localize10.0ms

Local error

Found 4 expressions with local error:

0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
0.5b
(* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))))

rewrite8.0ms

Algorithm
rewrite-expression-head
Rules
15×cbrt-prod
11×add-cube-cbrt
11×*-un-lft-identity
11×add-sqr-sqrt
pow1
pow1/3
add-exp-log
associate-*l*
associate-*r*
add-log-exp
log1p-expm1-u
add-cbrt-cube
expm1-log1p-u
pow-prod-up
pow-prod-down
cbrt-unprod
*-commutative
prod-exp
pow-plus
pow2
Counts
4 → 69
Calls
4 calls:
Slowest
5.0ms
(* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))

series575.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
161.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
142.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
140.0ms
(* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))))
132.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))

simplify5.5s

Counts
41 → 81
Calls
41 calls:
Slowest
418.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
408.0ms
(+ (exp (* 1/3 (- (log 4/893025) (* 10 (log (/ 1 x)))))) (+ (* 371/4 (/ (exp (* 1/3 (- (log 4/893025) (* 10 (log (/ 1 x)))))) (pow x 4))) (* 7 (/ (exp (* 1/3 (- (log 4/893025) (* 10 (log (/ 1 x)))))) (pow x 2)))))
382.0ms
(+ (* 2/45 (* (pow x 2) (exp (* 1/3 (+ (log 1/9) (* 2 (log x))))))) (+ (* 53/14175 (* (pow x 4) (exp (* 1/3 (+ (log 1/9) (* 2 (log x))))))) (exp (* 1/3 (+ (log 1/9) (* 2 (log x)))))))
373.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
361.0ms
(+ (exp (* 1/3 (+ (log 1/3) (log x)))) (+ (* 23/14175 (* (pow x 4) (exp (* 1/3 (+ (log 1/3) (log x)))))) (* 1/45 (* (pow x 2) (exp (* 1/3 (+ (log 1/3) (log x))))))))

prune825.0ms

Pruning

12 alts after pruning (11 fresh and 1 done)

Merged error: 0.2b

localize19.0ms

Local error

Found 4 expressions with local error:

0.6b
(cbrt (* (* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))))
0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
0.6b
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))

rewrite15.0ms

Algorithm
rewrite-expression-head
Rules
10×cbrt-prod
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
add-log-exp
log1p-expm1-u
add-exp-log
pow1/3
add-cbrt-cube
pow1
expm1-log1p-u
rem-cbrt-cube
pow3
Counts
4 → 51
Calls
4 calls:
Slowest
13.0ms
(cbrt (* (* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
1.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))

series512.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
135.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
134.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))
132.0ms
(cbrt (* (* (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))) (cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))))
111.0ms
(cbrt (fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945)))

simplify4.6s

Counts
23 → 63
Calls
23 calls:
Slowest
819.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
460.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
419.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
393.0ms
(+ (* 7/2 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 2))) (+ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (* 161/4 (/ (exp (* 1/3 (- (log 2/945) (* 5 (log (/ 1 x)))))) (pow x 4)))))
342.0ms
(+ (exp (* 1/3 (+ (log 1/3) (log x)))) (+ (* 23/14175 (* (pow x 4) (exp (* 1/3 (+ (log 1/3) (log x)))))) (* 1/45 (* (pow x 2) (exp (* 1/3 (+ (log 1/3) (log x))))))))

prune687.0ms

Pruning

14 alts after pruning (12 fresh and 2 done)

Merged error: 0.2b

regimes111.0ms

Accuracy

0% (0.1b remaining)

Error of 0.3b against oracle of 0.2b and baseline of 0.3b

bsearch2.0ms

end0.0ms

sample15.8s

Algorithm
intervals