| Alternative 1 | |
|---|---|
| Error | 12.52% |
| Cost | 13964 |
(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 -2e+124)
(- (/ c b) (/ b a))
(if (<= b 8.5e-233)
(* (/ (- b (sqrt (+ (* b b) (* a (* c -4.0))))) a) -0.5)
(if (<= b 2.2e-21)
(* (/ c (+ b (hypot b (sqrt (* c (* a -4.0)))))) -2.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 tmp;
if (b <= -2e+124) {
tmp = (c / b) - (b / a);
} else if (b <= 8.5e-233) {
tmp = ((b - sqrt(((b * b) + (a * (c * -4.0))))) / a) * -0.5;
} else if (b <= 2.2e-21) {
tmp = (c / (b + hypot(b, sqrt((c * (a * -4.0)))))) * -2.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 tmp;
if (b <= -2e+124) {
tmp = (c / b) - (b / a);
} else if (b <= 8.5e-233) {
tmp = ((b - Math.sqrt(((b * b) + (a * (c * -4.0))))) / a) * -0.5;
} else if (b <= 2.2e-21) {
tmp = (c / (b + Math.hypot(b, Math.sqrt((c * (a * -4.0)))))) * -2.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): tmp = 0 if b <= -2e+124: tmp = (c / b) - (b / a) elif b <= 8.5e-233: tmp = ((b - math.sqrt(((b * b) + (a * (c * -4.0))))) / a) * -0.5 elif b <= 2.2e-21: tmp = (c / (b + math.hypot(b, math.sqrt((c * (a * -4.0)))))) * -2.0 else: tmp = -c / b return tmp
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a)) end
function code(a, b, c) tmp = 0.0 if (b <= -2e+124) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 8.5e-233) tmp = Float64(Float64(Float64(b - sqrt(Float64(Float64(b * b) + Float64(a * Float64(c * -4.0))))) / a) * -0.5); elseif (b <= 2.2e-21) tmp = Float64(Float64(c / Float64(b + hypot(b, sqrt(Float64(c * Float64(a * -4.0)))))) * -2.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) tmp = 0.0; if (b <= -2e+124) tmp = (c / b) - (b / a); elseif (b <= 8.5e-233) tmp = ((b - sqrt(((b * b) + (a * (c * -4.0))))) / a) * -0.5; elseif (b <= 2.2e-21) tmp = (c / (b + hypot(b, sqrt((c * (a * -4.0)))))) * -2.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[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := If[LessEqual[b, -2e+124], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 8.5e-233], N[(N[(N[(b - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision] * -0.5), $MachinePrecision], If[LessEqual[b, 2.2e-21], N[(N[(c / N[(b + N[Sqrt[b ^ 2 + N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -2 \cdot 10^{+124}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 8.5 \cdot 10^{-233}:\\
\;\;\;\;\frac{b - \sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)}}{a} \cdot -0.5\\
\mathbf{elif}\;b \leq 2.2 \cdot 10^{-21}:\\
\;\;\;\;\frac{c}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)} \cdot -2\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
Results
| Original | 53.16% |
|---|---|
| Target | 32.98% |
| Herbie | 12.37% |
if b < -1.9999999999999999e124Initial program 84.03
Simplified84.09
[Start]84.03 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]84.03 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]84.03 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]84.03 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]84.03 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]84.03 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2}}{a}}
\] |
associate-*r/ [<=]84.09 | \[ \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{\frac{--1}{2}}{a}}
\] |
/-rgt-identity [<=]84.09 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
metadata-eval [<=]84.09 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{--1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
Taylor expanded in b around -inf 16.38
Taylor expanded in c around 0 5.35
Simplified5.35
[Start]5.35 | \[ \frac{c}{b} + -1 \cdot \frac{b}{a}
\] |
|---|---|
mul-1-neg [=>]5.35 | \[ \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)}
\] |
unsub-neg [=>]5.35 | \[ \color{blue}{\frac{c}{b} - \frac{b}{a}}
\] |
if -1.9999999999999999e124 < b < 8.5000000000000005e-233Initial program 14.46
Simplified14.51
[Start]14.46 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
associate-/r* [=>]14.43 | \[ \color{blue}{\frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{a}}
\] |
/-rgt-identity [<=]14.43 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\color{blue}{\frac{a}{1}}}
\] |
metadata-eval [<=]14.43 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\frac{a}{\color{blue}{-1 \cdot -1}}}
\] |
associate-/l/ [<=]14.43 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\color{blue}{\frac{\frac{a}{-1}}{-1}}}
\] |
associate-/l* [<=]14.43 | \[ \color{blue}{\frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2} \cdot -1}{\frac{a}{-1}}}
\] |
associate-*r/ [<=]14.65 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2} \cdot \frac{-1}{\frac{a}{-1}}}
\] |
times-frac [<=]14.46 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot -1}{2 \cdot \frac{a}{-1}}}
\] |
*-commutative [=>]14.46 | \[ \frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot -1}{\color{blue}{\frac{a}{-1} \cdot 2}}
\] |
times-frac [=>]14.49 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{a}{-1}} \cdot \frac{-1}{2}}
\] |
Applied egg-rr14.51
if 8.5000000000000005e-233 < b < 2.2000000000000001e-21Initial program 39.23
Simplified39.27
[Start]39.23 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]39.23 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]39.23 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]39.23 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]39.23 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]39.23 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2}}{a}}
\] |
associate-*r/ [<=]39.28 | \[ \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{\frac{--1}{2}}{a}}
\] |
/-rgt-identity [<=]39.28 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
metadata-eval [<=]39.28 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{--1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
Applied egg-rr48.34
Taylor expanded in b around 0 42.93
Applied egg-rr43.74
Simplified18.58
[Start]43.74 | \[ \frac{-4}{\left(a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)\right) \cdot -2} \cdot \left(-c \cdot a\right)
\] |
|---|---|
distribute-rgt-neg-out [=>]43.74 | \[ \color{blue}{-\frac{-4}{\left(a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)\right) \cdot -2} \cdot \left(c \cdot a\right)}
\] |
associate-*l/ [=>]42.92 | \[ -\color{blue}{\frac{-4 \cdot \left(c \cdot a\right)}{\left(a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)\right) \cdot -2}}
\] |
*-commutative [=>]42.92 | \[ -\frac{-4 \cdot \left(c \cdot a\right)}{\color{blue}{-2 \cdot \left(a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)\right)}}
\] |
times-frac [=>]42.92 | \[ -\color{blue}{\frac{-4}{-2} \cdot \frac{c \cdot a}{a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)}}
\] |
metadata-eval [=>]42.92 | \[ -\color{blue}{2} \cdot \frac{c \cdot a}{a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right)}
\] |
associate-/r* [=>]33.38 | \[ -2 \cdot \color{blue}{\frac{\frac{c \cdot a}{a}}{b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)}}
\] |
associate-/l* [=>]18.6 | \[ -2 \cdot \frac{\color{blue}{\frac{c}{\frac{a}{a}}}}{b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)}
\] |
associate-/l/ [=>]18.6 | \[ -2 \cdot \color{blue}{\frac{c}{\left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right) \cdot \frac{a}{a}}}
\] |
*-inverses [=>]18.6 | \[ -2 \cdot \frac{c}{\left(b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)\right) \cdot \color{blue}{1}}
\] |
*-rgt-identity [=>]18.6 | \[ -2 \cdot \frac{c}{\color{blue}{b + \mathsf{hypot}\left(b, \sqrt{-4 \cdot \left(c \cdot a\right)}\right)}}
\] |
*-commutative [=>]18.6 | \[ -2 \cdot \frac{c}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(c \cdot a\right) \cdot -4}}\right)}
\] |
associate-*l* [=>]18.58 | \[ -2 \cdot \frac{c}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{c \cdot \left(a \cdot -4\right)}}\right)}
\] |
if 2.2000000000000001e-21 < b Initial program 86.81
Taylor expanded in b around inf 10.32
Simplified10.32
[Start]10.32 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
associate-*r/ [=>]10.32 | \[ \color{blue}{\frac{-1 \cdot c}{b}}
\] |
neg-mul-1 [<=]10.32 | \[ \frac{\color{blue}{-c}}{b}
\] |
Final simplification12.37
| Alternative 1 | |
|---|---|
| Error | 12.52% |
| Cost | 13964 |
| Alternative 2 | |
|---|---|
| Error | 15.09% |
| Cost | 7624 |
| Alternative 3 | |
|---|---|
| Error | 20.43% |
| Cost | 7368 |
| Alternative 4 | |
|---|---|
| Error | 20.36% |
| Cost | 7368 |
| Alternative 5 | |
|---|---|
| Error | 35.54% |
| Cost | 708 |
| Alternative 6 | |
|---|---|
| Error | 35.52% |
| Cost | 580 |
| Alternative 7 | |
|---|---|
| Error | 61.63% |
| Cost | 388 |
| Alternative 8 | |
|---|---|
| Error | 35.82% |
| Cost | 388 |
| Alternative 9 | |
|---|---|
| Error | 88.41% |
| Cost | 192 |
herbie shell --seed 2023103
(FPCore (a b c)
:name "quadp (p42, positive)"
: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)))