
(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 -2.15e+130)
(/ (- b) a)
(if (<= b 3.1e-8)
(/ (- (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 <= -2.15e+130) {
tmp = -b / a;
} else if (b <= 3.1e-8) {
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 <= (-2.15d+130)) then
tmp = -b / a
else if (b <= 3.1d-8) 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 <= -2.15e+130) {
tmp = -b / a;
} else if (b <= 3.1e-8) {
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 <= -2.15e+130: tmp = -b / a elif b <= 3.1e-8: 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 <= -2.15e+130) tmp = Float64(Float64(-b) / a); elseif (b <= 3.1e-8) 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 <= -2.15e+130) tmp = -b / a; elseif (b <= 3.1e-8) 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, -2.15e+130], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 3.1e-8], 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 -2.15 \cdot 10^{+130}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 3.1 \cdot 10^{-8}:\\
\;\;\;\;\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 < -2.14999999999999992e130Initial program 53.4%
*-commutative53.4%
Simplified53.4%
Taylor expanded in b around -inf 97.0%
associate-*r/97.0%
mul-1-neg97.0%
Simplified97.0%
if -2.14999999999999992e130 < b < 3.1e-8Initial program 78.8%
if 3.1e-8 < b Initial program 12.6%
*-commutative12.6%
Simplified12.6%
Taylor expanded in b around inf 87.6%
mul-1-neg87.6%
distribute-neg-frac87.6%
Simplified87.6%
Final simplification85.6%
(FPCore (a b c) :precision binary64 (if (<= b -1.95e-91) (- (/ c b) (/ b a)) (if (<= b 3e-8) (* (/ 0.5 a) (+ b (sqrt (* a (* c -4.0))))) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.95e-91) {
tmp = (c / b) - (b / a);
} else if (b <= 3e-8) {
tmp = (0.5 / a) * (b + sqrt((a * (c * -4.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.95d-91)) then
tmp = (c / b) - (b / a)
else if (b <= 3d-8) then
tmp = (0.5d0 / a) * (b + sqrt((a * (c * (-4.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.95e-91) {
tmp = (c / b) - (b / a);
} else if (b <= 3e-8) {
tmp = (0.5 / a) * (b + Math.sqrt((a * (c * -4.0))));
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.95e-91: tmp = (c / b) - (b / a) elif b <= 3e-8: tmp = (0.5 / a) * (b + math.sqrt((a * (c * -4.0)))) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.95e-91) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 3e-8) tmp = Float64(Float64(0.5 / a) * Float64(b + sqrt(Float64(a * Float64(c * -4.0))))); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.95e-91) tmp = (c / b) - (b / a); elseif (b <= 3e-8) tmp = (0.5 / a) * (b + sqrt((a * (c * -4.0)))); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.95e-91], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 3e-8], N[(N[(0.5 / a), $MachinePrecision] * N[(b + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.95 \cdot 10^{-91}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 3 \cdot 10^{-8}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \left(c \cdot -4\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.94999999999999997e-91Initial program 67.7%
*-commutative67.7%
Simplified67.7%
Taylor expanded in b around -inf 90.2%
+-commutative90.2%
mul-1-neg90.2%
unsub-neg90.2%
Simplified90.2%
if -1.94999999999999997e-91 < b < 2.99999999999999973e-8Initial program 73.8%
*-commutative73.8%
Simplified73.8%
Applied egg-rr70.4%
Taylor expanded in a around inf 70.4%
*-commutative70.4%
associate-*r*70.4%
Simplified70.4%
if 2.99999999999999973e-8 < b Initial program 12.6%
*-commutative12.6%
Simplified12.6%
Taylor expanded in b around inf 87.6%
mul-1-neg87.6%
distribute-neg-frac87.6%
Simplified87.6%
Final simplification83.0%
(FPCore (a b c) :precision binary64 (if (<= b -2.2e-92) (- (/ c b) (/ b a)) (if (<= b 9.4e-8) (/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -2.2e-92) {
tmp = (c / b) - (b / a);
} else if (b <= 9.4e-8) {
tmp = (sqrt((a * (c * -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 <= (-2.2d-92)) then
tmp = (c / b) - (b / a)
else if (b <= 9.4d-8) then
tmp = (sqrt((a * (c * (-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 <= -2.2e-92) {
tmp = (c / b) - (b / a);
} else if (b <= 9.4e-8) {
tmp = (Math.sqrt((a * (c * -4.0))) - b) / (a * 2.0);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -2.2e-92: tmp = (c / b) - (b / a) elif b <= 9.4e-8: tmp = (math.sqrt((a * (c * -4.0))) - b) / (a * 2.0) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -2.2e-92) tmp = Float64(Float64(c / b) - Float64(b / a)); elseif (b <= 9.4e-8) tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - 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 <= -2.2e-92) tmp = (c / b) - (b / a); elseif (b <= 9.4e-8) tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -2.2e-92], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 9.4e-8], N[(N[(N[Sqrt[N[(a * N[(c * -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 -2.2 \cdot 10^{-92}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{elif}\;b \leq 9.4 \cdot 10^{-8}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -2.19999999999999987e-92Initial program 67.7%
*-commutative67.7%
Simplified67.7%
Taylor expanded in b around -inf 90.2%
+-commutative90.2%
mul-1-neg90.2%
unsub-neg90.2%
Simplified90.2%
if -2.19999999999999987e-92 < b < 9.3999999999999995e-8Initial program 73.8%
*-commutative73.8%
Simplified73.8%
Taylor expanded in b around 0 71.2%
*-commutative71.2%
associate-*r*71.2%
Simplified71.2%
if 9.3999999999999995e-8 < b Initial program 12.6%
*-commutative12.6%
Simplified12.6%
Taylor expanded in b around inf 87.6%
mul-1-neg87.6%
distribute-neg-frac87.6%
Simplified87.6%
Final simplification83.3%
(FPCore (a b c) :precision binary64 (if (<= b -4e-310) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= -4e-310) {
tmp = (c / b) - (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 <= (-4d-310)) then
tmp = (c / b) - (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 <= -4e-310) {
tmp = (c / b) - (b / a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -4e-310: tmp = (c / b) - (b / a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -4e-310) tmp = Float64(Float64(c / b) - 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 <= -4e-310) tmp = (c / b) - (b / a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -4e-310], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -3.999999999999988e-310Initial program 72.2%
*-commutative72.2%
Simplified72.2%
Taylor expanded in b around -inf 72.2%
+-commutative72.2%
mul-1-neg72.2%
unsub-neg72.2%
Simplified72.2%
if -3.999999999999988e-310 < b Initial program 36.3%
*-commutative36.3%
Simplified36.3%
Taylor expanded in b around inf 60.1%
mul-1-neg60.1%
distribute-neg-frac60.1%
Simplified60.1%
Final simplification66.6%
(FPCore (a b c) :precision binary64 (if (<= b 6.2e+68) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2e+68) {
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 <= 6.2d+68) 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 <= 6.2e+68) {
tmp = -b / a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 6.2e+68: tmp = -b / a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 6.2e+68) 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 <= 6.2e+68) tmp = -b / a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 6.2e+68], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2 \cdot 10^{+68}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 6.1999999999999997e68Initial program 64.8%
*-commutative64.8%
Simplified64.8%
Taylor expanded in b around -inf 48.4%
associate-*r/48.4%
mul-1-neg48.4%
Simplified48.4%
if 6.1999999999999997e68 < b Initial program 14.1%
*-commutative14.1%
Simplified14.1%
Applied egg-rr4.3%
Taylor expanded in b around -inf 38.6%
Final simplification46.6%
(FPCore (a b c) :precision binary64 (if (<= b 8.2e-306) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 8.2e-306) {
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 <= 8.2d-306) 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 <= 8.2e-306) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 8.2e-306: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 8.2e-306) 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 <= 8.2e-306) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 8.2e-306], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 8.2 \cdot 10^{-306}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 8.19999999999999969e-306Initial program 72.2%
*-commutative72.2%
Simplified72.2%
Taylor expanded in b around -inf 72.2%
associate-*r/72.2%
mul-1-neg72.2%
Simplified72.2%
if 8.19999999999999969e-306 < b Initial program 36.3%
*-commutative36.3%
Simplified36.3%
Taylor expanded in b around inf 60.1%
mul-1-neg60.1%
distribute-neg-frac60.1%
Simplified60.1%
Final simplification66.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(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.5%
*-commutative55.5%
Simplified55.5%
Applied egg-rr35.4%
Taylor expanded in a around 0 2.5%
Final simplification2.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.5%
*-commutative55.5%
Simplified55.5%
Applied egg-rr35.4%
Taylor expanded in b around -inf 9.3%
Final simplification9.3%
herbie shell --seed 2023331
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))