
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
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
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
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
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -1.9e+109)
(/ (- b) a)
(if (<= b 1.6e-23)
(/ (- (sqrt (- (* b b) (* c (* 4.0 a)))) b) (* a 2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.9e+109) {
tmp = -b / a;
} else if (b <= 1.6e-23) {
tmp = (sqrt(((b * b) - (c * (4.0 * a)))) - 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
real(8) :: tmp
if (b <= (-1.9d+109)) then
tmp = -b / a
else if (b <= 1.6d-23) then
tmp = (sqrt(((b * b) - (c * (4.0d0 * a)))) - b) / (a * 2.0d0)
else
tmp = c / -b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.9e+109) {
tmp = -b / a;
} else if (b <= 1.6e-23) {
tmp = (Math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.9e+109: tmp = -b / a elif b <= 1.6e-23: tmp = (math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.9e+109) tmp = Float64(Float64(-b) / a); elseif (b <= 1.6e-23) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a)))) - b) / Float64(a * 2.0)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.9e+109) tmp = -b / a; elseif (b <= 1.6e-23) tmp = (sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.9e+109], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.6e-23], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(4.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.9 \cdot 10^{+109}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.6 \cdot 10^{-23}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.90000000000000019e109Initial program 59.9%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6493.5
Applied rewrites93.5%
if -1.90000000000000019e109 < b < 1.59999999999999988e-23Initial program 80.2%
if 1.59999999999999988e-23 < b Initial program 8.8%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6494.4
Applied rewrites94.4%
Final simplification87.8%
(FPCore (a b c)
:precision binary64
(if (<= b -2.26e+92)
(/ (- b) a)
(if (<= b 1.6e-23)
(* (/ -0.5 a) (- b (sqrt (fma c (* a -4.0) (* b b)))))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.26e+92) {
tmp = -b / a;
} else if (b <= 1.6e-23) {
tmp = (-0.5 / a) * (b - sqrt(fma(c, (a * -4.0), (b * b))));
} else {
tmp = c / -b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -2.26e+92) tmp = Float64(Float64(-b) / a); elseif (b <= 1.6e-23) tmp = Float64(Float64(-0.5 / a) * Float64(b - sqrt(fma(c, Float64(a * -4.0), Float64(b * b))))); else tmp = Float64(c / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -2.26e+92], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.6e-23], N[(N[(-0.5 / a), $MachinePrecision] * N[(b - N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.26 \cdot 10^{+92}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.6 \cdot 10^{-23}:\\
\;\;\;\;\frac{-0.5}{a} \cdot \left(b - \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -2.26e92Initial program 60.6%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6493.6
Applied rewrites93.6%
if -2.26e92 < b < 1.59999999999999988e-23Initial program 80.0%
Applied rewrites79.9%
if 1.59999999999999988e-23 < b Initial program 8.8%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6494.4
Applied rewrites94.4%
Final simplification87.7%
(FPCore (a b c)
:precision binary64
(if (<= b -4.4e-71)
(fma b (/ c (* b b)) (/ (- b) a))
(if (<= b 1.6e-23)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -4.4e-71) {
tmp = fma(b, (c / (b * b)), (-b / a));
} else if (b <= 1.6e-23) {
tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0);
} else {
tmp = c / -b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -4.4e-71) tmp = fma(b, Float64(c / Float64(b * b)), Float64(Float64(-b) / a)); elseif (b <= 1.6e-23) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - b) / Float64(a * 2.0)); else tmp = Float64(c / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -4.4e-71], N[(b * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[((-b) / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.6e-23], N[(N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.4 \cdot 10^{-71}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{c}{b \cdot b}, \frac{-b}{a}\right)\\
\mathbf{elif}\;b \leq 1.6 \cdot 10^{-23}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -4.39999999999999995e-71Initial program 71.1%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-*l/N/A
*-lft-identityN/A
*-commutativeN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
remove-double-negN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6483.8
Applied rewrites83.8%
if -4.39999999999999995e-71 < b < 1.59999999999999988e-23Initial program 76.2%
Taylor expanded in b around 0
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6473.0
Applied rewrites73.0%
if 1.59999999999999988e-23 < b Initial program 8.8%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6494.4
Applied rewrites94.4%
Final simplification83.9%
(FPCore (a b c)
:precision binary64
(if (<= b -4.4e-71)
(fma b (/ c (* b b)) (/ (- b) a))
(if (<= b 1.6e-23)
(* (/ -0.5 a) (- b (sqrt (* -4.0 (* a c)))))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -4.4e-71) {
tmp = fma(b, (c / (b * b)), (-b / a));
} else if (b <= 1.6e-23) {
tmp = (-0.5 / a) * (b - sqrt((-4.0 * (a * c))));
} else {
tmp = c / -b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -4.4e-71) tmp = fma(b, Float64(c / Float64(b * b)), Float64(Float64(-b) / a)); elseif (b <= 1.6e-23) tmp = Float64(Float64(-0.5 / a) * Float64(b - sqrt(Float64(-4.0 * Float64(a * c))))); else tmp = Float64(c / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -4.4e-71], N[(b * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[((-b) / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.6e-23], N[(N[(-0.5 / a), $MachinePrecision] * N[(b - N[Sqrt[N[(-4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.4 \cdot 10^{-71}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{c}{b \cdot b}, \frac{-b}{a}\right)\\
\mathbf{elif}\;b \leq 1.6 \cdot 10^{-23}:\\
\;\;\;\;\frac{-0.5}{a} \cdot \left(b - \sqrt{-4 \cdot \left(a \cdot c\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -4.39999999999999995e-71Initial program 71.1%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-rgt-inN/A
distribute-neg-inN/A
associate-*l/N/A
*-lft-identityN/A
*-commutativeN/A
mul-1-negN/A
distribute-rgt-neg-outN/A
remove-double-negN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6483.8
Applied rewrites83.8%
if -4.39999999999999995e-71 < b < 1.59999999999999988e-23Initial program 76.2%
Applied rewrites76.1%
Taylor expanded in c around inf
lower-*.f64N/A
*-commutativeN/A
lower-*.f6472.9
Applied rewrites72.9%
if 1.59999999999999988e-23 < b Initial program 8.8%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6494.4
Applied rewrites94.4%
Final simplification83.8%
(FPCore (a b c) :precision binary64 (if (<= b 2.2e-286) (/ (- b) a) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.2e-286) {
tmp = -b / a;
} 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
real(8) :: tmp
if (b <= 2.2d-286) then
tmp = -b / a
else
tmp = c / -b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 2.2e-286) {
tmp = -b / a;
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.2e-286: tmp = -b / a else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.2e-286) tmp = Float64(Float64(-b) / a); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 2.2e-286) tmp = -b / a; else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.2e-286], N[((-b) / a), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.2 \cdot 10^{-286}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < 2.1999999999999999e-286Initial program 74.6%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6464.9
Applied rewrites64.9%
if 2.1999999999999999e-286 < b Initial program 29.4%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6469.7
Applied rewrites69.7%
Final simplification67.2%
(FPCore (a b c) :precision binary64 (if (<= b 4e-224) (/ (- b) a) 0.0))
double code(double a, double b, double c) {
double tmp;
if (b <= 4e-224) {
tmp = -b / a;
} else {
tmp = 0.0;
}
return tmp;
}
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 <= 4d-224) then
tmp = -b / a
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 4e-224) {
tmp = -b / a;
} else {
tmp = 0.0;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 4e-224: tmp = -b / a else: tmp = 0.0 return tmp
function code(a, b, c) tmp = 0.0 if (b <= 4e-224) tmp = Float64(Float64(-b) / a); else tmp = 0.0; end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 4e-224) tmp = -b / a; else tmp = 0.0; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 4e-224], N[((-b) / a), $MachinePrecision], 0.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 4 \cdot 10^{-224}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if b < 4.0000000000000001e-224Initial program 74.2%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6460.9
Applied rewrites60.9%
if 4.0000000000000001e-224 < b Initial program 26.3%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
div-subN/A
lower--.f64N/A
Applied rewrites25.2%
lift--.f64N/A
sub-negN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
div-invN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
lift-/.f64N/A
distribute-neg-frac2N/A
lower-/.f64N/A
lift-*.f64N/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
metadata-eval25.2
Applied rewrites25.2%
Taylor expanded in c around 0
distribute-rgt-outN/A
metadata-evalN/A
mul0-rgt19.5
Applied rewrites19.5%
Final simplification42.2%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.0;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 0.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 52.5%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
div-subN/A
lower--.f64N/A
Applied rewrites52.0%
lift--.f64N/A
sub-negN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
div-invN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
lift-/.f64N/A
distribute-neg-frac2N/A
lower-/.f64N/A
lift-*.f64N/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
metadata-eval52.0
Applied rewrites52.0%
Taylor expanded in c around 0
distribute-rgt-outN/A
metadata-evalN/A
mul0-rgt10.4
Applied rewrites10.4%
herbie shell --seed 2024233
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))