Average Error: 33.5 → 7.5
Time: 22.9s
Precision: 64
Internal Precision: 128
\[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
\[\begin{array}{l} \mathbf{if}\;b \le -2.8918891030401806 \cdot 10^{+33}:\\ \;\;\;\;\frac{-c}{b}\\ \mathbf{elif}\;b \le -1.882722696756613 \cdot 10^{-300}:\\ \;\;\;\;\frac{c}{2} \cdot \frac{4}{\sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*} - b}\\ \mathbf{elif}\;b \le 1.501809486178084 \cdot 10^{+23}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Target

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

Derivation

  1. Split input into 4 regimes
  2. if b < -2.8918891030401806e+33

    1. Initial program 56.3

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified56.3

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}}\]
    4. Simplified4.3

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

    if -2.8918891030401806e+33 < b < -1.882722696756613e-300

    1. Initial program 29.1

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified29.1

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

      \[\leadsto \frac{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*} \cdot \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}{\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}}}{2 \cdot a}\]
    5. Applied associate-/l/34.3

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

      \[\leadsto \frac{\color{blue}{\left(c \cdot a\right) \cdot 4}}{\left(2 \cdot a\right) \cdot \left(\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}\right)}\]
    7. Using strategy rm
    8. Applied times-frac17.3

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

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

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

    if -1.882722696756613e-300 < b < 1.501809486178084e+23

    1. Initial program 9.9

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified9.9

      \[\leadsto \color{blue}{\frac{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}{2 \cdot a}}\]
    3. Taylor expanded around 0 9.9

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

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

    if 1.501809486178084e+23 < b

    1. Initial program 32.7

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\]
    2. Simplified32.7

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

      \[\leadsto \frac{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*} \cdot \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}{\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}}}{2 \cdot a}\]
    5. Applied associate-/l/60.0

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

      \[\leadsto \frac{\color{blue}{\left(c \cdot a\right) \cdot 4}}{\left(2 \cdot a\right) \cdot \left(\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}\right)}\]
    7. Using strategy rm
    8. Applied times-frac59.3

      \[\leadsto \color{blue}{\frac{c \cdot a}{2 \cdot a} \cdot \frac{4}{\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}}\]
    9. Simplified59.1

      \[\leadsto \color{blue}{\frac{c}{2}} \cdot \frac{4}{\left(-b\right) + \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}\]
    10. Simplified59.1

      \[\leadsto \frac{c}{2} \cdot \color{blue}{\frac{4}{\sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*} - b}}\]
    11. Taylor expanded around 0 6.3

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}}\]
    12. Simplified6.3

      \[\leadsto \color{blue}{\frac{-b}{a}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification7.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \le -2.8918891030401806 \cdot 10^{+33}:\\ \;\;\;\;\frac{-c}{b}\\ \mathbf{elif}\;b \le -1.882722696756613 \cdot 10^{-300}:\\ \;\;\;\;\frac{c}{2} \cdot \frac{4}{\sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*} - b}\\ \mathbf{elif}\;b \le 1.501809486178084 \cdot 10^{+23}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{(\left(-4 \cdot a\right) \cdot c + \left(b \cdot b\right))_*}}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array}\]

Reproduce

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

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

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

Details

Time bar (total: 22.1s)Debug log

sample160.0ms

Algorithm
intervals
Results
34.0ms24×body1280valid
33.0ms32×body2560valid
33.0ms188×body80valid
15.0ms96×body80nan
11.0ms17×body640valid
7.0msbody5120valid
4.0msbody320valid
2.0msbody160valid

simplify106.0ms

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

prune20.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 33.7b

localize29.0ms

Local error

Found 4 expressions with local error:

22.4b
(sqrt (fma (* -4 a) c (* b b)))
1.1b
(- (- b) (sqrt (fma (* -4 a) c (* b b))))
0.1b
(/ (- (- b) (sqrt (fma (* -4 a) c (* b b)))) (* 2 a))
0.0b
(fma (* -4 a) c (* b b))

rewrite27.0ms

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

series576.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
424.0ms
(/ (- (- b) (sqrt (fma (* -4 a) c (* b b)))) (* 2 a))
58.0ms
(- (- b) (sqrt (fma (* -4 a) c (* b b))))
50.0ms
(sqrt (fma (* -4 a) c (* b b)))
44.0ms
(fma (* -4 a) c (* b b))

simplify3.4s

Counts
93 → 112
Calls
93 calls:
Slowest
334.0ms
(- (* (- b) (- b)) (* (sqrt (fma (* -4 a) c (* b b))) (sqrt (fma (* -4 a) c (* b b)))))
191.0ms
(- (pow (- b) 3) (pow (sqrt (fma (* -4 a) c (* b b))) 3))
97.0ms
(- (* 2 (/ (* a c) b)) (* 2 b))
76.0ms
(fma 1 (- b) (- (* (sqrt (fma (* -4 a) c (* b b))) 1)))
73.0ms
(/ (* (* (- (- b) (sqrt (fma (* -4 a) c (* b b)))) (- (- b) (sqrt (fma (* -4 a) c (* b b))))) (- (- b) (sqrt (fma (* -4 a) c (* b b))))) (* (* (* 2 a) (* 2 a)) (* 2 a)))

prune1.3s

Pruning

9 alts after pruning (9 fresh and 0 done)

Merged error: 5.5b

localize13.0ms

Local error

Found 4 expressions with local error:

22.4b
(sqrt (fma (* -4 a) c (* b b)))
14.0b
(/ (* (* c a) 4) (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b))))))
2.0b
(+ (- b) (sqrt (fma (* -4 a) c (* b b))))
0.1b
(* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))

rewrite30.0ms

Algorithm
rewrite-expression-head
Rules
11×add-exp-log
11×add-cbrt-cube
11×*-un-lft-identity
add-sqr-sqrt
add-cube-cbrt
add-log-exp
pow1
log1p-expm1-u
associate-*r/
associate-*r*
fma-def
expm1-log1p-u
flip-+
sqrt-prod
flip3-+
distribute-lft-out
cbrt-unprod
associate-/r/
prod-exp
div-exp
cbrt-undiv
associate-/l*
div-inv
associate-+l-
*-commutative
pow-prod-down
associate-*l*
pow1/2
times-frac
distribute-lft-in
frac-2neg
sum-log
neg-mul-1
associate-/r*
clear-num
rem-sqrt-square
+-commutative
distribute-rgt-in
neg-sub0
Counts
4 → 76
Calls
4 calls:
Slowest
16.0ms
(/ (* (* c a) 4) (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b))))))
7.0ms
(* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))
4.0ms
(+ (- b) (sqrt (fma (* -4 a) c (* b b))))
1.0ms
(sqrt (fma (* -4 a) c (* b b)))

series330.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
160.0ms
(/ (* (* c a) 4) (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b))))))
77.0ms
(* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))
49.0ms
(+ (- b) (sqrt (fma (* -4 a) c (* b b))))
43.0ms
(sqrt (fma (* -4 a) c (* b b)))

simplify4.5s

Counts
41 → 88
Calls
41 calls:
Slowest
869.0ms
(/ (* (* (* (* c a) 4) (* (* c a) 4)) (* (* c a) 4)) (* (* (* (* 2 a) (* 2 a)) (* 2 a)) (* (* (+ (- b) (sqrt (fma (* -4 a) c (* b b)))) (+ (- b) (sqrt (fma (* -4 a) c (* b b))))) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))))
721.0ms
(/ (* (* (* (* c a) 4) (* (* c a) 4)) (* (* c a) 4)) (* (* (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b))))) (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))) (* (* 2 a) (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))))
633.0ms
(- (log (* (* c a) 4)) (+ (log (* 2 a)) (log (+ (- b) (sqrt (fma (* -4 a) c (* b b)))))))
451.0ms
(+ (* (- b) (- b)) (- (* (sqrt (fma (* -4 a) c (* b b))) (sqrt (fma (* -4 a) c (* b b)))) (* (- b) (sqrt (fma (* -4 a) c (* b b))))))
416.0ms
(+ (* (- b) (- b)) (- (* (sqrt (fma (* -4 a) c (* b b))) (sqrt (fma (* -4 a) c (* b b)))) (* (- b) (sqrt (fma (* -4 a) c (* b b))))))

prune1.1s

Pruning

7 alts after pruning (7 fresh and 0 done)

Merged error: 5.5b

localize21.0ms

Local error

Found 4 expressions with local error:

22.4b
(sqrt (fma (* -4 a) c (* b b)))
2.0b
(- (sqrt (fma (* -4 a) c (* b b))) b)
0.2b
(* (/ c 2) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b)))
0.1b
(/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))

rewrite26.0ms

Algorithm
rewrite-expression-head
Rules
11×add-sqr-sqrt
10×add-cube-cbrt
10×*-un-lft-identity
sqrt-prod
add-exp-log
associate-*r*
fma-neg
add-cbrt-cube
pow1
add-log-exp
log1p-expm1-u
associate-/r/
associate-*l*
expm1-log1p-u
flip--
div-inv
flip3--
associate-/r*
cbrt-unprod
*-commutative
associate-*r/
prod-exp
associate-*l/
pow-prod-down
pow1/2
frac-2neg
sub-neg
clear-num
rem-sqrt-square
frac-times
Counts
4 → 76
Calls
4 calls:
Slowest
10.0ms
(- (sqrt (fma (* -4 a) c (* b b))) b)
8.0ms
(* (/ c 2) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b)))
5.0ms
(/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))
2.0ms
(sqrt (fma (* -4 a) c (* b b)))

series431.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
181.0ms
(* (/ c 2) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b)))
131.0ms
(/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))
64.0ms
(- (sqrt (fma (* -4 a) c (* b b))) b)
55.0ms
(sqrt (fma (* -4 a) c (* b b)))

simplify1.2s

Counts
41 → 88
Calls
41 calls:
Slowest
341.0ms
(* (* (* (/ c 2) (/ c 2)) (/ c 2)) (* (* (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b)) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))))
164.0ms
(+ (log (/ c 2)) (log (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b))))
82.0ms
(+ (* (sqrt (fma (* -4 a) c (* b b))) (sqrt (fma (* -4 a) c (* b b)))) (+ (* b b) (* (sqrt (fma (* -4 a) c (* b b))) b)))
77.0ms
(* (/ c 2) (/ 4 (- (sqrt (fma (* -4 a) c (* b b))) b)))
70.0ms
(+ (* (sqrt (fma (* -4 a) c (* b b))) (sqrt (fma (* -4 a) c (* b b)))) (+ (* b b) (* (sqrt (fma (* -4 a) c (* b b))) b)))

prune1.1s

Pruning

7 alts after pruning (6 fresh and 1 done)

Merged error: 5.5b

localize32.0ms

Local error

Found 4 expressions with local error:

22.4b
(sqrt (fma -4 (* a c) (* b b)))
13.0b
(/ (* (* c a) 4) (* 2 a))
2.0b
(- (sqrt (fma -4 (* a c) (* b b))) b)
0.4b
(fma -4 (* a c) (* b b))

rewrite27.0ms

Algorithm
rewrite-expression-head
Rules
add-sqr-sqrt
add-cube-cbrt
*-un-lft-identity
sqrt-prod
add-exp-log
fma-neg
add-cbrt-cube
add-log-exp
log1p-expm1-u
pow1
expm1-log1p-u
associate-/l*
flip--
div-inv
fma-udef
div-exp
pow1/2
flip3--
times-frac
frac-2neg
sub-neg
associate-/r*
clear-num
rem-sqrt-square
cbrt-undiv
Counts
4 → 60
Calls
4 calls:
Slowest
15.0ms
(/ (* (* c a) 4) (* 2 a))
10.0ms
(- (sqrt (fma -4 (* a c) (* b b))) b)
2.0ms
(sqrt (fma -4 (* a c) (* b b)))
0.0ms
(fma -4 (* a c) (* b b))

series171.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
64.0ms
(sqrt (fma -4 (* a c) (* b b)))
51.0ms
(fma -4 (* a c) (* b b))
49.0ms
(- (sqrt (fma -4 (* a c) (* b b))) b)
8.0ms
(/ (* (* c a) 4) (* 2 a))

simplify1.0s

Counts
26 → 72
Calls
26 calls:
Slowest
472.0ms
(- (log (* (* c a) 4)) (log (* 2 a)))
270.0ms
(/ (* (* (* (* c a) 4) (* (* c a) 4)) (* (* c a) 4)) (* (* (* 2 a) (* 2 a)) (* 2 a)))
49.0ms
(- (pow b 2) (* 4 (* a c)))
49.0ms
(- (pow b 2) (* 4 (* a c)))
41.0ms
(- (pow b 2) (* 4 (* a c)))

prune883.0ms

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 5.5b

regimes370.0ms

Accuracy

91.6% (2.0b remaining)

Error of 7.5b against oracle of 5.5b and baseline of 29.3b

bsearch326.0ms

end0.0ms

sample4.8s

Algorithm
intervals
Results
1.6s1063×body2560valid
1.2s937×body1280valid
870.0ms5849×body80valid
404.0ms2975×body80nan
324.0ms444×body640valid
122.0ms123×body5120valid
76.0ms203×body320valid
29.0ms111×body160valid