| Alternative 1 | |
|---|---|
| Error | 13.8 |
| Cost | 7368 |
(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 -5.2e-133)
(- (/ c b))
(if (<= b 1.8e+91)
(* (+ b (sqrt (- (* b b) (* c (* a 4.0))))) (/ -0.5 a))
(+ (- (/ b a)) (/ 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 <= -5.2e-133) {
tmp = -(c / b);
} else if (b <= 1.8e+91) {
tmp = (b + sqrt(((b * b) - (c * (a * 4.0))))) * (-0.5 / a);
} else {
tmp = -(b / a) + (c / b);
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b - sqrt(((b * b) - (4.0d0 * (a * c))))) / (2.0d0 * a)
end function
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: tmp
if (b <= (-5.2d-133)) then
tmp = -(c / b)
else if (b <= 1.8d+91) then
tmp = (b + sqrt(((b * b) - (c * (a * 4.0d0))))) * ((-0.5d0) / a)
else
tmp = -(b / a) + (c / b)
end if
code = tmp
end function
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 <= -5.2e-133) {
tmp = -(c / b);
} else if (b <= 1.8e+91) {
tmp = (b + Math.sqrt(((b * b) - (c * (a * 4.0))))) * (-0.5 / a);
} else {
tmp = -(b / a) + (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 <= -5.2e-133: tmp = -(c / b) elif b <= 1.8e+91: tmp = (b + math.sqrt(((b * b) - (c * (a * 4.0))))) * (-0.5 / a) else: tmp = -(b / a) + (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 <= -5.2e-133) tmp = Float64(-Float64(c / b)); elseif (b <= 1.8e+91) tmp = Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0))))) * Float64(-0.5 / a)); else tmp = Float64(Float64(-Float64(b / a)) + 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 <= -5.2e-133) tmp = -(c / b); elseif (b <= 1.8e+91) tmp = (b + sqrt(((b * b) - (c * (a * 4.0))))) * (-0.5 / a); else tmp = -(b / a) + (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, -5.2e-133], (-N[(c / b), $MachinePrecision]), If[LessEqual[b, 1.8e+91], N[(N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(-0.5 / a), $MachinePrecision]), $MachinePrecision], N[((-N[(b / a), $MachinePrecision]) + N[(c / b), $MachinePrecision]), $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 -5.2 \cdot 10^{-133}:\\
\;\;\;\;-\frac{c}{b}\\
\mathbf{elif}\;b \leq 1.8 \cdot 10^{+91}:\\
\;\;\;\;\left(b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}\right) \cdot \frac{-0.5}{a}\\
\mathbf{else}:\\
\;\;\;\;\left(-\frac{b}{a}\right) + \frac{c}{b}\\
\end{array}
Results
| Original | 33.8 |
|---|---|
| Target | 20.7 |
| Herbie | 10.6 |
if b < -5.1999999999999999e-133Initial program 51.1
Simplified51.1
[Start]51.1 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational.json-simplify-2 [=>]51.1 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around -inf 12.0
Simplified12.0
[Start]12.0 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
rational.json-simplify-2 [=>]12.0 | \[ \color{blue}{\frac{c}{b} \cdot -1}
\] |
rational.json-simplify-9 [=>]12.0 | \[ \color{blue}{-\frac{c}{b}}
\] |
if -5.1999999999999999e-133 < b < 1.8e91Initial program 11.6
Simplified11.6
[Start]11.6 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational.json-simplify-2 [=>]11.6 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot 2}}
\] |
Applied egg-rr11.6
Simplified11.7
[Start]11.6 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{-\left(a + a\right)} + 0
\] |
|---|---|
rational.json-simplify-4 [=>]11.6 | \[ \color{blue}{\frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{-\left(a + a\right)}}
\] |
rational.json-simplify-12 [=>]11.6 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{0 - \left(a + a\right)}}
\] |
rational.json-simplify-50 [=>]11.6 | \[ \color{blue}{\frac{-\left(b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)}{\left(a + a\right) - 0}}
\] |
rational.json-simplify-8 [=>]11.6 | \[ \frac{\color{blue}{\left(b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot -1}}{\left(a + a\right) - 0}
\] |
rational.json-simplify-2 [<=]11.6 | \[ \frac{\color{blue}{-1 \cdot \left(b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)}}{\left(a + a\right) - 0}
\] |
rational.json-simplify-5 [=>]11.6 | \[ \frac{-1 \cdot \left(b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right)}{\color{blue}{a + a}}
\] |
rational.json-simplify-49 [=>]11.7 | \[ \color{blue}{\left(b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{-1}{a + a}}
\] |
rational.json-simplify-2 [=>]11.7 | \[ \left(b + \sqrt{b \cdot b - 4 \cdot \color{blue}{\left(c \cdot a\right)}}\right) \cdot \frac{-1}{a + a}
\] |
rational.json-simplify-43 [=>]11.7 | \[ \left(b + \sqrt{b \cdot b - \color{blue}{c \cdot \left(a \cdot 4\right)}}\right) \cdot \frac{-1}{a + a}
\] |
rational.json-simplify-35 [=>]11.7 | \[ \left(b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}\right) \cdot \color{blue}{\frac{-1 + -1}{\left(a + a\right) + \left(a + a\right)}}
\] |
metadata-eval [=>]11.7 | \[ \left(b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}\right) \cdot \frac{\color{blue}{-2}}{\left(a + a\right) + \left(a + a\right)}
\] |
if 1.8e91 < b Initial program 43.6
Simplified43.6
[Start]43.6 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational.json-simplify-2 [=>]43.6 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around inf 3.9
Simplified3.9
[Start]3.9 | \[ \frac{c}{b} + -1 \cdot \frac{b}{a}
\] |
|---|---|
rational.json-simplify-1 [=>]3.9 | \[ \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}}
\] |
rational.json-simplify-2 [=>]3.9 | \[ \color{blue}{\frac{b}{a} \cdot -1} + \frac{c}{b}
\] |
rational.json-simplify-9 [=>]3.9 | \[ \color{blue}{\left(-\frac{b}{a}\right)} + \frac{c}{b}
\] |
Final simplification10.6
| Alternative 1 | |
|---|---|
| Error | 13.8 |
| Cost | 7368 |
| Alternative 2 | |
|---|---|
| Error | 14.2 |
| Cost | 7240 |
| Alternative 3 | |
|---|---|
| Error | 20.8 |
| Cost | 7112 |
| Alternative 4 | |
|---|---|
| Error | 39.9 |
| Cost | 388 |
| Alternative 5 | |
|---|---|
| Error | 22.9 |
| Cost | 388 |
| Alternative 6 | |
|---|---|
| Error | 57.0 |
| Cost | 192 |
herbie shell --seed 2023073
(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)))