(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 -1.9e-118)
(/ (- c) b)
(if (<= b 9e+124)
(/ (- (- b) (sqrt (+ (* b b) (* (* c a) -4.0)))) (* 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 <= -1.9e-118) {
tmp = -c / b;
} else if (b <= 9e+124) {
tmp = (-b - sqrt(((b * b) + ((c * a) * -4.0)))) / (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 <= (-1.9d-118)) then
tmp = -c / b
else if (b <= 9d+124) then
tmp = (-b - sqrt(((b * b) + ((c * a) * (-4.0d0))))) / (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 <= -1.9e-118) {
tmp = -c / b;
} else if (b <= 9e+124) {
tmp = (-b - Math.sqrt(((b * b) + ((c * a) * -4.0)))) / (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 <= -1.9e-118: tmp = -c / b elif b <= 9e+124: tmp = (-b - math.sqrt(((b * b) + ((c * a) * -4.0)))) / (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 <= -1.9e-118) tmp = Float64(Float64(-c) / b); elseif (b <= 9e+124) tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(Float64(c * a) * -4.0)))) / Float64(a * 2.0)); else tmp = Float64(-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 <= -1.9e-118) tmp = -c / b; elseif (b <= 9e+124) tmp = (-b - sqrt(((b * b) + ((c * a) * -4.0)))) / (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, -1.9e-118], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 9e+124], N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] * -4.0), $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 -1.9 \cdot 10^{-118}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 9 \cdot 10^{+124}:\\
\;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4}}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;-\frac{b}{a}\\
\end{array}
Results
| Original | 34.0 |
|---|---|
| Target | 20.9 |
| Herbie | 10.2 |
if b < -1.9e-118Initial program 51.5
Simplified51.5
Taylor expanded in b around -inf 11.6
Simplified11.6
if -1.9e-118 < b < 9.0000000000000008e124Initial program 11.3
if 9.0000000000000008e124 < b Initial program 53.3
Simplified53.4
Applied egg-rr36.0
Taylor expanded in a around 0 2.5
Simplified2.5
Final simplification10.2
herbie shell --seed 2022209
(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)))