| Alternative 1 | |
|---|---|
| Accuracy | 84.2% |
| Cost | 14092 |
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
(FPCore (a b c)
:precision binary64
(let* ((t_0 (/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))))
(if (<= b -5e+152)
(/ (- b) a)
(if (<= b -2e-143)
t_0
(if (<= b 1.55e-243)
(+ (* (hypot b (sqrt (* a (* c -4.0)))) (/ 0.5 a)) (* b (/ -0.5 a)))
(if (<= b 6e-14) t_0 (/ (- 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 t_0 = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
double tmp;
if (b <= -5e+152) {
tmp = -b / a;
} else if (b <= -2e-143) {
tmp = t_0;
} else if (b <= 1.55e-243) {
tmp = (hypot(b, sqrt((a * (c * -4.0)))) * (0.5 / a)) + (b * (-0.5 / a));
} else if (b <= 6e-14) {
tmp = t_0;
} 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 t_0 = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
double tmp;
if (b <= -5e+152) {
tmp = -b / a;
} else if (b <= -2e-143) {
tmp = t_0;
} else if (b <= 1.55e-243) {
tmp = (Math.hypot(b, Math.sqrt((a * (c * -4.0)))) * (0.5 / a)) + (b * (-0.5 / a));
} else if (b <= 6e-14) {
tmp = t_0;
} 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): t_0 = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0) tmp = 0 if b <= -5e+152: tmp = -b / a elif b <= -2e-143: tmp = t_0 elif b <= 1.55e-243: tmp = (math.hypot(b, math.sqrt((a * (c * -4.0)))) * (0.5 / a)) + (b * (-0.5 / a)) elif b <= 6e-14: tmp = t_0 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) t_0 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)) tmp = 0.0 if (b <= -5e+152) tmp = Float64(Float64(-b) / a); elseif (b <= -2e-143) tmp = t_0; elseif (b <= 1.55e-243) tmp = Float64(Float64(hypot(b, sqrt(Float64(a * Float64(c * -4.0)))) * Float64(0.5 / a)) + Float64(b * Float64(-0.5 / a))); elseif (b <= 6e-14) tmp = t_0; 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) t_0 = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0); tmp = 0.0; if (b <= -5e+152) tmp = -b / a; elseif (b <= -2e-143) tmp = t_0; elseif (b <= 1.55e-243) tmp = (hypot(b, sqrt((a * (c * -4.0)))) * (0.5 / a)) + (b * (-0.5 / a)); elseif (b <= 6e-14) tmp = t_0; 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_] := Block[{t$95$0 = N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * 4.0), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -5e+152], N[((-b) / a), $MachinePrecision], If[LessEqual[b, -2e-143], t$95$0, If[LessEqual[b, 1.55e-243], N[(N[(N[Sqrt[b ^ 2 + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] ^ 2], $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision] + N[(b * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6e-14], t$95$0, 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}
t_0 := \frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\
\mathbf{if}\;b \leq -5 \cdot 10^{+152}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq -2 \cdot 10^{-143}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 1.55 \cdot 10^{-243}:\\
\;\;\;\;\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) \cdot \frac{0.5}{a} + b \cdot \frac{-0.5}{a}\\
\mathbf{elif}\;b \leq 6 \cdot 10^{-14}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
Results
| Original | 46.3% |
|---|---|
| Target | 66.6% |
| Herbie | 84.4% |
if b < -5e152Initial program 0.9%
Simplified0.9%
[Start]0.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]0.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]0.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]0.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]0.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]0.9 | \[ \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/ [<=]0.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 [<=]0.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 [<=]0.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 96.8%
Simplified96.8%
[Start]96.8 | \[ -1 \cdot \frac{b}{a}
\] |
|---|---|
associate-*r/ [=>]96.8 | \[ \color{blue}{\frac{-1 \cdot b}{a}}
\] |
mul-1-neg [=>]96.8 | \[ \frac{\color{blue}{-b}}{a}
\] |
if -5e152 < b < -1.9999999999999999e-143 or 1.55e-243 < b < 5.9999999999999997e-14Initial program 76.9%
if -1.9999999999999999e-143 < b < 1.55e-243Initial program 75.6%
Simplified75.4%
[Start]75.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]75.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]75.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]75.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]75.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]75.6 | \[ \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/ [<=]75.5 | \[ \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 [<=]75.5 | \[ \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 [<=]75.5 | \[ \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-rr79.2%
[Start]75.4 | \[ \left(\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)} - b\right) \cdot \frac{0.5}{a}
\] |
|---|---|
*-commutative [=>]75.4 | \[ \color{blue}{\frac{0.5}{a} \cdot \left(\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)} - b\right)}
\] |
sub-neg [=>]75.4 | \[ \frac{0.5}{a} \cdot \color{blue}{\left(\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)} + \left(-b\right)\right)}
\] |
distribute-rgt-in [=>]75.4 | \[ \color{blue}{\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)} \cdot \frac{0.5}{a} + \left(-b\right) \cdot \frac{0.5}{a}}
\] |
fma-udef [=>]75.4 | \[ \sqrt{\color{blue}{b \cdot b + \left(a \cdot c\right) \cdot -4}} \cdot \frac{0.5}{a} + \left(-b\right) \cdot \frac{0.5}{a}
\] |
add-sqr-sqrt [=>]75.3 | \[ \sqrt{b \cdot b + \color{blue}{\sqrt{\left(a \cdot c\right) \cdot -4} \cdot \sqrt{\left(a \cdot c\right) \cdot -4}}} \cdot \frac{0.5}{a} + \left(-b\right) \cdot \frac{0.5}{a}
\] |
hypot-def [=>]79.1 | \[ \color{blue}{\mathsf{hypot}\left(b, \sqrt{\left(a \cdot c\right) \cdot -4}\right)} \cdot \frac{0.5}{a} + \left(-b\right) \cdot \frac{0.5}{a}
\] |
associate-*l* [=>]79.2 | \[ \mathsf{hypot}\left(b, \sqrt{\color{blue}{a \cdot \left(c \cdot -4\right)}}\right) \cdot \frac{0.5}{a} + \left(-b\right) \cdot \frac{0.5}{a}
\] |
if 5.9999999999999997e-14 < b Initial program 13.0%
Simplified13.0%
[Start]13.0 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
*-commutative [=>]13.0 | \[ \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 91.2%
Simplified91.2%
[Start]91.2 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
mul-1-neg [=>]91.2 | \[ \color{blue}{-\frac{c}{b}}
\] |
distribute-neg-frac [=>]91.2 | \[ \color{blue}{\frac{-c}{b}}
\] |
Final simplification84.4%
| Alternative 1 | |
|---|---|
| Accuracy | 84.2% |
| Cost | 14092 |
| Alternative 2 | |
|---|---|
| Accuracy | 84.0% |
| Cost | 13964 |
| Alternative 3 | |
|---|---|
| Accuracy | 83.7% |
| Cost | 7624 |
| Alternative 4 | |
|---|---|
| Accuracy | 83.9% |
| Cost | 7624 |
| Alternative 5 | |
|---|---|
| Accuracy | 78.6% |
| Cost | 7368 |
| Alternative 6 | |
|---|---|
| Accuracy | 78.5% |
| Cost | 7368 |
| Alternative 7 | |
|---|---|
| Accuracy | 78.6% |
| Cost | 7368 |
| Alternative 8 | |
|---|---|
| Accuracy | 64.8% |
| Cost | 836 |
| Alternative 9 | |
|---|---|
| Accuracy | 64.9% |
| Cost | 388 |
| Alternative 10 | |
|---|---|
| Accuracy | 29.1% |
| Cost | 256 |
| Alternative 11 | |
|---|---|
| Accuracy | 2.6% |
| Cost | 192 |
herbie shell --seed 2023133
(FPCore (a b c)
:name "The quadratic formula (r1)"
:precision binary64
:herbie-target
(if (< b 0.0) (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)) (/ c (* a (/ (- (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))