| Alternative 1 | |
|---|---|
| Error | 10.5 |
| Cost | 7624 |
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
(FPCore (a b c)
:precision binary64
(if (<= b -1.82e+40)
(/ (- b) a)
(if (<= b 5.4e-31)
(/ 0.5 (/ a (- (hypot b (sqrt (* a (* c -4.0)))) b)))
(/ (- c) b))))double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
double code(double a, double b, double c) {
double tmp;
if (b <= -1.82e+40) {
tmp = -b / a;
} else if (b <= 5.4e-31) {
tmp = 0.5 / (a / (hypot(b, sqrt((a * (c * -4.0)))) - b));
} else {
tmp = -c / b;
}
return tmp;
}
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.82e+40) {
tmp = -b / a;
} else if (b <= 5.4e-31) {
tmp = 0.5 / (a / (Math.hypot(b, Math.sqrt((a * (c * -4.0)))) - b));
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
def code(a, b, c): tmp = 0 if b <= -1.82e+40: tmp = -b / a elif b <= 5.4e-31: tmp = 0.5 / (a / (math.hypot(b, math.sqrt((a * (c * -4.0)))) - b)) else: tmp = -c / b return tmp
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function code(a, b, c) tmp = 0.0 if (b <= -1.82e+40) tmp = Float64(Float64(-b) / a); elseif (b <= 5.4e-31) tmp = Float64(0.5 / Float64(a / Float64(hypot(b, sqrt(Float64(a * Float64(c * -4.0)))) - b))); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.82e+40) tmp = -b / a; elseif (b <= 5.4e-31) tmp = 0.5 / (a / (hypot(b, sqrt((a * (c * -4.0)))) - b)); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := If[LessEqual[b, -1.82e+40], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 5.4e-31], N[(0.5 / N[(a / N[(N[Sqrt[b ^ 2 + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] ^ 2], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -1.82 \cdot 10^{+40}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 5.4 \cdot 10^{-31}:\\
\;\;\;\;\frac{0.5}{\frac{a}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b}}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
Results
if b < -1.82e40Initial program 36.8
Simplified36.9
[Start]36.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]36.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]36.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]36.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]36.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]36.8 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{--1}{2}}{a}}
\] |
associate-*r/ [<=]36.9 | \[ \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{\frac{--1}{2}}{a}}
\] |
/-rgt-identity [<=]36.9 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
metadata-eval [<=]36.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{--1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
Taylor expanded in b around -inf 5.8
Simplified5.8
[Start]5.8 | \[ -1 \cdot \frac{b}{a}
\] |
|---|---|
associate-*r/ [=>]5.8 | \[ \color{blue}{\frac{-1 \cdot b}{a}}
\] |
mul-1-neg [=>]5.8 | \[ \frac{\color{blue}{-b}}{a}
\] |
if -1.82e40 < b < 5.40000000000000027e-31Initial program 16.0
Simplified16.2
[Start]16.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]16.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]16.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]16.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]16.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]16.0 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{--1}{2}}{a}}
\] |
associate-*r/ [<=]16.1 | \[ \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right) \cdot \frac{\frac{--1}{2}}{a}}
\] |
/-rgt-identity [<=]16.1 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
metadata-eval [<=]16.1 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{--1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
Applied egg-rr19.7
if 5.40000000000000027e-31 < b Initial program 55.4
Simplified55.4
[Start]55.4 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
*-commutative [=>]55.4 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around inf 6.3
Simplified6.3
[Start]6.3 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
mul-1-neg [=>]6.3 | \[ \color{blue}{-\frac{c}{b}}
\] |
distribute-neg-frac [=>]6.3 | \[ \color{blue}{\frac{-c}{b}}
\] |
Final simplification12.0
| Alternative 1 | |
|---|---|
| Error | 10.5 |
| Cost | 7624 |
| Alternative 2 | |
|---|---|
| Error | 10.4 |
| Cost | 7624 |
| Alternative 3 | |
|---|---|
| Error | 13.6 |
| Cost | 7368 |
| Alternative 4 | |
|---|---|
| Error | 13.6 |
| Cost | 7368 |
| Alternative 5 | |
|---|---|
| Error | 22.2 |
| Cost | 580 |
| Alternative 6 | |
|---|---|
| Error | 39.9 |
| Cost | 388 |
| Alternative 7 | |
|---|---|
| Error | 22.2 |
| Cost | 388 |
| Alternative 8 | |
|---|---|
| Error | 62.3 |
| Cost | 192 |
| Alternative 9 | |
|---|---|
| Error | 56.4 |
| Cost | 192 |
herbie shell --seed 2023016
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))