
(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 5 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
(+
(* -2.0 (/ (* (pow a 2.0) (pow c 3.0)) (pow b 5.0)))
(-
(-
(* -0.25 (* (pow (* a c) 4.0) (/ 20.0 (* a (pow b 7.0)))))
(* (pow (/ c b) 2.0) (/ a b)))
(/ c b))))
double code(double a, double b, double c) {
return (-2.0 * ((pow(a, 2.0) * pow(c, 3.0)) / pow(b, 5.0))) + (((-0.25 * (pow((a * c), 4.0) * (20.0 / (a * pow(b, 7.0))))) - (pow((c / b), 2.0) * (a / b))) - (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 = ((-2.0d0) * (((a ** 2.0d0) * (c ** 3.0d0)) / (b ** 5.0d0))) + ((((-0.25d0) * (((a * c) ** 4.0d0) * (20.0d0 / (a * (b ** 7.0d0))))) - (((c / b) ** 2.0d0) * (a / b))) - (c / b))
end function
public static double code(double a, double b, double c) {
return (-2.0 * ((Math.pow(a, 2.0) * Math.pow(c, 3.0)) / Math.pow(b, 5.0))) + (((-0.25 * (Math.pow((a * c), 4.0) * (20.0 / (a * Math.pow(b, 7.0))))) - (Math.pow((c / b), 2.0) * (a / b))) - (c / b));
}
def code(a, b, c): return (-2.0 * ((math.pow(a, 2.0) * math.pow(c, 3.0)) / math.pow(b, 5.0))) + (((-0.25 * (math.pow((a * c), 4.0) * (20.0 / (a * math.pow(b, 7.0))))) - (math.pow((c / b), 2.0) * (a / b))) - (c / b))
function code(a, b, c) return Float64(Float64(-2.0 * Float64(Float64((a ^ 2.0) * (c ^ 3.0)) / (b ^ 5.0))) + Float64(Float64(Float64(-0.25 * Float64((Float64(a * c) ^ 4.0) * Float64(20.0 / Float64(a * (b ^ 7.0))))) - Float64((Float64(c / b) ^ 2.0) * Float64(a / b))) - Float64(c / b))) end
function tmp = code(a, b, c) tmp = (-2.0 * (((a ^ 2.0) * (c ^ 3.0)) / (b ^ 5.0))) + (((-0.25 * (((a * c) ^ 4.0) * (20.0 / (a * (b ^ 7.0))))) - (((c / b) ^ 2.0) * (a / b))) - (c / b)); end
code[a_, b_, c_] := N[(N[(-2.0 * N[(N[(N[Power[a, 2.0], $MachinePrecision] * N[Power[c, 3.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(-0.25 * N[(N[Power[N[(a * c), $MachinePrecision], 4.0], $MachinePrecision] * N[(20.0 / N[(a * N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[Power[N[(c / b), $MachinePrecision], 2.0], $MachinePrecision] * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \frac{{a}^{2} \cdot {c}^{3}}{{b}^{5}} + \left(\left(-0.25 \cdot \left({\left(a \cdot c\right)}^{4} \cdot \frac{20}{a \cdot {b}^{7}}\right) - {\left(\frac{c}{b}\right)}^{2} \cdot \frac{a}{b}\right) - \frac{c}{b}\right)
\end{array}
Initial program 19.8%
*-commutative19.8%
Simplified19.8%
Taylor expanded in b around inf 98.2%
*-commutative98.2%
unpow-prod-down98.2%
pow-prod-down98.2%
pow-pow98.2%
metadata-eval98.2%
metadata-eval98.2%
Applied egg-rr98.2%
Taylor expanded in c around 0 98.2%
Simplified98.2%
*-commutative98.2%
unpow398.2%
times-frac98.2%
unpow298.2%
frac-times98.2%
pow298.2%
Applied egg-rr98.2%
Final simplification98.2%
(FPCore (a b c) :precision binary64 (- (* -2.0 (/ (* (pow a 2.0) (pow c 3.0)) (pow b 5.0))) (+ (/ c b) (* (pow (/ c b) 2.0) (/ a b)))))
double code(double a, double b, double c) {
return (-2.0 * ((pow(a, 2.0) * pow(c, 3.0)) / pow(b, 5.0))) - ((c / b) + (pow((c / b), 2.0) * (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 = ((-2.0d0) * (((a ** 2.0d0) * (c ** 3.0d0)) / (b ** 5.0d0))) - ((c / b) + (((c / b) ** 2.0d0) * (a / b)))
end function
public static double code(double a, double b, double c) {
return (-2.0 * ((Math.pow(a, 2.0) * Math.pow(c, 3.0)) / Math.pow(b, 5.0))) - ((c / b) + (Math.pow((c / b), 2.0) * (a / b)));
}
def code(a, b, c): return (-2.0 * ((math.pow(a, 2.0) * math.pow(c, 3.0)) / math.pow(b, 5.0))) - ((c / b) + (math.pow((c / b), 2.0) * (a / b)))
function code(a, b, c) return Float64(Float64(-2.0 * Float64(Float64((a ^ 2.0) * (c ^ 3.0)) / (b ^ 5.0))) - Float64(Float64(c / b) + Float64((Float64(c / b) ^ 2.0) * Float64(a / b)))) end
function tmp = code(a, b, c) tmp = (-2.0 * (((a ^ 2.0) * (c ^ 3.0)) / (b ^ 5.0))) - ((c / b) + (((c / b) ^ 2.0) * (a / b))); end
code[a_, b_, c_] := N[(N[(-2.0 * N[(N[(N[Power[a, 2.0], $MachinePrecision] * N[Power[c, 3.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(c / b), $MachinePrecision] + N[(N[Power[N[(c / b), $MachinePrecision], 2.0], $MachinePrecision] * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot \frac{{a}^{2} \cdot {c}^{3}}{{b}^{5}} - \left(\frac{c}{b} + {\left(\frac{c}{b}\right)}^{2} \cdot \frac{a}{b}\right)
\end{array}
Initial program 19.8%
*-commutative19.8%
Simplified19.8%
Taylor expanded in b around inf 97.2%
*-commutative98.2%
unpow398.2%
times-frac98.2%
unpow298.2%
frac-times98.2%
pow298.2%
Applied egg-rr97.2%
Final simplification97.2%
(FPCore (a b c) :precision binary64 (- (/ c (- b)) (* a (/ (pow c 2.0) (pow b 3.0)))))
double code(double a, double b, double c) {
return (c / -b) - (a * (pow(c, 2.0) / pow(b, 3.0)));
}
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) - (a * ((c ** 2.0d0) / (b ** 3.0d0)))
end function
public static double code(double a, double b, double c) {
return (c / -b) - (a * (Math.pow(c, 2.0) / Math.pow(b, 3.0)));
}
def code(a, b, c): return (c / -b) - (a * (math.pow(c, 2.0) / math.pow(b, 3.0)))
function code(a, b, c) return Float64(Float64(c / Float64(-b)) - Float64(a * Float64((c ^ 2.0) / (b ^ 3.0)))) end
function tmp = code(a, b, c) tmp = (c / -b) - (a * ((c ^ 2.0) / (b ^ 3.0))); end
code[a_, b_, c_] := N[(N[(c / (-b)), $MachinePrecision] - N[(a * N[(N[Power[c, 2.0], $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{-b} - a \cdot \frac{{c}^{2}}{{b}^{3}}
\end{array}
Initial program 19.8%
*-commutative19.8%
Simplified19.8%
Taylor expanded in b around inf 95.1%
clear-num95.0%
inv-pow95.0%
distribute-lft-out95.0%
associate-/l*95.2%
fma-define95.2%
div-inv95.2%
pow-prod-down95.2%
pow-flip95.2%
metadata-eval95.2%
Applied egg-rr95.2%
Taylor expanded in a around 0 95.6%
mul-1-neg95.6%
distribute-frac-neg295.6%
+-commutative95.6%
distribute-frac-neg295.6%
unsub-neg95.6%
mul-1-neg95.6%
distribute-frac-neg295.6%
associate-/l*95.6%
Simplified95.6%
Final simplification95.6%
(FPCore (a b c) :precision binary64 (pow (- (/ a b) (/ b c)) -1.0))
double code(double a, double b, double c) {
return pow(((a / b) - (b / c)), -1.0);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((a / b) - (b / c)) ** (-1.0d0)
end function
public static double code(double a, double b, double c) {
return Math.pow(((a / b) - (b / c)), -1.0);
}
def code(a, b, c): return math.pow(((a / b) - (b / c)), -1.0)
function code(a, b, c) return Float64(Float64(a / b) - Float64(b / c)) ^ -1.0 end
function tmp = code(a, b, c) tmp = ((a / b) - (b / c)) ^ -1.0; end
code[a_, b_, c_] := N[Power[N[(N[(a / b), $MachinePrecision] - N[(b / c), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\frac{a}{b} - \frac{b}{c}\right)}^{-1}
\end{array}
Initial program 19.8%
*-commutative19.8%
Simplified19.8%
Taylor expanded in b around inf 95.1%
clear-num95.0%
inv-pow95.0%
distribute-lft-out95.0%
associate-/l*95.2%
fma-define95.2%
div-inv95.2%
pow-prod-down95.2%
pow-flip95.2%
metadata-eval95.2%
Applied egg-rr95.2%
Taylor expanded in a around 0 95.4%
+-commutative95.4%
neg-mul-195.4%
unsub-neg95.4%
Simplified95.4%
Final simplification95.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 / 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 19.8%
*-commutative19.8%
Simplified19.8%
Taylor expanded in b around inf 89.3%
mul-1-neg89.3%
distribute-neg-frac289.3%
Simplified89.3%
Final simplification89.3%
herbie shell --seed 2024041
(FPCore (a b c)
:name "Quadratic roots, wide range"
:precision binary64
:pre (and (and (and (< 4.930380657631324e-32 a) (< a 2.028240960365167e+31)) (and (< 4.930380657631324e-32 b) (< b 2.028240960365167e+31))) (and (< 4.930380657631324e-32 c) (< c 2.028240960365167e+31)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))