| Alternative 1 | |
|---|---|
| Error | 19.9 |
| 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 -7e+126)
(- (/ b a))
(if (<= b 2.7e-134)
(/ (- (sqrt (+ (* b b) (* (* a c) -4.0))) b) (* a 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 <= -7e+126) {
tmp = -(b / a);
} else if (b <= 2.7e-134) {
tmp = (sqrt(((b * b) + ((a * c) * -4.0))) - b) / (a * 2.0);
} else {
tmp = -(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 <= (-7d+126)) then
tmp = -(b / a)
else if (b <= 2.7d-134) then
tmp = (sqrt(((b * b) + ((a * c) * (-4.0d0)))) - b) / (a * 2.0d0)
else
tmp = -(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 <= -7e+126) {
tmp = -(b / a);
} else if (b <= 2.7e-134) {
tmp = (Math.sqrt(((b * b) + ((a * c) * -4.0))) - b) / (a * 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 <= -7e+126: tmp = -(b / a) elif b <= 2.7e-134: tmp = (math.sqrt(((b * b) + ((a * c) * -4.0))) - b) / (a * 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 <= -7e+126) tmp = Float64(-Float64(b / a)); elseif (b <= 2.7e-134) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(Float64(a * c) * -4.0))) - b) / Float64(a * 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 <= -7e+126) tmp = -(b / a); elseif (b <= 2.7e-134) tmp = (sqrt(((b * b) + ((a * c) * -4.0))) - b) / (a * 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, -7e+126], (-N[(b / a), $MachinePrecision]), If[LessEqual[b, 2.7e-134], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(N[(a * c), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $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 -7 \cdot 10^{+126}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{elif}\;b \leq 2.7 \cdot 10^{-134}:\\
\;\;\;\;\frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;-\frac{c}{b}\\
\end{array}
Results
| Original | 33.8 |
|---|---|
| Target | 20.7 |
| Herbie | 10.6 |
if b < -7.0000000000000005e126Initial program 54.9
Simplified54.9
[Start]54.9 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-62 [=>]54.9 | \[ \frac{\color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-\left(-b\right)\right)}}{2 \cdot a}
\] |
rational_best-simplify-61 [=>]54.9 | \[ \frac{\sqrt{\color{blue}{b \cdot b + \left(-4 \cdot \left(a \cdot c\right)\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-52 [=>]54.9 | \[ \frac{\sqrt{b \cdot b + \color{blue}{\left(a \cdot c\right) \cdot \left(-4\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
metadata-eval [=>]54.9 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot \color{blue}{-4}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-51 [=>]54.9 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - \color{blue}{b}}{2 \cdot a}
\] |
rational_best-simplify-3 [=>]54.9 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - b}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around -inf 3.0
Simplified3.0
[Start]3.0 | \[ -1 \cdot \frac{b}{a}
\] |
|---|---|
rational_best-simplify-3 [=>]3.0 | \[ \color{blue}{\frac{b}{a} \cdot -1}
\] |
rational_best-simplify-17 [=>]3.0 | \[ \color{blue}{-\frac{b}{a}}
\] |
if -7.0000000000000005e126 < b < 2.6999999999999998e-134Initial program 10.6
Simplified10.6
[Start]10.6 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-62 [=>]10.6 | \[ \frac{\color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-\left(-b\right)\right)}}{2 \cdot a}
\] |
rational_best-simplify-61 [=>]10.6 | \[ \frac{\sqrt{\color{blue}{b \cdot b + \left(-4 \cdot \left(a \cdot c\right)\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-52 [=>]10.6 | \[ \frac{\sqrt{b \cdot b + \color{blue}{\left(a \cdot c\right) \cdot \left(-4\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
metadata-eval [=>]10.6 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot \color{blue}{-4}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-51 [=>]10.6 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - \color{blue}{b}}{2 \cdot a}
\] |
rational_best-simplify-3 [=>]10.6 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - b}{\color{blue}{a \cdot 2}}
\] |
if 2.6999999999999998e-134 < b Initial program 50.1
Simplified50.1
[Start]50.1 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-62 [=>]50.1 | \[ \frac{\color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-\left(-b\right)\right)}}{2 \cdot a}
\] |
rational_best-simplify-61 [=>]50.1 | \[ \frac{\sqrt{\color{blue}{b \cdot b + \left(-4 \cdot \left(a \cdot c\right)\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-52 [=>]50.1 | \[ \frac{\sqrt{b \cdot b + \color{blue}{\left(a \cdot c\right) \cdot \left(-4\right)}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
metadata-eval [=>]50.1 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot \color{blue}{-4}} - \left(-\left(-b\right)\right)}{2 \cdot a}
\] |
rational_best-simplify-51 [=>]50.1 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - \color{blue}{b}}{2 \cdot a}
\] |
rational_best-simplify-3 [=>]50.1 | \[ \frac{\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - b}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around inf 12.9
Simplified12.9
[Start]12.9 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
rational_best-simplify-3 [=>]12.9 | \[ \color{blue}{\frac{c}{b} \cdot -1}
\] |
rational_best-simplify-17 [=>]12.9 | \[ \color{blue}{-\frac{c}{b}}
\] |
Final simplification10.6
| Alternative 1 | |
|---|---|
| Error | 19.9 |
| Cost | 7368 |
| Alternative 2 | |
|---|---|
| Error | 14.6 |
| Cost | 7368 |
| Alternative 3 | |
|---|---|
| Error | 20.0 |
| Cost | 7112 |
| Alternative 4 | |
|---|---|
| Error | 39.3 |
| Cost | 388 |
| Alternative 5 | |
|---|---|
| Error | 22.8 |
| Cost | 388 |
| Alternative 6 | |
|---|---|
| Error | 56.7 |
| Cost | 192 |
herbie shell --seed 2023104
(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)))