
(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 8 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.7e+104)
(/ (- b) a)
(if (<= b 5e-23)
(/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.7e+104) {
tmp = -b / a;
} else if (b <= 5e-23) {
tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - 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.7d+104)) then
tmp = -b / a
else if (b <= 5d-23) then
tmp = (sqrt(((b * b) - ((a * 4.0d0) * c))) - 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.7e+104) {
tmp = -b / a;
} else if (b <= 5e-23) {
tmp = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.7e+104: tmp = -b / a elif b <= 5e-23: tmp = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.7e+104) tmp = Float64(Float64(-b) / a); elseif (b <= 5e-23) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.7e+104) tmp = -b / a; elseif (b <= 5e-23) tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.7e+104], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 5e-23], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * 4.0), $MachinePrecision] * c), $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.7 \cdot 10^{+104}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 5 \cdot 10^{-23}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -4e-44)
(/ (- b) a)
(if (or (<= b 3.2e-67) (and (not (<= b 7.4e-32)) (<= b 6.8e-21)))
(* 0.5 (/ (sqrt (* a (* c -4.0))) a))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -4e-44) {
tmp = -b / a;
} else if ((b <= 3.2e-67) || (!(b <= 7.4e-32) && (b <= 6.8e-21))) {
tmp = 0.5 * (sqrt((a * (c * -4.0))) / 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 <= (-4d-44)) then
tmp = -b / a
else if ((b <= 3.2d-67) .or. (.not. (b <= 7.4d-32)) .and. (b <= 6.8d-21)) then
tmp = 0.5d0 * (sqrt((a * (c * (-4.0d0)))) / 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 <= -4e-44) {
tmp = -b / a;
} else if ((b <= 3.2e-67) || (!(b <= 7.4e-32) && (b <= 6.8e-21))) {
tmp = 0.5 * (Math.sqrt((a * (c * -4.0))) / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -4e-44: tmp = -b / a elif (b <= 3.2e-67) or (not (b <= 7.4e-32) and (b <= 6.8e-21)): tmp = 0.5 * (math.sqrt((a * (c * -4.0))) / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -4e-44) tmp = Float64(Float64(-b) / a); elseif ((b <= 3.2e-67) || (!(b <= 7.4e-32) && (b <= 6.8e-21))) tmp = Float64(0.5 * Float64(sqrt(Float64(a * Float64(c * -4.0))) / a)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -4e-44) tmp = -b / a; elseif ((b <= 3.2e-67) || (~((b <= 7.4e-32)) && (b <= 6.8e-21))) tmp = 0.5 * (sqrt((a * (c * -4.0))) / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -4e-44], N[((-b) / a), $MachinePrecision], If[Or[LessEqual[b, 3.2e-67], And[N[Not[LessEqual[b, 7.4e-32]], $MachinePrecision], LessEqual[b, 6.8e-21]]], N[(0.5 * N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4 \cdot 10^{-44}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 3.2 \cdot 10^{-67} \lor \neg \left(b \leq 7.4 \cdot 10^{-32}\right) \land b \leq 6.8 \cdot 10^{-21}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{a \cdot \left(c \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (* a (* c -4.0)))))
(if (<= b -5e-50)
(/ (- b) a)
(if (<= b 2.8e-67)
(* 0.5 (* t_0 (/ 1.0 a)))
(if (or (<= b 3.1e-31) (not (<= b 1.55e-24)))
(/ (- c) b)
(* 0.5 (/ t_0 a)))))))
double code(double a, double b, double c) {
double t_0 = sqrt((a * (c * -4.0)));
double tmp;
if (b <= -5e-50) {
tmp = -b / a;
} else if (b <= 2.8e-67) {
tmp = 0.5 * (t_0 * (1.0 / a));
} else if ((b <= 3.1e-31) || !(b <= 1.55e-24)) {
tmp = -c / b;
} else {
tmp = 0.5 * (t_0 / 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
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((a * (c * (-4.0d0))))
if (b <= (-5d-50)) then
tmp = -b / a
else if (b <= 2.8d-67) then
tmp = 0.5d0 * (t_0 * (1.0d0 / a))
else if ((b <= 3.1d-31) .or. (.not. (b <= 1.55d-24))) then
tmp = -c / b
else
tmp = 0.5d0 * (t_0 / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt((a * (c * -4.0)));
double tmp;
if (b <= -5e-50) {
tmp = -b / a;
} else if (b <= 2.8e-67) {
tmp = 0.5 * (t_0 * (1.0 / a));
} else if ((b <= 3.1e-31) || !(b <= 1.55e-24)) {
tmp = -c / b;
} else {
tmp = 0.5 * (t_0 / a);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt((a * (c * -4.0))) tmp = 0 if b <= -5e-50: tmp = -b / a elif b <= 2.8e-67: tmp = 0.5 * (t_0 * (1.0 / a)) elif (b <= 3.1e-31) or not (b <= 1.55e-24): tmp = -c / b else: tmp = 0.5 * (t_0 / a) return tmp
function code(a, b, c) t_0 = sqrt(Float64(a * Float64(c * -4.0))) tmp = 0.0 if (b <= -5e-50) tmp = Float64(Float64(-b) / a); elseif (b <= 2.8e-67) tmp = Float64(0.5 * Float64(t_0 * Float64(1.0 / a))); elseif ((b <= 3.1e-31) || !(b <= 1.55e-24)) tmp = Float64(Float64(-c) / b); else tmp = Float64(0.5 * Float64(t_0 / a)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt((a * (c * -4.0))); tmp = 0.0; if (b <= -5e-50) tmp = -b / a; elseif (b <= 2.8e-67) tmp = 0.5 * (t_0 * (1.0 / a)); elseif ((b <= 3.1e-31) || ~((b <= 1.55e-24))) tmp = -c / b; else tmp = 0.5 * (t_0 / a); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[b, -5e-50], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 2.8e-67], N[(0.5 * N[(t$95$0 * N[(1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[b, 3.1e-31], N[Not[LessEqual[b, 1.55e-24]], $MachinePrecision]], N[((-c) / b), $MachinePrecision], N[(0.5 * N[(t$95$0 / a), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{a \cdot \left(c \cdot -4\right)}\\
\mathbf{if}\;b \leq -5 \cdot 10^{-50}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 2.8 \cdot 10^{-67}:\\
\;\;\;\;0.5 \cdot \left(t_0 \cdot \frac{1}{a}\right)\\
\mathbf{elif}\;b \leq 3.1 \cdot 10^{-31} \lor \neg \left(b \leq 1.55 \cdot 10^{-24}\right):\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{t_0}{a}\\
\end{array}
\end{array}
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (* a (* c -4.0)))))
(if (<= b -4.3e-38)
(/ (- b) a)
(if (<= b 2.85e-67)
(/ (- t_0 b) (* a 2.0))
(if (or (<= b 8.5e-32) (not (<= b 4.9e-23)))
(/ (- c) b)
(* 0.5 (/ t_0 a)))))))
double code(double a, double b, double c) {
double t_0 = sqrt((a * (c * -4.0)));
double tmp;
if (b <= -4.3e-38) {
tmp = -b / a;
} else if (b <= 2.85e-67) {
tmp = (t_0 - b) / (a * 2.0);
} else if ((b <= 8.5e-32) || !(b <= 4.9e-23)) {
tmp = -c / b;
} else {
tmp = 0.5 * (t_0 / 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
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt((a * (c * (-4.0d0))))
if (b <= (-4.3d-38)) then
tmp = -b / a
else if (b <= 2.85d-67) then
tmp = (t_0 - b) / (a * 2.0d0)
else if ((b <= 8.5d-32) .or. (.not. (b <= 4.9d-23))) then
tmp = -c / b
else
tmp = 0.5d0 * (t_0 / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt((a * (c * -4.0)));
double tmp;
if (b <= -4.3e-38) {
tmp = -b / a;
} else if (b <= 2.85e-67) {
tmp = (t_0 - b) / (a * 2.0);
} else if ((b <= 8.5e-32) || !(b <= 4.9e-23)) {
tmp = -c / b;
} else {
tmp = 0.5 * (t_0 / a);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt((a * (c * -4.0))) tmp = 0 if b <= -4.3e-38: tmp = -b / a elif b <= 2.85e-67: tmp = (t_0 - b) / (a * 2.0) elif (b <= 8.5e-32) or not (b <= 4.9e-23): tmp = -c / b else: tmp = 0.5 * (t_0 / a) return tmp
function code(a, b, c) t_0 = sqrt(Float64(a * Float64(c * -4.0))) tmp = 0.0 if (b <= -4.3e-38) tmp = Float64(Float64(-b) / a); elseif (b <= 2.85e-67) tmp = Float64(Float64(t_0 - b) / Float64(a * 2.0)); elseif ((b <= 8.5e-32) || !(b <= 4.9e-23)) tmp = Float64(Float64(-c) / b); else tmp = Float64(0.5 * Float64(t_0 / a)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt((a * (c * -4.0))); tmp = 0.0; if (b <= -4.3e-38) tmp = -b / a; elseif (b <= 2.85e-67) tmp = (t_0 - b) / (a * 2.0); elseif ((b <= 8.5e-32) || ~((b <= 4.9e-23))) tmp = -c / b; else tmp = 0.5 * (t_0 / a); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[b, -4.3e-38], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 2.85e-67], N[(N[(t$95$0 - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[b, 8.5e-32], N[Not[LessEqual[b, 4.9e-23]], $MachinePrecision]], N[((-c) / b), $MachinePrecision], N[(0.5 * N[(t$95$0 / a), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{a \cdot \left(c \cdot -4\right)}\\
\mathbf{if}\;b \leq -4.3 \cdot 10^{-38}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 2.85 \cdot 10^{-67}:\\
\;\;\;\;\frac{t_0 - b}{a \cdot 2}\\
\mathbf{elif}\;b \leq 8.5 \cdot 10^{-32} \lor \neg \left(b \leq 4.9 \cdot 10^{-23}\right):\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{t_0}{a}\\
\end{array}
\end{array}
(FPCore (a b c) :precision binary64 (if (<= b 3.4e+22) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.4e+22) {
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 <= 3.4d+22) 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 <= 3.4e+22) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.4e+22: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.4e+22) tmp = Float64(Float64(-b) / a); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 3.4e+22) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.4e+22], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.4 \cdot 10^{+22}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
(FPCore (a b c) :precision binary64 (if (<= b 2.6e-264) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.6e-264) {
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.6d-264) 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.6e-264) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.6e-264: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.6e-264) tmp = Float64(Float64(-b) / a); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 2.6e-264) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.6e-264], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.6 \cdot 10^{-264}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
(FPCore (a b c) :precision binary64 (/ b a))
double code(double a, double b, double c) {
return b / 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 / a
end function
public static double code(double a, double b, double c) {
return b / a;
}
def code(a, b, c): return b / a
function code(a, b, c) return Float64(b / a) end
function tmp = code(a, b, c) tmp = b / a; end
code[a_, b_, c_] := N[(b / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{a}
\end{array}
(FPCore (a b c) :precision binary64 (/ c b))
double code(double a, double b, double c) {
return c / b;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = c / b
end function
public static double code(double a, double b, double c) {
return c / b;
}
def code(a, b, c): return c / b
function code(a, b, c) return Float64(c / b) end
function tmp = code(a, b, c) tmp = c / b; end
code[a_, b_, c_] := N[(c / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{b}
\end{array}
herbie shell --seed 2023342
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))