| Alternative 1 | |
|---|---|
| Error | 15.71% |
| Cost | 7624 |
(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 -4.2e-62)
(/ (- c) b)
(if (<= b 1.1e+125)
(/ (- (- b) (sqrt (- (* b b) (* 4.0 (* c a))))) (* a 2.0))
(- (/ 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 tmp;
if (b <= -4.2e-62) {
tmp = -c / b;
} else if (b <= 1.1e+125) {
tmp = (-b - sqrt(((b * b) - (4.0 * (c * a))))) / (a * 2.0);
} else {
tmp = (c / b) - (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 <= (-4.2d-62)) then
tmp = -c / b
else if (b <= 1.1d+125) then
tmp = (-b - sqrt(((b * b) - (4.0d0 * (c * a))))) / (a * 2.0d0)
else
tmp = (c / b) - (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 <= -4.2e-62) {
tmp = -c / b;
} else if (b <= 1.1e+125) {
tmp = (-b - Math.sqrt(((b * b) - (4.0 * (c * a))))) / (a * 2.0);
} 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): tmp = 0 if b <= -4.2e-62: tmp = -c / b elif b <= 1.1e+125: tmp = (-b - math.sqrt(((b * b) - (4.0 * (c * a))))) / (a * 2.0) 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) tmp = 0.0 if (b <= -4.2e-62) tmp = Float64(Float64(-c) / b); elseif (b <= 1.1e+125) tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(c * a))))) / Float64(a * 2.0)); 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) tmp = 0.0; if (b <= -4.2e-62) tmp = -c / b; elseif (b <= 1.1e+125) tmp = (-b - sqrt(((b * b) - (4.0 * (c * a))))) / (a * 2.0); 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_] := If[LessEqual[b, -4.2e-62], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1.1e+125], N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $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}
\mathbf{if}\;b \leq -4.2 \cdot 10^{-62}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 1.1 \cdot 10^{+125}:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
Results
| Original | 53.48% |
|---|---|
| Target | 32.79% |
| Herbie | 15.56% |
if b < -4.1999999999999998e-62Initial program 84.38
Simplified84.38
[Start]84.38 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
*-rgt-identity [<=]84.38 | \[ \color{blue}{\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot 1}
\] |
metadata-eval [<=]84.38 | \[ \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/ [=>]84.38 | \[ \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/ [<=]84.38 | \[ \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 [<=]84.38 | \[ \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 [<=]84.38 | \[ \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 [<=]84.38 | \[ \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 13.8
Simplified13.8
[Start]13.8 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
mul-1-neg [=>]13.8 | \[ \color{blue}{-\frac{c}{b}}
\] |
distribute-neg-frac [=>]13.8 | \[ \color{blue}{\frac{-c}{b}}
\] |
if -4.1999999999999998e-62 < b < 1.09999999999999995e125Initial program 20.09
if 1.09999999999999995e125 < b Initial program 85.27
Simplified85.34
[Start]85.27 | \[ \frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
*-rgt-identity [<=]85.27 | \[ \color{blue}{\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \cdot 1}
\] |
metadata-eval [<=]85.27 | \[ \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/ [=>]85.27 | \[ \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/ [<=]85.34 | \[ \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 [<=]85.34 | \[ \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 [<=]85.34 | \[ \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 [<=]85.34 | \[ \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 16.28
Simplified16.28
[Start]16.28 | \[ \left(b + \left(b + -2 \cdot \frac{c \cdot a}{b}\right)\right) \cdot \frac{-0.5}{a}
\] |
|---|---|
associate-*r/ [=>]16.28 | \[ \left(b + \left(b + \color{blue}{\frac{-2 \cdot \left(c \cdot a\right)}{b}}\right)\right) \cdot \frac{-0.5}{a}
\] |
*-commutative [=>]16.28 | \[ \left(b + \left(b + \frac{\color{blue}{\left(c \cdot a\right) \cdot -2}}{b}\right)\right) \cdot \frac{-0.5}{a}
\] |
Taylor expanded in b around 0 3.83
Simplified3.83
[Start]3.83 | \[ \frac{c}{b} + -1 \cdot \frac{b}{a}
\] |
|---|---|
mul-1-neg [=>]3.83 | \[ \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)}
\] |
unsub-neg [=>]3.83 | \[ \color{blue}{\frac{c}{b} - \frac{b}{a}}
\] |
Final simplification15.56
| Alternative 1 | |
|---|---|
| Error | 15.71% |
| Cost | 7624 |
| Alternative 2 | |
|---|---|
| Error | 21.02% |
| Cost | 7368 |
| Alternative 3 | |
|---|---|
| Error | 20.98% |
| Cost | 7368 |
| Alternative 4 | |
|---|---|
| Error | 35.71% |
| Cost | 580 |
| Alternative 5 | |
|---|---|
| Error | 62.58% |
| Cost | 388 |
| Alternative 6 | |
|---|---|
| Error | 35.81% |
| Cost | 388 |
| Alternative 7 | |
|---|---|
| Error | 88.59% |
| Cost | 192 |
herbie shell --seed 2023090
(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)))