| Alternative 1 | |
|---|---|
| Error | 13.7 |
| 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 -3.5e-69)
(/ c (- b))
(if (<= b 8e+46)
(/ (+ b (sqrt (- (* b b) (* 4.0 (* a c))))) (* a -2.0))
(/ 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 tmp;
if (b <= -3.5e-69) {
tmp = c / -b;
} else if (b <= 8e+46) {
tmp = (b + sqrt(((b * b) - (4.0 * (a * c))))) / (a * -2.0);
} else {
tmp = b / -a;
}
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 <= (-3.5d-69)) then
tmp = c / -b
else if (b <= 8d+46) then
tmp = (b + sqrt(((b * b) - (4.0d0 * (a * c))))) / (a * (-2.0d0))
else
tmp = b / -a
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 <= -3.5e-69) {
tmp = c / -b;
} else if (b <= 8e+46) {
tmp = (b + Math.sqrt(((b * b) - (4.0 * (a * c))))) / (a * -2.0);
} else {
tmp = 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): tmp = 0 if b <= -3.5e-69: tmp = c / -b elif b <= 8e+46: tmp = (b + math.sqrt(((b * b) - (4.0 * (a * c))))) / (a * -2.0) else: tmp = 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) tmp = 0.0 if (b <= -3.5e-69) tmp = Float64(c / Float64(-b)); elseif (b <= 8e+46) tmp = Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(a * -2.0)); else tmp = Float64(b / Float64(-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) tmp = 0.0; if (b <= -3.5e-69) tmp = c / -b; elseif (b <= 8e+46) tmp = (b + sqrt(((b * b) - (4.0 * (a * c))))) / (a * -2.0); else tmp = 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_] := If[LessEqual[b, -3.5e-69], N[(c / (-b)), $MachinePrecision], If[LessEqual[b, 8e+46], N[(N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * -2.0), $MachinePrecision]), $MachinePrecision], N[(b / (-a)), $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 -3.5 \cdot 10^{-69}:\\
\;\;\;\;\frac{c}{-b}\\
\mathbf{elif}\;b \leq 8 \cdot 10^{+46}:\\
\;\;\;\;\frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{a \cdot -2}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{-a}\\
\end{array}
Results
| Original | 33.8 |
|---|---|
| Target | 20.7 |
| Herbie | 10.0 |
if b < -3.5000000000000001e-69Initial program 53.2
Simplified53.2
[Start]53.2 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-53 [=>]54.3 | \[ \color{blue}{\frac{-b}{2 \cdot a} - \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}
\] |
rational_best-simplify-54 [=>]53.2 | \[ \color{blue}{\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{-2 \cdot a}}
\] |
rational_best-simplify-12 [<=]53.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{\left(2 \cdot a\right) \cdot -1}}
\] |
rational_best-simplify-2 [=>]53.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{-1 \cdot \left(2 \cdot a\right)}}
\] |
rational_best-simplify-10 [=>]53.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \color{blue}{\left(0 - b\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-49 [=>]53.2 | \[ \frac{\color{blue}{b + \left(\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - 0\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-4 [=>]53.2 | \[ \frac{b + \color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-2 [=>]53.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{-1 \cdot \color{blue}{\left(a \cdot 2\right)}}
\] |
rational_best-simplify-44 [=>]53.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot \left(-1 \cdot 2\right)}}
\] |
metadata-eval [=>]53.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{a \cdot \color{blue}{-2}}
\] |
Taylor expanded in b around -inf 8.9
Simplified8.9
[Start]8.9 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
rational_best-simplify-2 [=>]8.9 | \[ \color{blue}{\frac{c}{b} \cdot -1}
\] |
rational_best-simplify-12 [=>]8.9 | \[ \color{blue}{-\frac{c}{b}}
\] |
rational_best-simplify-9 [=>]8.9 | \[ \color{blue}{\frac{\frac{c}{b}}{-1}}
\] |
rational_best-simplify-48 [=>]8.9 | \[ \color{blue}{\frac{c}{-1 \cdot b}}
\] |
rational_best-simplify-2 [=>]8.9 | \[ \frac{c}{\color{blue}{b \cdot -1}}
\] |
rational_best-simplify-13 [<=]8.9 | \[ \frac{c}{\color{blue}{-b}}
\] |
if -3.5000000000000001e-69 < b < 7.9999999999999999e46Initial program 13.2
Simplified13.2
[Start]13.2 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-53 [=>]13.2 | \[ \color{blue}{\frac{-b}{2 \cdot a} - \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}
\] |
rational_best-simplify-54 [=>]13.2 | \[ \color{blue}{\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{-2 \cdot a}}
\] |
rational_best-simplify-12 [<=]13.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{\left(2 \cdot a\right) \cdot -1}}
\] |
rational_best-simplify-2 [=>]13.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{-1 \cdot \left(2 \cdot a\right)}}
\] |
rational_best-simplify-10 [=>]13.2 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \color{blue}{\left(0 - b\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-49 [=>]13.2 | \[ \frac{\color{blue}{b + \left(\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - 0\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-4 [=>]13.2 | \[ \frac{b + \color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-2 [=>]13.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{-1 \cdot \color{blue}{\left(a \cdot 2\right)}}
\] |
rational_best-simplify-44 [=>]13.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot \left(-1 \cdot 2\right)}}
\] |
metadata-eval [=>]13.2 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{a \cdot \color{blue}{-2}}
\] |
if 7.9999999999999999e46 < b Initial program 36.8
Simplified36.8
[Start]36.8 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
rational_best-simplify-53 [=>]36.7 | \[ \color{blue}{\frac{-b}{2 \cdot a} - \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}
\] |
rational_best-simplify-54 [=>]36.8 | \[ \color{blue}{\frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{-2 \cdot a}}
\] |
rational_best-simplify-12 [<=]36.8 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{\left(2 \cdot a\right) \cdot -1}}
\] |
rational_best-simplify-2 [=>]36.8 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \left(-b\right)}{\color{blue}{-1 \cdot \left(2 \cdot a\right)}}
\] |
rational_best-simplify-10 [=>]36.8 | \[ \frac{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - \color{blue}{\left(0 - b\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-49 [=>]36.8 | \[ \frac{\color{blue}{b + \left(\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)} - 0\right)}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-4 [=>]36.8 | \[ \frac{b + \color{blue}{\sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}}{-1 \cdot \left(2 \cdot a\right)}
\] |
rational_best-simplify-2 [=>]36.8 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{-1 \cdot \color{blue}{\left(a \cdot 2\right)}}
\] |
rational_best-simplify-44 [=>]36.8 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{a \cdot \left(-1 \cdot 2\right)}}
\] |
metadata-eval [=>]36.8 | \[ \frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{a \cdot \color{blue}{-2}}
\] |
Taylor expanded in b around inf 5.7
Simplified5.7
[Start]5.7 | \[ -1 \cdot \frac{b}{a}
\] |
|---|---|
rational_best-simplify-2 [=>]5.7 | \[ \color{blue}{\frac{b}{a} \cdot -1}
\] |
rational_best-simplify-12 [=>]5.7 | \[ \color{blue}{-\frac{b}{a}}
\] |
rational_best-simplify-9 [=>]5.7 | \[ \color{blue}{\frac{\frac{b}{a}}{-1}}
\] |
rational_best-simplify-48 [=>]5.7 | \[ \color{blue}{\frac{b}{-1 \cdot a}}
\] |
rational_best-simplify-2 [<=]5.7 | \[ \frac{b}{\color{blue}{a \cdot -1}}
\] |
rational_best-simplify-13 [<=]5.7 | \[ \frac{b}{\color{blue}{-a}}
\] |
Final simplification10.0
| Alternative 1 | |
|---|---|
| Error | 13.7 |
| Cost | 7368 |
| Alternative 2 | |
|---|---|
| Error | 19.4 |
| Cost | 7112 |
| Alternative 3 | |
|---|---|
| Error | 39.5 |
| Cost | 388 |
| Alternative 4 | |
|---|---|
| Error | 22.5 |
| Cost | 388 |
| Alternative 5 | |
|---|---|
| Error | 56.3 |
| Cost | 192 |
herbie shell --seed 2023096
(FPCore (a b c)
:name "The quadratic formula (r2)"
: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)))