
(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 -3.9e-69)
(/ (- c) b)
(if (<= b 1e+75)
(* -0.5 (/ (+ b (sqrt (- (* b b) (* 4.0 (* c a))))) a))
(- (/ c b) (/ b a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.9e-69) {
tmp = -c / b;
} else if (b <= 1e+75) {
tmp = -0.5 * ((b + sqrt(((b * b) - (4.0 * (c * a))))) / 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 <= (-3.9d-69)) then
tmp = -c / b
else if (b <= 1d+75) then
tmp = (-0.5d0) * ((b + sqrt(((b * b) - (4.0d0 * (c * a))))) / 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 <= -3.9e-69) {
tmp = -c / b;
} else if (b <= 1e+75) {
tmp = -0.5 * ((b + Math.sqrt(((b * b) - (4.0 * (c * a))))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -3.9e-69: tmp = -c / b elif b <= 1e+75: tmp = -0.5 * ((b + math.sqrt(((b * b) - (4.0 * (c * a))))) / a) else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -3.9e-69) tmp = Float64(Float64(-c) / b); elseif (b <= 1e+75) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(c * a))))) / 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 <= -3.9e-69) tmp = -c / b; elseif (b <= 1e+75) tmp = -0.5 * ((b + sqrt(((b * b) - (4.0 * (c * a))))) / a); else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -3.9e-69], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1e+75], N[(-0.5 * N[(N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(c * a), $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 -3.9 \cdot 10^{-69}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 10^{+75}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -3.89999999999999981e-69Initial program 11.2%
Taylor expanded in b around -inf 89.9%
associate-*r/89.9%
neg-mul-189.9%
Simplified89.9%
if -3.89999999999999981e-69 < b < 9.99999999999999927e74Initial program 78.1%
sub-neg78.1%
distribute-neg-out78.1%
neg-mul-178.1%
times-frac79.0%
metadata-eval79.0%
remove-double-neg79.0%
neg-sub079.0%
associate-+l-79.0%
Simplified79.0%
fma-udef79.0%
*-commutative79.0%
metadata-eval79.0%
cancel-sign-sub-inv79.0%
prod-diff78.7%
*-commutative78.7%
fma-neg78.7%
associate-+l-78.7%
add-sqr-sqrt19.5%
sqrt-unprod23.5%
*-commutative23.5%
*-commutative23.5%
swap-sqr23.5%
metadata-eval23.5%
metadata-eval23.5%
swap-sqr23.5%
sqrt-unprod14.5%
add-sqr-sqrt23.4%
associate-*l*23.4%
Applied egg-rr78.7%
associate--r+78.7%
+-inverses79.0%
neg-sub079.0%
associate-*r*79.0%
*-commutative79.0%
distribute-lft-neg-in79.0%
metadata-eval79.0%
*-commutative79.0%
Simplified79.0%
if 9.99999999999999927e74 < b Initial program 56.9%
Taylor expanded in b around inf 96.3%
mul-1-neg96.3%
unsub-neg96.3%
Simplified96.3%
Final simplification87.4%
(FPCore (a b c)
:precision binary64
(if (<= b -2.5e-68)
(/ (- c) b)
(if (<= b 8.5e-70)
(* -0.5 (/ (+ b (sqrt (* c (* a -4.0)))) a))
(/ (- b) a))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.5e-68) {
tmp = -c / b;
} else if (b <= 8.5e-70) {
tmp = -0.5 * ((b + sqrt((c * (a * -4.0)))) / a);
} 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 <= (-2.5d-68)) then
tmp = -c / b
else if (b <= 8.5d-70) then
tmp = (-0.5d0) * ((b + sqrt((c * (a * (-4.0d0))))) / a)
else
tmp = -b / a
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -2.5e-68) {
tmp = -c / b;
} else if (b <= 8.5e-70) {
tmp = -0.5 * ((b + Math.sqrt((c * (a * -4.0)))) / a);
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2.5e-68: tmp = -c / b elif b <= 8.5e-70: tmp = -0.5 * ((b + math.sqrt((c * (a * -4.0)))) / a) else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2.5e-68) tmp = Float64(Float64(-c) / b); elseif (b <= 8.5e-70) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(c * Float64(a * -4.0)))) / a)); else tmp = Float64(Float64(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -2.5e-68) tmp = -c / b; elseif (b <= 8.5e-70) tmp = -0.5 * ((b + sqrt((c * (a * -4.0)))) / a); else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2.5e-68], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 8.5e-70], N[(-0.5 * N[(N[(b + N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[((-b) / a), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.5 \cdot 10^{-68}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 8.5 \cdot 10^{-70}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{c \cdot \left(a \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -2.49999999999999986e-68Initial program 11.2%
Taylor expanded in b around -inf 89.9%
associate-*r/89.9%
neg-mul-189.9%
Simplified89.9%
if -2.49999999999999986e-68 < b < 8.5000000000000002e-70Initial program 74.3%
sub-neg74.3%
distribute-neg-out74.3%
neg-mul-174.3%
times-frac74.3%
metadata-eval74.3%
remove-double-neg74.3%
neg-sub074.3%
associate-+l-74.3%
Simplified74.3%
Taylor expanded in b around 0 69.5%
*-commutative69.5%
*-commutative69.5%
*-commutative69.5%
associate-*l*69.5%
Simplified69.5%
if 8.5000000000000002e-70 < b Initial program 65.5%
Taylor expanded in b around inf 87.4%
associate-*r/87.4%
mul-1-neg87.4%
Simplified87.4%
Final simplification83.2%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (/ (- c) b) (- (/ c b) (/ b a))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-310) {
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-310)) 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-310) {
tmp = -c / b;
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = -c / b else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) 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-310) tmp = -c / b; else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], 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^{-310}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 27.3%
Taylor expanded in b around -inf 71.4%
associate-*r/71.4%
neg-mul-171.4%
Simplified71.4%
if -4.999999999999985e-310 < b Initial program 70.3%
Taylor expanded in b around inf 69.7%
mul-1-neg69.7%
unsub-neg69.7%
Simplified69.7%
Final simplification70.5%
(FPCore (a b c) :precision binary64 (if (<= b -1750000000000.0) (/ c b) (/ (- b) a)))
double code(double a, double b, double c) {
double tmp;
if (b <= -1750000000000.0) {
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 <= (-1750000000000.0d0)) 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 <= -1750000000000.0) {
tmp = c / b;
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1750000000000.0: tmp = c / b else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1750000000000.0) tmp = 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 <= -1750000000000.0) tmp = c / b; else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1750000000000.0], N[(c / b), $MachinePrecision], N[((-b) / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1750000000000:\\
\;\;\;\;\frac{c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -1.75e12Initial program 8.2%
Taylor expanded in b around inf 2.4%
Taylor expanded in c around inf 32.7%
if -1.75e12 < b Initial program 66.8%
Taylor expanded in b around inf 53.3%
associate-*r/53.3%
mul-1-neg53.3%
Simplified53.3%
Final simplification47.8%
(FPCore (a b c) :precision binary64 (if (<= b -2.8e-266) (/ (- c) b) (/ (- b) a)))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.8e-266) {
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 <= (-2.8d-266)) 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 <= -2.8e-266) {
tmp = -c / b;
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2.8e-266: tmp = -c / b else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2.8e-266) 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 <= -2.8e-266) tmp = -c / b; else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2.8e-266], N[((-c) / b), $MachinePrecision], N[((-b) / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.8 \cdot 10^{-266}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -2.8e-266Initial program 25.6%
Taylor expanded in b around -inf 73.8%
associate-*r/73.8%
neg-mul-173.8%
Simplified73.8%
if -2.8e-266 < b Initial program 70.4%
Taylor expanded in b around inf 67.8%
associate-*r/67.8%
mul-1-neg67.8%
Simplified67.8%
Final simplification70.4%
(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 51.0%
Taylor expanded in b around inf 39.0%
Taylor expanded in c around inf 11.0%
Final simplification11.0%
(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 2023275
(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)))