Average Error: 33.2 → 9.6
Time: 31.5s
Precision: 64
Internal Precision: 128
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -4.1146996886759634 \cdot 10^{+123}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 8.585369465311041 \cdot 10^{-54}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - (\left(\frac{c}{b} \cdot a\right) \cdot -2 + b)_*}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

Original33.2
Target20.6
Herbie9.6
\[\begin{array}{l} \mathbf{if}\;b \lt 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if b < -4.1146996886759634e+123

    1. Initial program 49.1

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv49.1

      \[\leadsto \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{1}{2 \cdot a}}\]
    4. Taylor expanded around -inf 3.1

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}}\]

    if -4.1146996886759634e+123 < b < 8.585369465311041e-54

    1. Initial program 13.0

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv13.1

      \[\leadsto \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{1}{2 \cdot a}}\]
    4. Using strategy rm
    5. Applied un-div-inv13.0

      \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}}\]

    if 8.585369465311041e-54 < b

    1. Initial program 53.1

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\]
    2. Using strategy rm
    3. Applied div-inv53.1

      \[\leadsto \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{1}{2 \cdot a}}\]
    4. Using strategy rm
    5. Applied flip-+53.2

      \[\leadsto \color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}} \cdot \frac{1}{2 \cdot a}\]
    6. Applied associate-*l/53.2

      \[\leadsto \color{blue}{\frac{\left(\left(-b\right) \cdot \left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{1}{2 \cdot a}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}}\]
    7. Simplified24.6

      \[\leadsto \frac{\color{blue}{\left(4 \cdot \left(c \cdot a\right)\right) \cdot \frac{\frac{1}{2}}{a}}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\]
    8. Taylor expanded around inf 21.0

      \[\leadsto \frac{\color{blue}{2 \cdot c}}{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}\]
    9. Taylor expanded around inf 9.6

      \[\leadsto \frac{2 \cdot c}{\left(-b\right) - \color{blue}{\left(b - 2 \cdot \frac{a \cdot c}{b}\right)}}\]
    10. Simplified7.7

      \[\leadsto \frac{2 \cdot c}{\left(-b\right) - \color{blue}{(\left(\frac{c}{b} \cdot a\right) \cdot -2 + b)_*}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification9.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -4.1146996886759634 \cdot 10^{+123}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \le 8.585369465311041 \cdot 10^{-54}:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot 2}{\left(-b\right) - (\left(\frac{c}{b} \cdot a\right) \cdot -2 + b)_*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019010 +o rules:numerics
(FPCore (a b c)
  :name "The quadratic formula (r1)"

  :herbie-target
  (if (< b 0) (/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)) (/ c (* a (/ (- (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)))))

  (/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a)))

Details

Time bar (total: 30.8s)Debug log

sample164.0ms

Algorithm
intervals

simplify62.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
62.0ms
(/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a))

prune20.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 32.9b

localize35.0ms

Local error

Found 4 expressions with local error:

22.0b
(sqrt (- (* b b) (* (* 4 a) c)))
1.7b
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
0.2b
(- (* b b) (* (* 4 a) c))
0.1b
(/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a))

rewrite29.0ms

Algorithm
rewrite-expression-head
Rules
14×*-un-lft-identity
add-sqr-sqrt
add-log-exp
add-cube-cbrt
add-exp-log
add-cbrt-cube
associate-/l*
log1p-expm1-u
times-frac
fma-def
pow1
expm1-log1p-u
distribute-lft-out
sqrt-prod
associate-/l/
flip--
flip-+
associate-+l-
sqrt-div
flip3--
flip3-+
neg-sub0
div-inv
prod-diff
div-exp
div-sub
diff-log
fma-neg
pow1/2
frac-2neg
sub-neg
sum-log
neg-mul-1
associate-/r*
clear-num
rem-sqrt-square
+-commutative
cbrt-undiv
Counts
4 → 77
Calls
4 calls:
Slowest
17.0ms
(/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a))
6.0ms
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
3.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
2.0ms
(- (* b b) (* (* 4 a) c))

series214.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
70.0ms
(/ (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (* 2 a))
53.0ms
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
47.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
43.0ms
(- (* b b) (* (* 4 a) c))

simplify2.4s

Counts
40 → 89
Calls
40 calls:
Slowest
360.0ms
(- (* (- b) (- b)) (* (sqrt (- (* b b) (* (* 4 a) c))) (sqrt (- (* b b) (* (* 4 a) c)))))
348.0ms
(sqrt (- (pow (* b b) 3) (pow (* (* 4 a) c) 3)))
157.0ms
(+ (pow (- b) 3) (pow (sqrt (- (* b b) (* (* 4 a) c))) 3))
131.0ms
(/ (* (* (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))) (+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))) (* (* (* 2 a) (* 2 a)) (* 2 a)))
128.0ms
(sqrt (- (* (* b b) (* b b)) (* (* (* 4 a) c) (* (* 4 a) c))))

prune1.1s

Pruning

8 alts after pruning (8 fresh and 0 done)

Merged error: 6.3b

localize6.0ms

Local error

Found 4 expressions with local error:

22.0b
(sqrt (- (* b b) (* (* 4 a) c)))
1.7b
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
0.2b
(* (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (/ 1 (* 2 a)))
0.2b
(- (* b b) (* (* 4 a) c))

rewrite25.0ms

Algorithm
rewrite-expression-head
Rules
12×*-un-lft-identity
add-sqr-sqrt
add-log-exp
add-cube-cbrt
add-exp-log
add-cbrt-cube
pow1
log1p-expm1-u
associate-*l*
associate-*r*
fma-def
expm1-log1p-u
flip-+
sqrt-prod
flip3-+
distribute-lft-out
flip--
prod-exp
associate-*l/
sqrt-div
flip3--
frac-times
div-inv
cbrt-unprod
associate-+l-
*-commutative
un-div-inv
associate-*r/
pow-prod-down
prod-diff
diff-log
fma-neg
pow1/2
sub-neg
sum-log
neg-mul-1
rec-exp
rem-sqrt-square
+-commutative
neg-sub0
Counts
4 → 79
Calls
4 calls:
Slowest
12.0ms
(* (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (/ 1 (* 2 a)))
6.0ms
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
3.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
2.0ms
(- (* b b) (* (* 4 a) c))

series276.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
103.0ms
(* (+ (- b) (sqrt (- (* b b) (* (* 4 a) c)))) (/ 1 (* 2 a)))
72.0ms
(+ (- b) (sqrt (- (* b b) (* (* 4 a) c))))
56.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
45.0ms
(- (* b b) (* (* 4 a) c))

simplify4.2s

Counts
43 → 91
Calls
43 calls:
Slowest
502.0ms
(* (- (* (- b) (- b)) (* (sqrt (- (* b b) (* (* 4 a) c))) (sqrt (- (* b b) (* (* 4 a) c))))) 1)
453.0ms
(* (+ (pow (- b) 3) (pow (sqrt (- (* b b) (* (* 4 a) c))) 3)) 1)
441.0ms
(sqrt (- (pow (* b b) 3) (pow (* (* 4 a) c) 3)))
372.0ms
(* (+ (* (- b) (- b)) (- (* (sqrt (- (* b b) (* (* 4 a) c))) (sqrt (- (* b b) (* (* 4 a) c)))) (* (- b) (sqrt (- (* b b) (* (* 4 a) c)))))) (* 2 a))
368.0ms
(* (- (* (- b) (- b)) (* (sqrt (- (* b b) (* (* 4 a) c))) (sqrt (- (* b b) (* (* 4 a) c))))) (/ 1 (* 2 a)))

prune1.1s

Pruning

8 alts after pruning (8 fresh and 0 done)

Merged error: 6.2b

localize29.0ms

Local error

Found 4 expressions with local error:

22.0b
(sqrt (- (* b b) (* (* 4 a) c)))
14.3b
(* (* 4 (* c a)) (/ 1/2 a))
0.3b
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
0.2b
(- (* b b) (* (* 4 a) c))

rewrite34.0ms

Algorithm
rewrite-expression-head
Rules
26×add-sqr-sqrt
25×prod-diff
23×*-un-lft-identity
21×add-cube-cbrt
16×sqrt-prod
add-log-exp
neg-mul-1
add-exp-log
add-cbrt-cube
pow1
fma-neg
log1p-expm1-u
associate-*r*
expm1-log1p-u
flip--
flip3--
difference-of-squares
diff-log
sqrt-div
sub-neg
distribute-lft-out--
div-inv
cbrt-unprod
*-commutative
associate-*r/
prod-exp
pow-prod-down
associate-*l*
pow1/2
rem-sqrt-square
neg-sub0
associate--l-
Counts
4 → 97
Calls
4 calls:
Slowest
14.0ms
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
8.0ms
(* (* 4 (* c a)) (/ 1/2 a))
5.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
5.0ms
(- (* b b) (* (* 4 a) c))

series152.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
55.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
53.0ms
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
38.0ms
(- (* b b) (* (* 4 a) c))
5.0ms
(* (* 4 (* c a)) (/ 1/2 a))

simplify5.4s

Counts
84 → 109
Calls
84 calls:
Slowest
363.0ms
(sqrt (- (pow (* b b) 3) (pow (* (* 4 a) c) 3)))
300.0ms
(+ (log (* 4 (* c a))) (log (/ 1/2 a)))
232.0ms
(sqrt (- (* (* b b) (* b b)) (* (* (* 4 a) c) (* (* 4 a) c))))
217.0ms
(* (* 4 (* c a)) (/ 1/2 a))
136.0ms
(- b (* 2 (/ (* a c) b)))

prune1.4s

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 6.2b

localize12.0ms

Local error

Found 4 expressions with local error:

22.0b
(sqrt (- (* b b) (* (* 4 a) c)))
0.3b
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
0.2b
(- (* b b) (* (* 4 a) c))
0.1b
(/ (* 2 c) (- (- b) (sqrt (- (* b b) (* (* 4 a) c)))))

rewrite53.0ms

Algorithm
rewrite-expression-head
Rules
35×add-sqr-sqrt
28×*-un-lft-identity
25×prod-diff
22×add-cube-cbrt
18×sqrt-prod
add-log-exp
neg-mul-1
difference-of-squares
add-exp-log
add-cbrt-cube
times-frac
associate-/r*
fma-neg
flip--
log1p-expm1-u
flip3--
pow1
expm1-log1p-u
distribute-lft-out--
associate-/r/
diff-log
sqrt-div
sub-neg
associate-/l*
div-inv
div-exp
pow1/2
frac-2neg
clear-num
rem-sqrt-square
cbrt-undiv
neg-sub0
associate--l-
Counts
4 → 107
Calls
4 calls:
Slowest
25.0ms
(/ (* 2 c) (- (- b) (sqrt (- (* b b) (* (* 4 a) c)))))
15.0ms
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
6.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
4.0ms
(- (* b b) (* (* 4 a) c))

series320.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
152.0ms
(/ (* 2 c) (- (- b) (sqrt (- (* b b) (* (* 4 a) c)))))
68.0ms
(sqrt (- (* b b) (* (* 4 a) c)))
52.0ms
(- (- b) (sqrt (- (* b b) (* (* 4 a) c))))
47.0ms
(- (* b b) (* (* 4 a) c))

simplify5.9s

Counts
99 → 119
Calls
99 calls:
Slowest
455.0ms
(+ (* (- b) (- b)) (+ (* (sqrt (- (* b b) (* (* 4 a) c))) (sqrt (- (* b b) (* (* 4 a) c)))) (* (- b) (sqrt (- (* b b) (* (* 4 a) c))))))
330.0ms
(sqrt (- (pow (* b b) 3) (pow (* (* 4 a) c) 3)))
135.0ms
(- b (* 2 (/ (* a c) b)))
121.0ms
(sqrt (- (* (* b b) (* b b)) (* (* (* 4 a) c) (* (* 4 a) c))))
112.0ms
(fma 1 (- b) (- (* (cbrt (sqrt (- (* b b) (* (* 4 a) c)))) (* (cbrt (sqrt (- (* b b) (* (* 4 a) c)))) (cbrt (sqrt (- (* b b) (* (* 4 a) c))))))))

prune1.3s

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 6.2b

regimes547.0ms

Accuracy

84.2% (4.2b remaining)

Error of 9.6b against oracle of 5.5b and baseline of 31.8b

bsearch810.0ms

end0.0ms

sample5.3s

Algorithm
intervals