
(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 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(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 -5e+147)
(- (/ b a))
(if (<= b 5.6e-34)
(/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
(/ -1.0 (- (/ b c) (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e+147) {
tmp = -(b / a);
} else if (b <= 5.6e-34) {
tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = -1.0 / ((b / c) - (a / 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 <= (-5d+147)) then
tmp = -(b / a)
else if (b <= 5.6d-34) then
tmp = (sqrt(((b * b) - ((a * 4.0d0) * c))) - b) / (a * 2.0d0)
else
tmp = (-1.0d0) / ((b / c) - (a / b))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5e+147) {
tmp = -(b / a);
} else if (b <= 5.6e-34) {
tmp = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
} else {
tmp = -1.0 / ((b / c) - (a / b));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e+147: tmp = -(b / a) elif b <= 5.6e-34: tmp = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0) else: tmp = -1.0 / ((b / c) - (a / b)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e+147) tmp = Float64(-Float64(b / a)); elseif (b <= 5.6e-34) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0)); else tmp = Float64(-1.0 / Float64(Float64(b / c) - Float64(a / b))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5e+147) tmp = -(b / a); elseif (b <= 5.6e-34) tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0); else tmp = -1.0 / ((b / c) - (a / b)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e+147], (-N[(b / a), $MachinePrecision]), If[LessEqual[b, 5.6e-34], 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[(-1.0 / N[(N[(b / c), $MachinePrecision] - N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{+147}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{elif}\;b \leq 5.6 \cdot 10^{-34}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{b}{c} - \frac{a}{b}}\\
\end{array}
\end{array}
if b < -5.0000000000000002e147Initial program 42.2%
*-commutative42.2%
Simplified42.2%
Taylor expanded in b around -inf 94.0%
associate-*r/94.0%
mul-1-neg94.0%
Simplified94.0%
if -5.0000000000000002e147 < b < 5.59999999999999994e-34Initial program 81.9%
if 5.59999999999999994e-34 < b Initial program 7.4%
*-commutative7.4%
Simplified7.4%
clear-num7.4%
inv-pow7.4%
Applied egg-rr2.9%
unpow-12.9%
Simplified2.9%
associate-/l*2.9%
frac-2neg2.9%
metadata-eval2.9%
hypot-undefine2.7%
add-sqr-sqrt3.2%
fma-undefine3.2%
add-cube-cbrt3.2%
unpow33.2%
distribute-neg-in3.2%
sub-neg3.2%
add-sqr-sqrt0.0%
Applied egg-rr17.1%
Taylor expanded in a around 0 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
times-frac0.0%
unpow20.0%
rem-square-sqrt94.2%
metadata-eval94.2%
neg-mul-194.2%
distribute-neg-frac294.2%
Simplified94.2%
Final simplification88.1%
(FPCore (a b c)
:precision binary64
(if (<= b -2.5e-82)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 2.7e-30)
(/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
(/ -1.0 (- (/ b c) (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.5e-82) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2.7e-30) {
tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0);
} else {
tmp = -1.0 / ((b / c) - (a / 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.5d-82)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 2.7d-30) then
tmp = (sqrt((a * (c * (-4.0d0)))) - b) / (a * 2.0d0)
else
tmp = (-1.0d0) / ((b / c) - (a / b))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -2.5e-82) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2.7e-30) {
tmp = (Math.sqrt((a * (c * -4.0))) - b) / (a * 2.0);
} else {
tmp = -1.0 / ((b / c) - (a / b));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2.5e-82: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 2.7e-30: tmp = (math.sqrt((a * (c * -4.0))) - b) / (a * 2.0) else: tmp = -1.0 / ((b / c) - (a / b)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2.5e-82) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 2.7e-30) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - b) / Float64(a * 2.0)); else tmp = Float64(-1.0 / Float64(Float64(b / c) - Float64(a / b))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -2.5e-82) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 2.7e-30) tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0); else tmp = -1.0 / ((b / c) - (a / b)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2.5e-82], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.7e-30], N[(N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(N[(b / c), $MachinePrecision] - N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.5 \cdot 10^{-82}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 2.7 \cdot 10^{-30}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{b}{c} - \frac{a}{b}}\\
\end{array}
\end{array}
if b < -2.4999999999999999e-82Initial program 69.3%
*-commutative69.3%
Simplified69.3%
Taylor expanded in b around -inf 89.3%
mul-1-neg89.3%
*-commutative89.3%
distribute-rgt-neg-in89.3%
+-commutative89.3%
mul-1-neg89.3%
unsub-neg89.3%
Simplified89.3%
if -2.4999999999999999e-82 < b < 2.69999999999999987e-30Initial program 74.0%
*-commutative74.0%
Simplified74.0%
Taylor expanded in b around 0 66.6%
*-commutative66.6%
associate-*r*66.6%
Simplified66.6%
if 2.69999999999999987e-30 < b Initial program 7.4%
*-commutative7.4%
Simplified7.4%
clear-num7.4%
inv-pow7.4%
Applied egg-rr2.9%
unpow-12.9%
Simplified2.9%
associate-/l*2.9%
frac-2neg2.9%
metadata-eval2.9%
hypot-undefine2.7%
add-sqr-sqrt3.2%
fma-undefine3.2%
add-cube-cbrt3.2%
unpow33.2%
distribute-neg-in3.2%
sub-neg3.2%
add-sqr-sqrt0.0%
Applied egg-rr17.1%
Taylor expanded in a around 0 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
times-frac0.0%
unpow20.0%
rem-square-sqrt94.2%
metadata-eval94.2%
neg-mul-194.2%
distribute-neg-frac294.2%
Simplified94.2%
Final simplification84.1%
(FPCore (a b c) :precision binary64 (if (<= b -3.6e-302) (- (/ b a)) (/ -1.0 (- (/ b c) (/ a b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.6e-302) {
tmp = -(b / a);
} else {
tmp = -1.0 / ((b / c) - (a / 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.6d-302)) then
tmp = -(b / a)
else
tmp = (-1.0d0) / ((b / c) - (a / b))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -3.6e-302) {
tmp = -(b / a);
} else {
tmp = -1.0 / ((b / c) - (a / b));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -3.6e-302: tmp = -(b / a) else: tmp = -1.0 / ((b / c) - (a / b)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -3.6e-302) tmp = Float64(-Float64(b / a)); else tmp = Float64(-1.0 / Float64(Float64(b / c) - Float64(a / b))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -3.6e-302) tmp = -(b / a); else tmp = -1.0 / ((b / c) - (a / b)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -3.6e-302], (-N[(b / a), $MachinePrecision]), N[(-1.0 / N[(N[(b / c), $MachinePrecision] - N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.6 \cdot 10^{-302}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{b}{c} - \frac{a}{b}}\\
\end{array}
\end{array}
if b < -3.6000000000000001e-302Initial program 71.9%
*-commutative71.9%
Simplified71.9%
Taylor expanded in b around -inf 70.0%
associate-*r/70.0%
mul-1-neg70.0%
Simplified70.0%
if -3.6000000000000001e-302 < b Initial program 26.9%
*-commutative26.9%
Simplified26.9%
clear-num26.9%
inv-pow26.9%
Applied egg-rr23.7%
unpow-123.7%
Simplified23.7%
associate-/l*23.7%
frac-2neg23.7%
metadata-eval23.7%
hypot-undefine23.5%
add-sqr-sqrt23.9%
fma-undefine23.9%
add-cube-cbrt23.7%
unpow323.7%
distribute-neg-in23.7%
sub-neg23.7%
add-sqr-sqrt0.8%
Applied egg-rr33.4%
Taylor expanded in a around 0 0.0%
+-commutative0.0%
associate-*r/0.0%
*-commutative0.0%
times-frac0.0%
unpow20.0%
rem-square-sqrt73.0%
metadata-eval73.0%
neg-mul-173.0%
distribute-neg-frac273.0%
Simplified73.0%
Final simplification71.4%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (- (/ b a)) (/ c (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-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 <= (-5d-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 <= -5e-310) {
tmp = -(b / a);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = -(b / a) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) 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 <= -5e-310) tmp = -(b / a); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], (-N[(b / a), $MachinePrecision]), N[(c / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;-\frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 71.6%
*-commutative71.6%
Simplified71.6%
Taylor expanded in b around -inf 69.0%
associate-*r/69.0%
mul-1-neg69.0%
Simplified69.0%
if -4.999999999999985e-310 < b Initial program 26.5%
*-commutative26.5%
Simplified26.5%
Taylor expanded in b around inf 73.8%
associate-*r/73.8%
mul-1-neg73.8%
Simplified73.8%
Final simplification71.2%
(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 50.6%
*-commutative50.6%
Simplified50.6%
Taylor expanded in b around -inf 38.2%
associate-*r/38.2%
mul-1-neg38.2%
Simplified38.2%
Final simplification38.2%
(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 50.6%
*-commutative50.6%
Simplified50.6%
clear-num50.5%
inv-pow50.5%
Applied egg-rr22.2%
unpow-122.2%
Simplified22.2%
Taylor expanded in a around 0 2.3%
Final simplification2.3%
herbie shell --seed 2024080
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))