
(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
(/
(fma
-2.0
(/ (* (pow c 3.0) (pow a 2.0)) (pow b 4.0))
(-
(-
(* -0.25 (* (pow (* c a) 4.0) (/ 20.0 (* a (pow b 6.0)))))
(* a (* (/ c b) (/ c b))))
c))
b))
double code(double a, double b, double c) {
return fma(-2.0, ((pow(c, 3.0) * pow(a, 2.0)) / pow(b, 4.0)), (((-0.25 * (pow((c * a), 4.0) * (20.0 / (a * pow(b, 6.0))))) - (a * ((c / b) * (c / b)))) - c)) / b;
}
function code(a, b, c) return Float64(fma(-2.0, Float64(Float64((c ^ 3.0) * (a ^ 2.0)) / (b ^ 4.0)), Float64(Float64(Float64(-0.25 * Float64((Float64(c * a) ^ 4.0) * Float64(20.0 / Float64(a * (b ^ 6.0))))) - Float64(a * Float64(Float64(c / b) * Float64(c / b)))) - c)) / b) end
code[a_, b_, c_] := N[(N[(-2.0 * N[(N[(N[Power[c, 3.0], $MachinePrecision] * N[Power[a, 2.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(-0.25 * N[(N[Power[N[(c * a), $MachinePrecision], 4.0], $MachinePrecision] * N[(20.0 / N[(a * N[Power[b, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(a * N[(N[(c / b), $MachinePrecision] * N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - c), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-2, \frac{{c}^{3} \cdot {a}^{2}}{{b}^{4}}, \left(-0.25 \cdot \left({\left(c \cdot a\right)}^{4} \cdot \frac{20}{a \cdot {b}^{6}}\right) - a \cdot \left(\frac{c}{b} \cdot \frac{c}{b}\right)\right) - c\right)}{b}
\end{array}
Initial program 31.6%
*-commutative31.6%
Simplified31.6%
add-cbrt-cube31.4%
pow1/332.8%
pow332.8%
pow232.8%
pow-pow32.7%
metadata-eval32.7%
Applied egg-rr32.7%
Taylor expanded in b around inf 95.1%
Simplified95.1%
pow195.1%
frac-times95.1%
pow-prod-down95.1%
Applied egg-rr95.1%
unpow195.1%
associate-/l*95.1%
*-commutative95.1%
*-commutative95.1%
Simplified95.1%
unpow295.1%
distribute-frac-neg95.1%
distribute-frac-neg95.1%
Applied egg-rr95.1%
Final simplification95.1%
(FPCore (a b c)
:precision binary64
(-
(*
a
(-
(*
a
(+
(* -2.0 (/ (pow c 3.0) (pow b 5.0)))
(* -5.0 (/ (* a (pow c 4.0)) (pow b 7.0)))))
(/ (pow c 2.0) (pow b 3.0))))
(/ c b)))
double code(double a, double b, double c) {
return (a * ((a * ((-2.0 * (pow(c, 3.0) / pow(b, 5.0))) + (-5.0 * ((a * pow(c, 4.0)) / pow(b, 7.0))))) - (pow(c, 2.0) / pow(b, 3.0)))) - (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 = (a * ((a * (((-2.0d0) * ((c ** 3.0d0) / (b ** 5.0d0))) + ((-5.0d0) * ((a * (c ** 4.0d0)) / (b ** 7.0d0))))) - ((c ** 2.0d0) / (b ** 3.0d0)))) - (c / b)
end function
public static double code(double a, double b, double c) {
return (a * ((a * ((-2.0 * (Math.pow(c, 3.0) / Math.pow(b, 5.0))) + (-5.0 * ((a * Math.pow(c, 4.0)) / Math.pow(b, 7.0))))) - (Math.pow(c, 2.0) / Math.pow(b, 3.0)))) - (c / b);
}
def code(a, b, c): return (a * ((a * ((-2.0 * (math.pow(c, 3.0) / math.pow(b, 5.0))) + (-5.0 * ((a * math.pow(c, 4.0)) / math.pow(b, 7.0))))) - (math.pow(c, 2.0) / math.pow(b, 3.0)))) - (c / b)
function code(a, b, c) return Float64(Float64(a * Float64(Float64(a * Float64(Float64(-2.0 * Float64((c ^ 3.0) / (b ^ 5.0))) + Float64(-5.0 * Float64(Float64(a * (c ^ 4.0)) / (b ^ 7.0))))) - Float64((c ^ 2.0) / (b ^ 3.0)))) - Float64(c / b)) end
function tmp = code(a, b, c) tmp = (a * ((a * ((-2.0 * ((c ^ 3.0) / (b ^ 5.0))) + (-5.0 * ((a * (c ^ 4.0)) / (b ^ 7.0))))) - ((c ^ 2.0) / (b ^ 3.0)))) - (c / b); end
code[a_, b_, c_] := N[(N[(a * N[(N[(a * N[(N[(-2.0 * N[(N[Power[c, 3.0], $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-5.0 * N[(N[(a * N[Power[c, 4.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[Power[c, 2.0], $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(c / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
a \cdot \left(a \cdot \left(-2 \cdot \frac{{c}^{3}}{{b}^{5}} + -5 \cdot \frac{a \cdot {c}^{4}}{{b}^{7}}\right) - \frac{{c}^{2}}{{b}^{3}}\right) - \frac{c}{b}
\end{array}
Initial program 31.6%
*-commutative31.6%
+-commutative31.6%
sqr-neg31.6%
unsub-neg31.6%
sqr-neg31.6%
fma-neg31.7%
distribute-lft-neg-in31.7%
*-commutative31.7%
*-commutative31.7%
distribute-rgt-neg-in31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in a around 0 95.1%
Taylor expanded in c around 0 95.1%
Final simplification95.1%
(FPCore (a b c) :precision binary64 (let* ((t_0 (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0)))) (if (<= t_0 -0.45) t_0 (/ (- (* a (- (pow (/ c (- b)) 2.0))) c) b))))
double code(double a, double b, double c) {
double t_0 = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
double tmp;
if (t_0 <= -0.45) {
tmp = t_0;
} else {
tmp = ((a * -pow((c / -b), 2.0)) - 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) :: t_0
real(8) :: tmp
t_0 = (sqrt(((b * b) - (c * (a * 4.0d0)))) - b) / (a * 2.0d0)
if (t_0 <= (-0.45d0)) then
tmp = t_0
else
tmp = ((a * -((c / -b) ** 2.0d0)) - c) / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = (Math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
double tmp;
if (t_0 <= -0.45) {
tmp = t_0;
} else {
tmp = ((a * -Math.pow((c / -b), 2.0)) - c) / b;
}
return tmp;
}
def code(a, b, c): t_0 = (math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0) tmp = 0 if t_0 <= -0.45: tmp = t_0 else: tmp = ((a * -math.pow((c / -b), 2.0)) - c) / b return tmp
function code(a, b, c) t_0 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(a * 2.0)) tmp = 0.0 if (t_0 <= -0.45) tmp = t_0; else tmp = Float64(Float64(Float64(a * Float64(-(Float64(c / Float64(-b)) ^ 2.0))) - c) / b); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0); tmp = 0.0; if (t_0 <= -0.45) tmp = t_0; else tmp = ((a * -((c / -b) ^ 2.0)) - c) / b; end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.45], t$95$0, N[(N[(N[(a * (-N[Power[N[(c / (-b)), $MachinePrecision], 2.0], $MachinePrecision])), $MachinePrecision] - c), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}\\
\mathbf{if}\;t\_0 \leq -0.45:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{a \cdot \left(-{\left(\frac{c}{-b}\right)}^{2}\right) - c}{b}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 4 binary64) a) c)))) (*.f64 #s(literal 2 binary64) a)) < -0.450000000000000011Initial program 78.6%
if -0.450000000000000011 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 4 binary64) a) c)))) (*.f64 #s(literal 2 binary64) a)) Initial program 24.6%
*-commutative24.6%
+-commutative24.6%
sqr-neg24.6%
unsub-neg24.6%
sqr-neg24.6%
fma-neg24.8%
distribute-lft-neg-in24.8%
*-commutative24.8%
*-commutative24.8%
distribute-rgt-neg-in24.8%
metadata-eval24.8%
Simplified24.8%
Taylor expanded in a around 0 93.6%
mul-1-neg93.6%
unsub-neg93.6%
mul-1-neg93.6%
distribute-neg-frac293.6%
associate-/l*93.6%
Simplified93.6%
Taylor expanded in b around inf 93.6%
Simplified93.6%
Final simplification91.7%
(FPCore (a b c) :precision binary64 (/ 1.0 (* 2.0 (fma a (+ (* a (/ (* c 0.5) (pow b 3.0))) (/ 0.5 b)) (* (/ b c) -0.5)))))
double code(double a, double b, double c) {
return 1.0 / (2.0 * fma(a, ((a * ((c * 0.5) / pow(b, 3.0))) + (0.5 / b)), ((b / c) * -0.5)));
}
function code(a, b, c) return Float64(1.0 / Float64(2.0 * fma(a, Float64(Float64(a * Float64(Float64(c * 0.5) / (b ^ 3.0))) + Float64(0.5 / b)), Float64(Float64(b / c) * -0.5)))) end
code[a_, b_, c_] := N[(1.0 / N[(2.0 * N[(a * N[(N[(a * N[(N[(c * 0.5), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(0.5 / b), $MachinePrecision]), $MachinePrecision] + N[(N[(b / c), $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{2 \cdot \mathsf{fma}\left(a, a \cdot \frac{c \cdot 0.5}{{b}^{3}} + \frac{0.5}{b}, \frac{b}{c} \cdot -0.5\right)}
\end{array}
Initial program 31.6%
*-commutative31.6%
+-commutative31.6%
sqr-neg31.6%
unsub-neg31.6%
sqr-neg31.6%
fma-neg31.7%
distribute-lft-neg-in31.7%
*-commutative31.7%
*-commutative31.7%
distribute-rgt-neg-in31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in b around inf 93.0%
fma-define93.0%
cube-prod93.0%
distribute-lft-out93.0%
*-commutative93.0%
Simplified93.0%
clear-num92.9%
inv-pow92.9%
Applied egg-rr92.9%
unpow-192.9%
*-commutative92.9%
*-lft-identity92.9%
times-frac92.9%
metadata-eval92.9%
Simplified92.9%
Taylor expanded in a around 0 93.5%
+-commutative93.5%
fma-define93.5%
mul-1-neg93.5%
distribute-rgt-neg-in93.5%
distribute-rgt-out93.5%
metadata-eval93.5%
distribute-rgt-neg-in93.5%
metadata-eval93.5%
*-commutative93.5%
associate-*r/93.5%
associate-*r/93.5%
metadata-eval93.5%
*-commutative93.5%
Simplified93.5%
Final simplification93.5%
(FPCore (a b c) :precision binary64 (/ -1.0 (- (/ b c) (/ a b))))
double code(double a, double b, double c) {
return -1.0 / ((b / c) - (a / b));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-1.0d0) / ((b / c) - (a / b))
end function
public static double code(double a, double b, double c) {
return -1.0 / ((b / c) - (a / b));
}
def code(a, b, c): return -1.0 / ((b / c) - (a / b))
function code(a, b, c) return Float64(-1.0 / Float64(Float64(b / c) - Float64(a / b))) end
function tmp = code(a, b, c) tmp = -1.0 / ((b / c) - (a / b)); end
code[a_, b_, c_] := N[(-1.0 / N[(N[(b / c), $MachinePrecision] - N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{\frac{b}{c} - \frac{a}{b}}
\end{array}
Initial program 31.6%
*-commutative31.6%
+-commutative31.6%
sqr-neg31.6%
unsub-neg31.6%
sqr-neg31.6%
fma-neg31.7%
distribute-lft-neg-in31.7%
*-commutative31.7%
*-commutative31.7%
distribute-rgt-neg-in31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in b around inf 93.0%
fma-define93.0%
cube-prod93.0%
distribute-lft-out93.0%
*-commutative93.0%
Simplified93.0%
clear-num92.9%
inv-pow92.9%
Applied egg-rr92.9%
unpow-192.9%
*-commutative92.9%
*-lft-identity92.9%
times-frac92.9%
metadata-eval92.9%
Simplified92.9%
Taylor expanded in a around 0 90.0%
+-commutative90.0%
mul-1-neg90.0%
unsub-neg90.0%
Simplified90.0%
Final simplification90.0%
(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 / Float64(-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 31.6%
*-commutative31.6%
+-commutative31.6%
sqr-neg31.6%
unsub-neg31.6%
sqr-neg31.6%
fma-neg31.7%
distribute-lft-neg-in31.7%
*-commutative31.7%
*-commutative31.7%
distribute-rgt-neg-in31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in b around inf 80.6%
associate-*r/80.6%
mul-1-neg80.6%
Simplified80.6%
Final simplification80.6%
herbie shell --seed 2024141
(FPCore (a b c)
:name "Quadratic roots, medium range"
:precision binary64
:pre (and (and (and (< 1.1102230246251565e-16 a) (< a 9007199254740992.0)) (and (< 1.1102230246251565e-16 b) (< b 9007199254740992.0))) (and (< 1.1102230246251565e-16 c) (< c 9007199254740992.0)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))