| Alternative 1 | |
|---|---|
| Error | 10.3 |
| Cost | 7688 |
(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 (/ (- c) b))
(t_1 (* 2.0 (/ c (- (hypot b (sqrt (* c (* a -4.0)))) b)))))
(if (<= b -8.5e+55)
t_0
(if (<= b -7.5e+27)
t_1
(if (<= b -1e-20)
t_0
(if (<= b 3.2e-261)
t_1
(if (<= b 2.25e+65)
(/ (- (- b) (sqrt (+ (* b b) (* -4.0 (* c a))))) (* 2.0 a))
(- (/ c b) (/ b a)))))))))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 = -c / b;
double t_1 = 2.0 * (c / (hypot(b, sqrt((c * (a * -4.0)))) - b));
double tmp;
if (b <= -8.5e+55) {
tmp = t_0;
} else if (b <= -7.5e+27) {
tmp = t_1;
} else if (b <= -1e-20) {
tmp = t_0;
} else if (b <= 3.2e-261) {
tmp = t_1;
} else if (b <= 2.25e+65) {
tmp = (-b - sqrt(((b * b) + (-4.0 * (c * a))))) / (2.0 * a);
} else {
tmp = (c / b) - (b / a);
}
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 = -c / b;
double t_1 = 2.0 * (c / (Math.hypot(b, Math.sqrt((c * (a * -4.0)))) - b));
double tmp;
if (b <= -8.5e+55) {
tmp = t_0;
} else if (b <= -7.5e+27) {
tmp = t_1;
} else if (b <= -1e-20) {
tmp = t_0;
} else if (b <= 3.2e-261) {
tmp = t_1;
} else if (b <= 2.25e+65) {
tmp = (-b - Math.sqrt(((b * b) + (-4.0 * (c * a))))) / (2.0 * a);
} else {
tmp = (c / b) - (b / a);
}
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 = -c / b t_1 = 2.0 * (c / (math.hypot(b, math.sqrt((c * (a * -4.0)))) - b)) tmp = 0 if b <= -8.5e+55: tmp = t_0 elif b <= -7.5e+27: tmp = t_1 elif b <= -1e-20: tmp = t_0 elif b <= 3.2e-261: tmp = t_1 elif b <= 2.25e+65: tmp = (-b - math.sqrt(((b * b) + (-4.0 * (c * a))))) / (2.0 * a) else: tmp = (c / b) - (b / a) 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) t_0 = Float64(Float64(-c) / b) t_1 = Float64(2.0 * Float64(c / Float64(hypot(b, sqrt(Float64(c * Float64(a * -4.0)))) - b))) tmp = 0.0 if (b <= -8.5e+55) tmp = t_0; elseif (b <= -7.5e+27) tmp = t_1; elseif (b <= -1e-20) tmp = t_0; elseif (b <= 3.2e-261) tmp = t_1; elseif (b <= 2.25e+65) tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a))))) / Float64(2.0 * a)); else tmp = Float64(Float64(c / b) - Float64(b / a)); 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 = -c / b; t_1 = 2.0 * (c / (hypot(b, sqrt((c * (a * -4.0)))) - b)); tmp = 0.0; if (b <= -8.5e+55) tmp = t_0; elseif (b <= -7.5e+27) tmp = t_1; elseif (b <= -1e-20) tmp = t_0; elseif (b <= 3.2e-261) tmp = t_1; elseif (b <= 2.25e+65) tmp = (-b - sqrt(((b * b) + (-4.0 * (c * a))))) / (2.0 * a); else tmp = (c / b) - (b / a); 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_] := Block[{t$95$0 = N[((-c) / b), $MachinePrecision]}, Block[{t$95$1 = N[(2.0 * N[(c / N[(N[Sqrt[b ^ 2 + N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] ^ 2], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -8.5e+55], t$95$0, If[LessEqual[b, -7.5e+27], t$95$1, If[LessEqual[b, -1e-20], t$95$0, If[LessEqual[b, 3.2e-261], t$95$1, If[LessEqual[b, 2.25e+65], N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]]]]]]]
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
t_0 := \frac{-c}{b}\\
t_1 := 2 \cdot \frac{c}{\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b}\\
\mathbf{if}\;b \leq -8.5 \cdot 10^{+55}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq -7.5 \cdot 10^{+27}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq -1 \cdot 10^{-20}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;b \leq 3.2 \cdot 10^{-261}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;b \leq 2.25 \cdot 10^{+65}:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b + -4 \cdot \left(c \cdot a\right)}}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
Results
| Original | 33.8 |
|---|---|
| Target | 21.0 |
| Herbie | 8.3 |
if b < -8.50000000000000002e55 or -7.5000000000000002e27 < b < -9.99999999999999945e-21Initial program 56.0
Simplified56.0
[Start]56.0 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
*-rgt-identity [<=]56.0 | \[ \color{blue}{\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot 1}
\] |
metadata-eval [<=]56.0 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot \color{blue}{\left(--1\right)}
\] |
associate-*l/ [=>]56.0 | \[ \color{blue}{\frac{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \left(--1\right)}{2 \cdot a}}
\] |
associate-*r/ [<=]56.0 | \[ \color{blue}{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2 \cdot a}}
\] |
distribute-neg-frac [<=]56.0 | \[ \left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \color{blue}{\left(-\frac{-1}{2 \cdot a}\right)}
\] |
distribute-rgt-neg-in [<=]56.0 | \[ \color{blue}{-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{-1}{2 \cdot a}}
\] |
distribute-lft-neg-out [<=]56.0 | \[ \color{blue}{\left(-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)\right) \cdot \frac{-1}{2 \cdot a}}
\] |
Taylor expanded in b around -inf 5.7
Simplified5.7
[Start]5.7 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
mul-1-neg [=>]5.7 | \[ \color{blue}{-\frac{c}{b}}
\] |
distribute-neg-frac [=>]5.7 | \[ \color{blue}{\frac{-c}{b}}
\] |
if -8.50000000000000002e55 < b < -7.5000000000000002e27 or -9.99999999999999945e-21 < b < 3.20000000000000004e-261Initial program 24.8
Simplified24.8
[Start]24.8 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
*-rgt-identity [<=]24.8 | \[ \color{blue}{\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot 1}
\] |
metadata-eval [<=]24.8 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot \color{blue}{\left(--1\right)}
\] |
associate-*l/ [=>]24.8 | \[ \color{blue}{\frac{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \left(--1\right)}{2 \cdot a}}
\] |
associate-*r/ [<=]24.8 | \[ \color{blue}{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2 \cdot a}}
\] |
distribute-neg-frac [<=]24.8 | \[ \left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \color{blue}{\left(-\frac{-1}{2 \cdot a}\right)}
\] |
distribute-rgt-neg-in [<=]24.8 | \[ \color{blue}{-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{-1}{2 \cdot a}}
\] |
distribute-lft-neg-out [<=]24.8 | \[ \color{blue}{\left(-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)\right) \cdot \frac{-1}{2 \cdot a}}
\] |
Applied egg-rr31.2
Simplified31.2
[Start]31.2 | \[ \frac{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -4\right)\right) - b \cdot b}{\left(a \cdot -2\right) \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b\right)}
\] |
|---|---|
fma-def [<=]31.2 | \[ \frac{\color{blue}{\left(b \cdot b + a \cdot \left(c \cdot -4\right)\right)} - b \cdot b}{\left(a \cdot -2\right) \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b\right)}
\] |
+-commutative [=>]31.2 | \[ \frac{\color{blue}{\left(a \cdot \left(c \cdot -4\right) + b \cdot b\right)} - b \cdot b}{\left(a \cdot -2\right) \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b\right)}
\] |
fma-def [=>]31.2 | \[ \frac{\color{blue}{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)} - b \cdot b}{\left(a \cdot -2\right) \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b\right)}
\] |
*-commutative [=>]31.2 | \[ \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{\color{blue}{\left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}}
\] |
associate-*r* [=>]31.2 | \[ \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{\left(\mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(a \cdot c\right) \cdot -4}}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
*-commutative [<=]31.2 | \[ \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{\left(\mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(c \cdot a\right)} \cdot -4}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
associate-*l* [=>]31.2 | \[ \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{\left(\mathsf{hypot}\left(b, \sqrt{\color{blue}{c \cdot \left(a \cdot -4\right)}}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
Taylor expanded in a around 0 27.1
Simplified27.1
[Start]27.1 | \[ \frac{-4 \cdot \left(c \cdot a\right)}{\left(\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
|---|---|
associate-*r* [=>]27.1 | \[ \frac{\color{blue}{\left(-4 \cdot c\right) \cdot a}}{\left(\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
*-commutative [<=]27.1 | \[ \frac{\color{blue}{\left(c \cdot -4\right)} \cdot a}{\left(\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
*-commutative [<=]27.1 | \[ \frac{\color{blue}{a \cdot \left(c \cdot -4\right)}}{\left(\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
*-commutative [=>]27.1 | \[ \frac{a \cdot \color{blue}{\left(-4 \cdot c\right)}}{\left(\mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right) - b\right) \cdot \left(a \cdot -2\right)}
\] |
Applied egg-rr54.4
Simplified13.8
[Start]54.4 | \[ e^{\mathsf{log1p}\left(\frac{a \cdot \left(2 \cdot \frac{c}{a}\right)}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}\right)} - 1
\] |
|---|---|
expm1-def [=>]35.9 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{a \cdot \left(2 \cdot \frac{c}{a}\right)}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}\right)\right)}
\] |
expm1-log1p [=>]27.9 | \[ \color{blue}{\frac{a \cdot \left(2 \cdot \frac{c}{a}\right)}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}}
\] |
*-commutative [=>]27.9 | \[ \frac{\color{blue}{\left(2 \cdot \frac{c}{a}\right) \cdot a}}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}
\] |
associate-*r/ [<=]28.3 | \[ \color{blue}{\left(2 \cdot \frac{c}{a}\right) \cdot \frac{a}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}}
\] |
associate-*l* [=>]28.3 | \[ \color{blue}{2 \cdot \left(\frac{c}{a} \cdot \frac{a}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}\right)}
\] |
times-frac [<=]27.0 | \[ 2 \cdot \color{blue}{\frac{c \cdot a}{a \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b\right)}}
\] |
*-commutative [=>]27.0 | \[ 2 \cdot \frac{\color{blue}{a \cdot c}}{a \cdot \left(\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b\right)}
\] |
times-frac [=>]13.8 | \[ 2 \cdot \color{blue}{\left(\frac{a}{a} \cdot \frac{c}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}\right)}
\] |
associate-*r/ [=>]13.8 | \[ 2 \cdot \color{blue}{\frac{\frac{a}{a} \cdot c}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}}
\] |
*-inverses [=>]13.8 | \[ 2 \cdot \frac{\color{blue}{1} \cdot c}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}
\] |
*-lft-identity [=>]13.8 | \[ 2 \cdot \frac{\color{blue}{c}}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(-4 \cdot c\right)}\right) - b}
\] |
associate-*r* [=>]13.8 | \[ 2 \cdot \frac{c}{\mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(a \cdot -4\right) \cdot c}}\right) - b}
\] |
*-commutative [=>]13.8 | \[ 2 \cdot \frac{c}{\mathsf{hypot}\left(b, \sqrt{\color{blue}{c \cdot \left(a \cdot -4\right)}}\right) - b}
\] |
if 3.20000000000000004e-261 < b < 2.25e65Initial program 8.5
if 2.25e65 < b Initial program 41.3
Simplified41.4
[Start]41.3 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
*-rgt-identity [<=]41.3 | \[ \color{blue}{\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot 1}
\] |
metadata-eval [<=]41.3 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot \color{blue}{\left(--1\right)}
\] |
associate-*l/ [=>]41.3 | \[ \color{blue}{\frac{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \left(--1\right)}{2 \cdot a}}
\] |
associate-*r/ [<=]41.4 | \[ \color{blue}{\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2 \cdot a}}
\] |
distribute-neg-frac [<=]41.4 | \[ \left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \color{blue}{\left(-\frac{-1}{2 \cdot a}\right)}
\] |
distribute-rgt-neg-in [<=]41.4 | \[ \color{blue}{-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{-1}{2 \cdot a}}
\] |
distribute-lft-neg-out [<=]41.4 | \[ \color{blue}{\left(-\left(\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)\right) \cdot \frac{-1}{2 \cdot a}}
\] |
Taylor expanded in b around inf 4.9
Simplified4.9
[Start]4.9 | \[ \frac{c}{b} + -1 \cdot \frac{b}{a}
\] |
|---|---|
+-commutative [=>]4.9 | \[ \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}}
\] |
associate-*r/ [=>]4.9 | \[ \color{blue}{\frac{-1 \cdot b}{a}} + \frac{c}{b}
\] |
mul-1-neg [=>]4.9 | \[ \frac{\color{blue}{-b}}{a} + \frac{c}{b}
\] |
Final simplification8.3
| Alternative 1 | |
|---|---|
| Error | 10.3 |
| Cost | 7688 |
| Alternative 2 | |
|---|---|
| Error | 10.3 |
| Cost | 7624 |
| Alternative 3 | |
|---|---|
| Error | 13.9 |
| Cost | 7432 |
| Alternative 4 | |
|---|---|
| Error | 13.9 |
| Cost | 7368 |
| Alternative 5 | |
|---|---|
| Error | 23.1 |
| Cost | 836 |
| Alternative 6 | |
|---|---|
| Error | 39.8 |
| Cost | 388 |
| Alternative 7 | |
|---|---|
| Error | 23.1 |
| Cost | 388 |
| Alternative 8 | |
|---|---|
| Error | 62.3 |
| Cost | 192 |
| Alternative 9 | |
|---|---|
| Error | 56.7 |
| Cost | 192 |
herbie shell --seed 2023027
(FPCore (a b c)
:name "quadm (p42, negative)"
:precision binary64
:herbie-target
(if (< b 0.0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
(/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))