
(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.5e+127)
(/ b (- a))
(if (<= b 1.4e-28)
(/ (- (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.5e+127) {
tmp = b / -a;
} else if (b <= 1.4e-28) {
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.5d+127)) then
tmp = b / -a
else if (b <= 1.4d-28) 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.5e+127) {
tmp = b / -a;
} else if (b <= 1.4e-28) {
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.5e+127: tmp = b / -a elif b <= 1.4e-28: 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.5e+127) tmp = Float64(b / Float64(-a)); elseif (b <= 1.4e-28) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - 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.5e+127) tmp = b / -a; elseif (b <= 1.4e-28) 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.5e+127], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 1.4e-28], 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.5 \cdot 10^{+127}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 1.4 \cdot 10^{-28}:\\
\;\;\;\;\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}
if b < -1.5000000000000001e127Initial program 49.2%
*-commutative49.2%
Simplified49.2%
Taylor expanded in b around -inf 95.8%
associate-*r/95.8%
mul-1-neg95.8%
Simplified95.8%
if -1.5000000000000001e127 < b < 1.3999999999999999e-28Initial program 84.5%
if 1.3999999999999999e-28 < b Initial program 12.3%
*-commutative12.3%
Simplified12.4%
Taylor expanded in a around 0 93.3%
associate-*r/93.3%
mul-1-neg93.3%
Simplified93.3%
Final simplification89.2%
(FPCore (a b c)
:precision binary64
(if (<= b -1.65e-62)
(/ b (- a))
(if (<= b 4.5e-28)
(/ (- (sqrt (* c (* a -4.0))) b) (* a 2.0))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.65e-62) {
tmp = b / -a;
} else if (b <= 4.5e-28) {
tmp = (sqrt((c * (a * -4.0))) - 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.65d-62)) then
tmp = b / -a
else if (b <= 4.5d-28) then
tmp = (sqrt((c * (a * (-4.0d0)))) - 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.65e-62) {
tmp = b / -a;
} else if (b <= 4.5e-28) {
tmp = (Math.sqrt((c * (a * -4.0))) - b) / (a * 2.0);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.65e-62: tmp = b / -a elif b <= 4.5e-28: tmp = (math.sqrt((c * (a * -4.0))) - b) / (a * 2.0) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.65e-62) tmp = Float64(b / Float64(-a)); elseif (b <= 4.5e-28) tmp = Float64(Float64(sqrt(Float64(c * Float64(a * -4.0))) - 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.65e-62) tmp = b / -a; elseif (b <= 4.5e-28) tmp = (sqrt((c * (a * -4.0))) - b) / (a * 2.0); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.65e-62], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 4.5e-28], N[(N[(N[Sqrt[N[(c * N[(a * -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 -1.65 \cdot 10^{-62}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 4.5 \cdot 10^{-28}:\\
\;\;\;\;\frac{\sqrt{c \cdot \left(a \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.65000000000000002e-62Initial program 72.3%
*-commutative72.3%
Simplified72.3%
Taylor expanded in b around -inf 87.9%
associate-*r/87.9%
mul-1-neg87.9%
Simplified87.9%
if -1.65000000000000002e-62 < b < 4.4999999999999998e-28Initial program 78.5%
*-commutative78.5%
Simplified78.5%
Taylor expanded in a around inf 73.3%
associate-*r*73.3%
*-commutative73.3%
Simplified73.3%
if 4.4999999999999998e-28 < b Initial program 12.3%
*-commutative12.3%
Simplified12.4%
Taylor expanded in a around 0 93.3%
associate-*r/93.3%
mul-1-neg93.3%
Simplified93.3%
Final simplification84.5%
(FPCore (a b c) :precision binary64 (if (<= b -2.8e-100) (/ b (- a)) (if (<= b 9e-68) (* -0.5 (- (sqrt (* c (/ -4.0 a))))) (/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.8e-100) {
tmp = b / -a;
} else if (b <= 9e-68) {
tmp = -0.5 * -sqrt((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 <= (-2.8d-100)) then
tmp = b / -a
else if (b <= 9d-68) then
tmp = (-0.5d0) * -sqrt((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 <= -2.8e-100) {
tmp = b / -a;
} else if (b <= 9e-68) {
tmp = -0.5 * -Math.sqrt((c * (-4.0 / a)));
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2.8e-100: tmp = b / -a elif b <= 9e-68: tmp = -0.5 * -math.sqrt((c * (-4.0 / a))) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2.8e-100) tmp = Float64(b / Float64(-a)); elseif (b <= 9e-68) tmp = Float64(-0.5 * Float64(-sqrt(Float64(c * Float64(-4.0 / a))))); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -2.8e-100) tmp = b / -a; elseif (b <= 9e-68) tmp = -0.5 * -sqrt((c * (-4.0 / a))); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2.8e-100], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 9e-68], N[(-0.5 * (-N[Sqrt[N[(c * N[(-4.0 / a), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.8 \cdot 10^{-100}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 9 \cdot 10^{-68}:\\
\;\;\;\;-0.5 \cdot \left(-\sqrt{c \cdot \frac{-4}{a}}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -2.79999999999999995e-100Initial program 74.9%
*-commutative74.9%
Simplified74.9%
Taylor expanded in b around -inf 83.1%
associate-*r/83.1%
mul-1-neg83.1%
Simplified83.1%
if -2.79999999999999995e-100 < b < 8.99999999999999998e-68Initial program 76.7%
*-commutative76.7%
Simplified76.7%
add-cube-cbrt76.0%
pow376.1%
Applied egg-rr76.1%
Taylor expanded in a around -inf 0.0%
*-commutative0.0%
unpow20.0%
rem-square-sqrt40.1%
associate-/l*40.1%
rem-cube-cbrt40.4%
Simplified40.4%
if 8.99999999999999998e-68 < b Initial program 16.3%
*-commutative16.3%
Simplified16.3%
Taylor expanded in a around 0 89.4%
associate-*r/89.4%
mul-1-neg89.4%
Simplified89.4%
Final simplification72.7%
(FPCore (a b c) :precision binary64 (if (<= b -2e-310) (/ b (- a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2e-310) {
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 <= (-2d-310)) 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 <= -2e-310) {
tmp = b / -a;
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2e-310: tmp = b / -a else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2e-310) tmp = Float64(b / Float64(-a)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -2e-310) tmp = b / -a; else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2e-310], N[(b / (-a)), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2 \cdot 10^{-310}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.999999999999994e-310Initial program 77.5%
*-commutative77.5%
Simplified77.5%
Taylor expanded in b around -inf 64.5%
associate-*r/64.5%
mul-1-neg64.5%
Simplified64.5%
if -1.999999999999994e-310 < b Initial program 33.0%
*-commutative33.0%
Simplified33.0%
Taylor expanded in a around 0 66.7%
associate-*r/66.7%
mul-1-neg66.7%
Simplified66.7%
Final simplification65.6%
(FPCore (a b c) :precision binary64 (if (<= b 2.8e-27) (/ b (- a)) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.8e-27) {
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.8d-27) 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.8e-27) {
tmp = b / -a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.8e-27: tmp = b / -a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.8e-27) tmp = Float64(b / Float64(-a)); else tmp = Float64(c / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 2.8e-27) tmp = b / -a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.8e-27], N[(b / (-a)), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.8 \cdot 10^{-27}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 2.8e-27Initial program 75.5%
*-commutative75.5%
Simplified75.5%
Taylor expanded in b around -inf 48.7%
associate-*r/48.7%
mul-1-neg48.7%
Simplified48.7%
if 2.8e-27 < b Initial program 12.3%
*-commutative12.3%
Simplified12.4%
Applied egg-rr4.8%
unpow-14.8%
associate-/l*4.8%
Simplified4.8%
Taylor expanded in b around -inf 25.6%
Final simplification41.5%
(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}
Initial program 55.7%
*-commutative55.7%
Simplified55.7%
Applied egg-rr36.1%
unpow-136.1%
associate-/l*36.1%
Simplified36.1%
Taylor expanded in b around -inf 10.1%
(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}
Initial program 55.7%
*-commutative55.7%
Simplified55.7%
Applied egg-rr36.1%
unpow-136.1%
associate-/l*36.1%
Simplified36.1%
Taylor expanded in a around 0 2.6%
herbie shell --seed 2024129
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))