
(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 -5e+150)
(/ b (- a))
(if (<= b 3.7e-25)
(/ (- (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 <= -5e+150) {
tmp = b / -a;
} else if (b <= 3.7e-25) {
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 <= (-5d+150)) then
tmp = b / -a
else if (b <= 3.7d-25) 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 <= -5e+150) {
tmp = b / -a;
} else if (b <= 3.7e-25) {
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 <= -5e+150: tmp = b / -a elif b <= 3.7e-25: 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 <= -5e+150) tmp = Float64(b / Float64(-a)); elseif (b <= 3.7e-25) 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 <= -5e+150) tmp = b / -a; elseif (b <= 3.7e-25) 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, -5e+150], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 3.7e-25], 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 -5 \cdot 10^{+150}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 3.7 \cdot 10^{-25}:\\
\;\;\;\;\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 < -5.00000000000000009e150Initial program 44.5%
*-commutative44.5%
Simplified44.5%
Taylor expanded in b around -inf 97.8%
associate-*r/97.8%
mul-1-neg97.8%
Simplified97.8%
if -5.00000000000000009e150 < b < 3.70000000000000009e-25Initial program 77.5%
if 3.70000000000000009e-25 < b Initial program 13.1%
*-commutative13.1%
Simplified13.1%
Taylor expanded in b around inf 95.0%
associate-*r/95.0%
mul-1-neg95.0%
Simplified95.0%
Final simplification86.6%
(FPCore (a b c)
:precision binary64
(if (<= b -1.25e+148)
(/ b (- a))
(if (<= b 4.2e-22)
(* (/ 0.5 a) (- (sqrt (+ (* b b) (* a (* c -4.0)))) b))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.25e+148) {
tmp = b / -a;
} else if (b <= 4.2e-22) {
tmp = (0.5 / a) * (sqrt(((b * b) + (a * (c * -4.0)))) - b);
} 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.25d+148)) then
tmp = b / -a
else if (b <= 4.2d-22) then
tmp = (0.5d0 / a) * (sqrt(((b * b) + (a * (c * (-4.0d0))))) - b)
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.25e+148) {
tmp = b / -a;
} else if (b <= 4.2e-22) {
tmp = (0.5 / a) * (Math.sqrt(((b * b) + (a * (c * -4.0)))) - b);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.25e+148: tmp = b / -a elif b <= 4.2e-22: tmp = (0.5 / a) * (math.sqrt(((b * b) + (a * (c * -4.0)))) - b) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.25e+148) tmp = Float64(b / Float64(-a)); elseif (b <= 4.2e-22) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(Float64(b * b) + Float64(a * Float64(c * -4.0)))) - b)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.25e+148) tmp = b / -a; elseif (b <= 4.2e-22) tmp = (0.5 / a) * (sqrt(((b * b) + (a * (c * -4.0)))) - b); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.25e+148], N[(b / (-a)), $MachinePrecision], If[LessEqual[b, 4.2e-22], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.25 \cdot 10^{+148}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{elif}\;b \leq 4.2 \cdot 10^{-22}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{b \cdot b + a \cdot \left(c \cdot -4\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.25000000000000006e148Initial program 44.5%
*-commutative44.5%
Simplified44.5%
Taylor expanded in b around -inf 97.8%
associate-*r/97.8%
mul-1-neg97.8%
Simplified97.8%
if -1.25000000000000006e148 < b < 4.20000000000000016e-22Initial program 77.5%
*-commutative77.5%
Simplified77.5%
Applied egg-rr77.4%
sub-neg77.4%
distribute-rgt-out--77.4%
Simplified77.4%
fma-undefine77.4%
Applied egg-rr77.4%
unpow277.4%
Applied egg-rr77.4%
if 4.20000000000000016e-22 < b Initial program 13.1%
*-commutative13.1%
Simplified13.1%
Taylor expanded in b around inf 95.0%
associate-*r/95.0%
mul-1-neg95.0%
Simplified95.0%
Final simplification86.6%
(FPCore (a b c)
:precision binary64
(if (<= b -1.35e-31)
(* b (+ (/ c (pow b 2.0)) (/ -1.0 a)))
(if (<= b 2.4e-25)
(* (/ 0.5 a) (- (sqrt (* a (* c -4.0))) b))
(/ c (- b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.35e-31) {
tmp = b * ((c / pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2.4e-25) {
tmp = (0.5 / a) * (sqrt((a * (c * -4.0))) - b);
} 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.35d-31)) then
tmp = b * ((c / (b ** 2.0d0)) + ((-1.0d0) / a))
else if (b <= 2.4d-25) then
tmp = (0.5d0 / a) * (sqrt((a * (c * (-4.0d0)))) - b)
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.35e-31) {
tmp = b * ((c / Math.pow(b, 2.0)) + (-1.0 / a));
} else if (b <= 2.4e-25) {
tmp = (0.5 / a) * (Math.sqrt((a * (c * -4.0))) - b);
} else {
tmp = c / -b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.35e-31: tmp = b * ((c / math.pow(b, 2.0)) + (-1.0 / a)) elif b <= 2.4e-25: tmp = (0.5 / a) * (math.sqrt((a * (c * -4.0))) - b) else: tmp = c / -b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.35e-31) tmp = Float64(b * Float64(Float64(c / (b ^ 2.0)) + Float64(-1.0 / a))); elseif (b <= 2.4e-25) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(a * Float64(c * -4.0))) - b)); else tmp = Float64(c / Float64(-b)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.35e-31) tmp = b * ((c / (b ^ 2.0)) + (-1.0 / a)); elseif (b <= 2.4e-25) tmp = (0.5 / a) * (sqrt((a * (c * -4.0))) - b); else tmp = c / -b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.35e-31], N[(b * N[(N[(c / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(-1.0 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 2.4e-25], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision], N[(c / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.35 \cdot 10^{-31}:\\
\;\;\;\;b \cdot \left(\frac{c}{{b}^{2}} + \frac{-1}{a}\right)\\
\mathbf{elif}\;b \leq 2.4 \cdot 10^{-25}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{a \cdot \left(c \cdot -4\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{-b}\\
\end{array}
\end{array}
if b < -1.35000000000000007e-31Initial program 68.9%
*-commutative68.9%
Simplified68.9%
Taylor expanded in b around -inf 94.1%
mul-1-neg94.1%
distribute-rgt-neg-in94.1%
+-commutative94.1%
mul-1-neg94.1%
unsub-neg94.1%
Simplified94.1%
if -1.35000000000000007e-31 < b < 2.40000000000000009e-25Initial program 69.5%
*-commutative69.5%
Simplified69.5%
Applied egg-rr69.5%
sub-neg69.5%
distribute-rgt-out--69.5%
Simplified69.5%
Taylor expanded in a around inf 61.3%
*-commutative61.3%
associate-*r*61.3%
Simplified61.3%
if 2.40000000000000009e-25 < b Initial program 13.1%
*-commutative13.1%
Simplified13.1%
Taylor expanded in b around inf 95.0%
associate-*r/95.0%
mul-1-neg95.0%
Simplified95.0%
Final simplification83.0%
(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(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 <= -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 73.2%
*-commutative73.2%
Simplified73.2%
Taylor expanded in b around -inf 69.6%
associate-*r/69.6%
mul-1-neg69.6%
Simplified69.6%
if -4.999999999999985e-310 < b Initial program 28.5%
*-commutative28.5%
Simplified28.5%
Taylor expanded in b around inf 71.4%
associate-*r/71.4%
mul-1-neg71.4%
Simplified71.4%
Final simplification70.5%
(FPCore (a b c) :precision binary64 (if (<= b 9.5e-5) (/ b (- a)) (/ c b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 9.5e-5) {
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 <= 9.5d-5) 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 <= 9.5e-5) {
tmp = b / -a;
} else {
tmp = c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 9.5e-5: tmp = b / -a else: tmp = c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 9.5e-5) 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 <= 9.5e-5) tmp = b / -a; else tmp = c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 9.5e-5], N[(b / (-a)), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 9.5 \cdot 10^{-5}:\\
\;\;\;\;\frac{b}{-a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\
\end{array}
\end{array}
if b < 9.5000000000000005e-5Initial program 67.9%
*-commutative67.9%
Simplified67.9%
Taylor expanded in b around -inf 51.2%
associate-*r/51.2%
mul-1-neg51.2%
Simplified51.2%
if 9.5000000000000005e-5 < b Initial program 12.5%
*-commutative12.5%
Simplified12.5%
Applied egg-rr2.4%
Taylor expanded in b around -inf 34.6%
Final simplification46.1%
(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 50.8%
*-commutative50.8%
Simplified50.8%
Applied egg-rr30.0%
Taylor expanded in b around -inf 12.9%
(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.8%
*-commutative50.8%
Simplified50.8%
Applied egg-rr30.0%
Taylor expanded in a around 0 2.4%
herbie shell --seed 2024113
(FPCore (a b c)
:name "Quadratic roots, full range"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))