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

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. Using strategy rm
  5. Applied fma-udef0.3

    \[\leadsto \color{blue}{(\frac{1}{45} \cdot \left(x \cdot x\right) + \frac{1}{3})_* \cdot x + {x}^{5} \cdot \frac{2}{945}}\]
  6. Final simplification0.3

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

Reproduce

herbie shell --seed 2019016 +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: 32.4s)Debug log

sample505.0ms

Algorithm
intervals
Results
256×(pre true 80)
99×(body real 2560)
79×(body real 1280)
35×(body real 640)
28×(body real 320)
12×(body real 160)
(body real 80)

simplify9.0ms

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

prune8.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 59.7b

localize23.0ms

Local error

Found 2 expressions with local error:

3.7b
(- (/ 1 x) (/ 1 (tan x)))
0.0b
(/ 1 (tan x))

rewrite11.0ms

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

series131.0ms

Counts
2 → 6
Calls
2 calls:
Slowest
88.0ms
(- (/ 1 x) (/ 1 (tan x)))
43.0ms
(/ 1 (tan x))

simplify1.9s

Counts
62 → 69
Calls
62 calls:
Slowest
205.0ms
(- (/ 1 x) (+ (* 1/3 x) (* 1/45 (pow x 3))))
120.0ms
(fma (- (cos x)) (/ 1 (sin x)) (* (cos x) (/ 1 (sin x))))
106.0ms
(fma 1 (/ 1 x) (- (* (cos x) (/ 1 (sin x)))))
99.0ms
(fma (* (cbrt (/ 1 x)) (cbrt (/ 1 x))) (cbrt (/ 1 x)) (- (* (cos x) (/ 1 (sin x)))))
99.0ms
(fma (- (cos x)) (/ 1 (sin x)) (* (cos x) (/ 1 (sin x))))

prune565.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 0.3b

localize33.0ms

Local error

Found 3 expressions with local error:

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

rewrite6.0ms

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

series118.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
45.0ms
(fma (fma 1/45 (* x x) 1/3) x (* (pow x 5) 2/945))
37.0ms
(fma 1/45 (* x x) 1/3)
36.0ms
(* (pow x 5) 2/945)

simplify259.0ms

Counts
11 → 42
Calls
11 calls:
Slowest
83.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))))
51.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
16.0ms
(+ (* 1/45 (pow x 2)) 1/3)
14.0ms
(+ (* 1/45 (pow x 2)) 1/3)

prune425.0ms

Pruning

5 alts after pruning (5 fresh and 0 done)

Merged error: 0.2b

localize6.0ms

Local error

Found 4 expressions with local error:

0.3b
(* (fma 1/45 (* x x) 1/3) x)
0.1b
(* (pow x 5) 2/945)
0.0b
(fma 1/45 (* x x) 1/3)
0.0b
(+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945))

rewrite11.0ms

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

series114.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
38.0ms
(* (fma 1/45 (* x x) 1/3) x)
29.0ms
(+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945))
27.0ms
(fma 1/45 (* x x) 1/3)
20.0ms
(* (pow x 5) 2/945)

simplify490.0ms

Counts
18 → 62
Calls
18 calls:
Slowest
95.0ms
(* (exp (* (fma 1/45 (* x x) 1/3) x)) (exp (* (pow x 5) 2/945)))
89.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
77.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
73.0ms
(+ (* 1/3 x) (+ (* 1/45 (pow x 3)) (* 2/945 (pow x 5))))
39.0ms
(+ (* 1/3 x) (* 1/45 (pow x 3)))

prune547.0ms

Pruning

5 alts after pruning (4 fresh and 1 done)

Merged error: 0.2b

localize11.0ms

Local error

Found 4 expressions with local error:

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

rewrite44.0ms

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

series600.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
175.0ms
(* (cbrt (+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945))) (cbrt (+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945))))
168.0ms
(cbrt (+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945)))
131.0ms
(cbrt (+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945)))
126.0ms
(cbrt (+ (* (fma 1/45 (* x x) 1/3) x) (* (pow x 5) 2/945)))

simplify9.8s

Counts
59 → 95
Calls
59 calls:
Slowest
536.0ms
(* (cbrt (- (* (* (fma 1/45 (* x x) 1/3) x) (* (fma 1/45 (* x x) 1/3) x)) (* (* (pow x 5) 2/945) (* (pow x 5) 2/945)))) (cbrt (+ (pow (* (fma 1/45 (* x x) 1/3) x) 3) (pow (* (pow x 5) 2/945) 3))))
525.0ms
(cbrt (- (* (* (fma 1/45 (* x x) 1/3) x) (* (fma 1/45 (* x x) 1/3) x)) (* (* (pow x 5) 2/945) (* (pow x 5) 2/945))))
517.0ms
(* (cbrt (+ (pow (* (fma 1/45 (* x x) 1/3) x) 3) (pow (* (pow x 5) 2/945) 3))) (cbrt (- (* (* (fma 1/45 (* x x) 1/3) x) (* (fma 1/45 (* x x) 1/3) x)) (* (* (pow x 5) 2/945) (* (pow x 5) 2/945)))))
491.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)))))
459.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)))))

prune1.1s

Pruning

14 alts after pruning (12 fresh and 2 done)

Merged error: 0.1b

regimes129.0ms

Accuracy

0% (0.2b remaining)

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

bsearch2.0ms

end0.0ms

sample15.6s

Algorithm
intervals
Results
8000×(pre true 80)
3231×(body real 2560)
2519×(body real 1280)
1261×(body real 640)
604×(body real 320)
342×(body real 160)
43×(body real 80)