
(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(4.0 * Float64(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[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 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(4.0 * Float64(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[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -6.1e-38)
(/ (- c) b)
(if (<= b 1e+132)
(* -0.5 (/ (+ b (sqrt (fma b b (* a (* c -4.0))))) a))
(- (/ c b) (/ b a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -6.1e-38) {
tmp = -c / b;
} else if (b <= 1e+132) {
tmp = -0.5 * ((b + sqrt(fma(b, b, (a * (c * -4.0))))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -6.1e-38) tmp = Float64(Float64(-c) / b); elseif (b <= 1e+132) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(fma(b, b, Float64(a * Float64(c * -4.0))))) / a)); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -6.1e-38], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1e+132], N[(-0.5 * N[(N[(b + N[Sqrt[N[(b * b + N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -6.1 \cdot 10^{-38}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 10^{+132}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -4\right)\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -6.09999999999999972e-38Initial program 13.9%
Taylor expanded in b around -inf 91.6%
associate-*r/91.6%
neg-mul-191.6%
Simplified91.6%
if -6.09999999999999972e-38 < b < 9.99999999999999991e131Initial program 85.0%
/-rgt-identity85.0%
metadata-eval85.0%
associate-/l*84.8%
associate-/r/84.7%
*-commutative84.7%
metadata-eval84.7%
metadata-eval84.7%
associate-*l/84.7%
associate-/r/84.7%
times-frac85.0%
*-commutative85.0%
times-frac85.0%
metadata-eval85.0%
associate-/r/85.0%
*-commutative85.0%
div-sub85.0%
Simplified85.1%
if 9.99999999999999991e131 < b Initial program 47.5%
Taylor expanded in b around inf 98.0%
mul-1-neg98.0%
unsub-neg98.0%
Simplified98.0%
Final simplification89.8%
(FPCore (a b c)
:precision binary64
(if (<= b -9.2e-37)
(/ (- c) b)
(if (<= b 1.8e+131)
(* -0.5 (/ (+ b (sqrt (+ (* a (* c -4.0)) (* b b)))) a))
(- (/ c b) (/ b a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -9.2e-37) {
tmp = -c / b;
} else if (b <= 1.8e+131) {
tmp = -0.5 * ((b + sqrt(((a * (c * -4.0)) + (b * b)))) / a);
} 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
real(8) :: tmp
if (b <= (-9.2d-37)) then
tmp = -c / b
else if (b <= 1.8d+131) then
tmp = (-0.5d0) * ((b + sqrt(((a * (c * (-4.0d0))) + (b * b)))) / a)
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -9.2e-37) {
tmp = -c / b;
} else if (b <= 1.8e+131) {
tmp = -0.5 * ((b + Math.sqrt(((a * (c * -4.0)) + (b * b)))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -9.2e-37: tmp = -c / b elif b <= 1.8e+131: tmp = -0.5 * ((b + math.sqrt(((a * (c * -4.0)) + (b * b)))) / a) else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -9.2e-37) tmp = Float64(Float64(-c) / b); elseif (b <= 1.8e+131) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(Float64(a * Float64(c * -4.0)) + Float64(b * b)))) / a)); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -9.2e-37) tmp = -c / b; elseif (b <= 1.8e+131) tmp = -0.5 * ((b + sqrt(((a * (c * -4.0)) + (b * b)))) / a); else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -9.2e-37], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1.8e+131], N[(-0.5 * N[(N[(b + N[Sqrt[N[(N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.2 \cdot 10^{-37}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 1.8 \cdot 10^{+131}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{a \cdot \left(c \cdot -4\right) + b \cdot b}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -9.1999999999999999e-37Initial program 13.9%
Taylor expanded in b around -inf 91.6%
associate-*r/91.6%
neg-mul-191.6%
Simplified91.6%
if -9.1999999999999999e-37 < b < 1.80000000000000016e131Initial program 85.0%
/-rgt-identity85.0%
metadata-eval85.0%
associate-/l*84.8%
associate-/r/84.7%
*-commutative84.7%
metadata-eval84.7%
metadata-eval84.7%
associate-*l/84.7%
associate-/r/84.7%
times-frac85.0%
*-commutative85.0%
times-frac85.0%
metadata-eval85.0%
associate-/r/85.0%
*-commutative85.0%
div-sub85.0%
Simplified85.1%
fma-udef85.1%
Applied egg-rr85.1%
if 1.80000000000000016e131 < b Initial program 47.5%
Taylor expanded in b around inf 98.0%
mul-1-neg98.0%
unsub-neg98.0%
Simplified98.0%
Final simplification89.8%
(FPCore (a b c)
:precision binary64
(if (<= b -1.25e-39)
(/ (- c) b)
(if (<= b 1.5e-81)
(* -0.5 (/ (+ b (sqrt (* a (* c -4.0)))) a))
(- (/ c b) (/ b a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.25e-39) {
tmp = -c / b;
} else if (b <= 1.5e-81) {
tmp = -0.5 * ((b + sqrt((a * (c * -4.0)))) / a);
} 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
real(8) :: tmp
if (b <= (-1.25d-39)) then
tmp = -c / b
else if (b <= 1.5d-81) then
tmp = (-0.5d0) * ((b + sqrt((a * (c * (-4.0d0))))) / a)
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.25e-39) {
tmp = -c / b;
} else if (b <= 1.5e-81) {
tmp = -0.5 * ((b + Math.sqrt((a * (c * -4.0)))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.25e-39: tmp = -c / b elif b <= 1.5e-81: tmp = -0.5 * ((b + math.sqrt((a * (c * -4.0)))) / a) else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.25e-39) tmp = Float64(Float64(-c) / b); elseif (b <= 1.5e-81) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(a * Float64(c * -4.0)))) / a)); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.25e-39) tmp = -c / b; elseif (b <= 1.5e-81) tmp = -0.5 * ((b + sqrt((a * (c * -4.0)))) / a); else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.25e-39], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1.5e-81], N[(-0.5 * N[(N[(b + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.25 \cdot 10^{-39}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 1.5 \cdot 10^{-81}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{a \cdot \left(c \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -1.25e-39Initial program 13.9%
Taylor expanded in b around -inf 91.6%
associate-*r/91.6%
neg-mul-191.6%
Simplified91.6%
if -1.25e-39 < b < 1.4999999999999999e-81Initial program 80.0%
Simplified80.1%
Taylor expanded in a around inf 77.7%
associate-*r*77.8%
*-commutative77.8%
*-commutative77.8%
Simplified77.8%
if 1.4999999999999999e-81 < b Initial program 69.6%
Taylor expanded in b around inf 85.7%
mul-1-neg85.7%
unsub-neg85.7%
Simplified85.7%
Final simplification85.6%
(FPCore (a b c) :precision binary64 (if (<= b -5e-311) (/ (- c) b) (- (/ c b) (/ b a))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-311) {
tmp = -c / b;
} 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
real(8) :: tmp
if (b <= (-5d-311)) then
tmp = -c / b
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5e-311) {
tmp = -c / b;
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-311: tmp = -c / b else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-311) tmp = Float64(Float64(-c) / b); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5e-311) tmp = -c / b; else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-311], N[((-c) / b), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-311}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -5.00000000000023e-311Initial program 32.3%
Taylor expanded in b around -inf 69.0%
associate-*r/69.0%
neg-mul-169.0%
Simplified69.0%
if -5.00000000000023e-311 < b Initial program 73.1%
Taylor expanded in b around inf 66.5%
mul-1-neg66.5%
unsub-neg66.5%
Simplified66.5%
Final simplification67.8%
(FPCore (a b c) :precision binary64 (if (<= b -5e-311) (/ (- c) b) (/ (- b) a)))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-311) {
tmp = -c / b;
} 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
real(8) :: tmp
if (b <= (-5d-311)) then
tmp = -c / b
else
tmp = -b / a
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5e-311) {
tmp = -c / b;
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-311: tmp = -c / b else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-311) tmp = Float64(Float64(-c) / b); else tmp = Float64(Float64(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5e-311) tmp = -c / b; else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-311], N[((-c) / b), $MachinePrecision], N[((-b) / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-311}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -5.00000000000023e-311Initial program 32.3%
Taylor expanded in b around -inf 69.0%
associate-*r/69.0%
neg-mul-169.0%
Simplified69.0%
if -5.00000000000023e-311 < b Initial program 73.1%
Taylor expanded in b around inf 66.2%
associate-*r/66.2%
mul-1-neg66.2%
Simplified66.2%
Final simplification67.6%
(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(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}
Initial program 52.6%
Taylor expanded in b around inf 34.3%
associate-*r/34.3%
mul-1-neg34.3%
Simplified34.3%
Final simplification34.3%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (- (* b b) (* 4.0 (* a c))))))
(if (< b 0.0)
(/ c (* a (/ (+ (- b) t_0) (* 2.0 a))))
(/ (- (- b) t_0) (* 2.0 a)))))
double code(double a, double b, double c) {
double t_0 = sqrt(((b * b) - (4.0 * (a * c))));
double tmp;
if (b < 0.0) {
tmp = c / (a * ((-b + t_0) / (2.0 * a)));
} else {
tmp = (-b - t_0) / (2.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(((b * b) - (4.0d0 * (a * c))))
if (b < 0.0d0) then
tmp = c / (a * ((-b + t_0) / (2.0d0 * a)))
else
tmp = (-b - t_0) / (2.0d0 * a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt(((b * b) - (4.0 * (a * c))));
double tmp;
if (b < 0.0) {
tmp = c / (a * ((-b + t_0) / (2.0 * a)));
} else {
tmp = (-b - t_0) / (2.0 * a);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt(((b * b) - (4.0 * (a * c)))) tmp = 0 if b < 0.0: tmp = c / (a * ((-b + t_0) / (2.0 * a))) else: tmp = (-b - t_0) / (2.0 * a) return tmp
function code(a, b, c) t_0 = sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c)))) tmp = 0.0 if (b < 0.0) tmp = Float64(c / Float64(a * Float64(Float64(Float64(-b) + t_0) / Float64(2.0 * a)))); else tmp = Float64(Float64(Float64(-b) - t_0) / Float64(2.0 * a)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt(((b * b) - (4.0 * (a * c)))); tmp = 0.0; if (b < 0.0) tmp = c / (a * ((-b + t_0) / (2.0 * a))); else tmp = (-b - t_0) / (2.0 * a); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[b, 0.0], N[(c / N[(a * N[(N[((-b) + t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-b) - t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\\
\mathbf{if}\;b < 0:\\
\;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) + t_0}{2 \cdot a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-b\right) - t_0}{2 \cdot a}\\
\end{array}
\end{array}
herbie shell --seed 2023182
(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)))