
(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 (/ (/ (* (* a 4.0) c) (- (- b) (sqrt (fma b b (* (* a c) -4.0))))) (* a 2.0)))
double code(double a, double b, double c) {
return (((a * 4.0) * c) / (-b - sqrt(fma(b, b, ((a * c) * -4.0))))) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(Float64(a * 4.0) * c) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(Float64(a * c) * -4.0))))) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(N[(a * 4.0), $MachinePrecision] * c), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(N[(a * c), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(a \cdot 4\right) \cdot c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)}}}{a \cdot 2}
\end{array}
Initial program 17.6%
*-commutative17.6%
Simplified17.6%
neg-sub017.6%
flip--17.8%
metadata-eval17.8%
pow217.7%
add-sqr-sqrt18.1%
sqrt-prod17.7%
sqr-neg17.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod17.7%
sqr-neg17.7%
sqrt-prod18.1%
add-sqr-sqrt17.7%
Applied egg-rr17.7%
neg-sub017.7%
Simplified17.7%
flip-+17.7%
pow217.7%
pow217.7%
distribute-frac-neg17.7%
pow217.7%
pow117.7%
pow-div17.6%
metadata-eval17.6%
pow117.6%
add-sqr-sqrt18.0%
pow218.0%
associate-*l*18.0%
Applied egg-rr18.0%
associate--r-99.3%
unpow298.7%
unpow299.3%
difference-of-squares99.3%
+-commutative99.3%
neg-mul-199.3%
distribute-rgt1-in99.3%
metadata-eval99.3%
mul0-lft99.3%
associate-*r*99.3%
unpow299.3%
fma-neg99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
associate-*r*99.3%
*-commutative99.3%
Simplified99.3%
(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 17.6%
*-commutative17.6%
Simplified17.6%
Taylor expanded in a around 0 94.7%
mul-1-neg94.7%
unsub-neg94.7%
mul-1-neg94.7%
distribute-neg-frac294.7%
associate-/l*94.7%
Simplified94.7%
(FPCore (a b c) :precision binary64 (/ (- (* a (- (pow (/ c (- b)) 2.0))) c) b))
double code(double a, double b, double c) {
return ((a * -pow((c / -b), 2.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 * -((c / -b) ** 2.0d0)) - c) / b
end function
public static double code(double a, double b, double c) {
return ((a * -Math.pow((c / -b), 2.0)) - c) / b;
}
def code(a, b, c): return ((a * -math.pow((c / -b), 2.0)) - c) / b
function code(a, b, c) return Float64(Float64(Float64(a * Float64(-(Float64(c / Float64(-b)) ^ 2.0))) - c) / b) end
function tmp = code(a, b, c) tmp = ((a * -((c / -b) ^ 2.0)) - c) / b; end
code[a_, b_, c_] := N[(N[(N[(a * (-N[Power[N[(c / (-b)), $MachinePrecision], 2.0], $MachinePrecision])), $MachinePrecision] - c), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot \left(-{\left(\frac{c}{-b}\right)}^{2}\right) - c}{b}
\end{array}
Initial program 17.6%
*-commutative17.6%
Simplified17.6%
Taylor expanded in c around 0 94.4%
associate-*r/94.4%
neg-mul-194.4%
distribute-lft-neg-in94.4%
Simplified94.4%
Taylor expanded in b around inf 94.7%
Simplified94.7%
(FPCore (a b c) :precision binary64 (/ 1.0 (- (/ a b) (/ b c))))
double code(double a, double b, double c) {
return 1.0 / ((a / b) - (b / c));
}
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 / ((a / b) - (b / c))
end function
public static double code(double a, double b, double c) {
return 1.0 / ((a / b) - (b / c));
}
def code(a, b, c): return 1.0 / ((a / b) - (b / c))
function code(a, b, c) return Float64(1.0 / Float64(Float64(a / b) - Float64(b / c))) end
function tmp = code(a, b, c) tmp = 1.0 / ((a / b) - (b / c)); end
code[a_, b_, c_] := N[(1.0 / N[(N[(a / b), $MachinePrecision] - N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{a}{b} - \frac{b}{c}}
\end{array}
Initial program 17.6%
*-commutative17.6%
Simplified17.6%
neg-sub017.6%
flip--17.8%
metadata-eval17.8%
pow217.7%
add-sqr-sqrt18.1%
sqrt-prod17.7%
sqr-neg17.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod17.7%
sqr-neg17.7%
sqrt-prod18.1%
add-sqr-sqrt17.7%
Applied egg-rr17.7%
neg-sub017.7%
Simplified17.7%
clear-num17.7%
inv-pow17.7%
*-commutative17.7%
pow217.8%
distribute-frac-neg17.8%
pow217.7%
pow117.7%
pow-div17.6%
metadata-eval17.6%
pow117.6%
pow217.6%
associate-*l*17.6%
Applied egg-rr17.6%
unpow-117.6%
*-commutative17.6%
+-commutative17.6%
unsub-neg17.6%
unpow217.6%
fma-neg17.7%
*-commutative17.7%
distribute-rgt-neg-in17.7%
metadata-eval17.7%
Simplified17.7%
Taylor expanded in a around 0 94.6%
mul-1-neg94.6%
Applied egg-rr94.6%
Final simplification94.6%
(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 17.6%
*-commutative17.6%
Simplified17.6%
Taylor expanded in b around inf 90.5%
associate-*r/90.5%
mul-1-neg90.5%
Simplified90.5%
Final simplification90.5%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.0;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 0.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 17.6%
*-commutative17.6%
Simplified17.6%
neg-sub017.6%
flip--17.8%
metadata-eval17.8%
pow217.7%
add-sqr-sqrt18.1%
sqrt-prod17.7%
sqr-neg17.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod17.7%
sqr-neg17.7%
sqrt-prod18.1%
add-sqr-sqrt17.7%
Applied egg-rr17.7%
neg-sub017.7%
Simplified17.7%
Taylor expanded in a around 0 3.3%
associate-*r/3.3%
distribute-rgt1-in3.3%
metadata-eval3.3%
mul0-lft3.3%
metadata-eval3.3%
Simplified3.3%
Taylor expanded in a around 0 3.3%
herbie shell --seed 2024121
(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)))