
(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 (* (/ (* c (* a -4.0)) (+ b (sqrt (fma b b (* -4.0 (* c a)))))) (/ 0.5 a)))
double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + sqrt(fma(b, b, (-4.0 * (c * a)))))) * (0.5 / a);
}
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * -4.0)) / Float64(b + sqrt(fma(b, b, Float64(-4.0 * Float64(c * a)))))) * Float64(0.5 / a)) end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[N[(b * b + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(a \cdot -4\right)}{b + \sqrt{\mathsf{fma}\left(b, b, -4 \cdot \left(c \cdot a\right)\right)}} \cdot \frac{0.5}{a}
\end{array}
Initial program 56.6%
/-rgt-identity56.6%
metadata-eval56.6%
associate-/l*56.6%
associate-*r/56.6%
+-commutative56.6%
unsub-neg56.6%
fma-neg56.8%
associate-*l*56.8%
*-commutative56.8%
distribute-rgt-neg-in56.8%
metadata-eval56.8%
associate-/r*56.8%
metadata-eval56.8%
metadata-eval56.8%
Simplified56.8%
fma-udef56.6%
*-commutative56.6%
Applied egg-rr56.6%
flip--56.4%
add-sqr-sqrt58.1%
fma-def57.8%
fma-def57.8%
Applied egg-rr57.8%
*-commutative57.8%
*-commutative57.8%
*-commutative57.8%
+-commutative57.8%
*-commutative57.8%
*-commutative57.8%
*-commutative57.8%
Simplified57.8%
Taylor expanded in b around 0 99.2%
*-commutative99.2%
metadata-eval99.2%
distribute-rgt-neg-in99.2%
associate-*r*99.2%
*-commutative99.2%
distribute-rgt-neg-in99.2%
distribute-lft-neg-in99.2%
metadata-eval99.2%
*-commutative99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (a b c) :precision binary64 (if (<= b 105.0) (/ (- (sqrt (fma b b (* -4.0 (* c a)))) b) (* a 2.0)) (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 105.0) {
tmp = (sqrt(fma(b, b, (-4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((c * c) / (pow(b, 3.0) / a));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 105.0) tmp = Float64(Float64(sqrt(fma(b, b, Float64(-4.0 * Float64(c * a)))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(c * c) / Float64((b ^ 3.0) / a))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 105.0], N[(N[(N[Sqrt[N[(b * b + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 105:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, -4 \cdot \left(c \cdot a\right)\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 105Initial program 79.0%
*-commutative79.0%
+-commutative79.0%
unsub-neg79.0%
fma-neg79.0%
associate-*l*79.0%
*-commutative79.0%
distribute-rgt-neg-in79.0%
metadata-eval79.0%
Simplified79.0%
if 105 < b Initial program 46.4%
/-rgt-identity46.4%
metadata-eval46.4%
associate-/l*46.4%
associate-*r/46.4%
+-commutative46.4%
unsub-neg46.4%
fma-neg46.7%
associate-*l*46.7%
*-commutative46.7%
distribute-rgt-neg-in46.7%
metadata-eval46.7%
associate-/r*46.7%
metadata-eval46.7%
metadata-eval46.7%
Simplified46.7%
fma-udef46.4%
*-commutative46.4%
Applied egg-rr46.4%
Taylor expanded in b around inf 88.5%
+-commutative88.5%
mul-1-neg88.5%
unsub-neg88.5%
associate-*r/88.5%
neg-mul-188.5%
associate-/l*88.5%
unpow288.5%
Simplified88.5%
Final simplification85.5%
(FPCore (a b c) :precision binary64 (if (<= b 105.0) (* (/ 0.5 a) (- (sqrt (+ (* b b) (* -4.0 (* c a)))) b)) (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 105.0) {
tmp = (0.5 / a) * (sqrt(((b * b) + (-4.0 * (c * a)))) - b);
} else {
tmp = (-c / b) - ((c * c) / (pow(b, 3.0) / a));
}
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 <= 105.0d0) then
tmp = (0.5d0 / a) * (sqrt(((b * b) + ((-4.0d0) * (c * a)))) - b)
else
tmp = (-c / b) - ((c * c) / ((b ** 3.0d0) / a))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 105.0) {
tmp = (0.5 / a) * (Math.sqrt(((b * b) + (-4.0 * (c * a)))) - b);
} else {
tmp = (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 105.0: tmp = (0.5 / a) * (math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) else: tmp = (-c / b) - ((c * c) / (math.pow(b, 3.0) / a)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= 105.0) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a)))) - b)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(c * c) / Float64((b ^ 3.0) / a))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 105.0) tmp = (0.5 / a) * (sqrt(((b * b) + (-4.0 * (c * a)))) - b); else tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 105.0], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 105:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{b \cdot b + -4 \cdot \left(c \cdot a\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 105Initial program 79.0%
/-rgt-identity79.0%
metadata-eval79.0%
associate-/l*79.0%
associate-*r/78.9%
+-commutative78.9%
unsub-neg78.9%
fma-neg79.0%
associate-*l*79.0%
*-commutative79.0%
distribute-rgt-neg-in79.0%
metadata-eval79.0%
associate-/r*79.0%
metadata-eval79.0%
metadata-eval79.0%
Simplified79.0%
fma-udef78.9%
*-commutative78.9%
Applied egg-rr78.9%
if 105 < b Initial program 46.4%
/-rgt-identity46.4%
metadata-eval46.4%
associate-/l*46.4%
associate-*r/46.4%
+-commutative46.4%
unsub-neg46.4%
fma-neg46.7%
associate-*l*46.7%
*-commutative46.7%
distribute-rgt-neg-in46.7%
metadata-eval46.7%
associate-/r*46.7%
metadata-eval46.7%
metadata-eval46.7%
Simplified46.7%
fma-udef46.4%
*-commutative46.4%
Applied egg-rr46.4%
Taylor expanded in b around inf 88.5%
+-commutative88.5%
mul-1-neg88.5%
unsub-neg88.5%
associate-*r/88.5%
neg-mul-188.5%
associate-/l*88.5%
unpow288.5%
Simplified88.5%
Final simplification85.5%
(FPCore (a b c) :precision binary64 (if (<= b 105.0) (/ (- (sqrt (+ (* b b) (* -4.0 (* c a)))) b) (* a 2.0)) (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 105.0) {
tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((c * c) / (pow(b, 3.0) / a));
}
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 <= 105.0d0) then
tmp = (sqrt(((b * b) + ((-4.0d0) * (c * a)))) - b) / (a * 2.0d0)
else
tmp = (-c / b) - ((c * c) / ((b ** 3.0d0) / a))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 105.0) {
tmp = (Math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 105.0: tmp = (math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0) else: tmp = (-c / b) - ((c * c) / (math.pow(b, 3.0) / a)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= 105.0) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a)))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(c * c) / Float64((b ^ 3.0) / a))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 105.0) tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0); else tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 105.0], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 105:\\
\;\;\;\;\frac{\sqrt{b \cdot b + -4 \cdot \left(c \cdot a\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 105Initial program 79.0%
*-commutative79.0%
+-commutative79.0%
unsub-neg79.0%
fma-neg79.0%
associate-*l*79.0%
*-commutative79.0%
distribute-rgt-neg-in79.0%
metadata-eval79.0%
Simplified79.0%
fma-udef78.9%
*-commutative78.9%
Applied egg-rr79.0%
if 105 < b Initial program 46.4%
/-rgt-identity46.4%
metadata-eval46.4%
associate-/l*46.4%
associate-*r/46.4%
+-commutative46.4%
unsub-neg46.4%
fma-neg46.7%
associate-*l*46.7%
*-commutative46.7%
distribute-rgt-neg-in46.7%
metadata-eval46.7%
associate-/r*46.7%
metadata-eval46.7%
metadata-eval46.7%
Simplified46.7%
fma-udef46.4%
*-commutative46.4%
Applied egg-rr46.4%
Taylor expanded in b around inf 88.5%
+-commutative88.5%
mul-1-neg88.5%
unsub-neg88.5%
associate-*r/88.5%
neg-mul-188.5%
associate-/l*88.5%
unpow288.5%
Simplified88.5%
Final simplification85.5%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a))))
double code(double a, double b, double c) {
return (-c / b) - ((c * c) / (pow(b, 3.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 = (-c / b) - ((c * c) / ((b ** 3.0d0) / a))
end function
public static double code(double a, double b, double c) {
return (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
def code(a, b, c): return (-c / b) - ((c * c) / (math.pow(b, 3.0) / a))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(Float64(c * c) / Float64((b ^ 3.0) / a))) end
function tmp = code(a, b, c) tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}
\end{array}
Initial program 56.6%
/-rgt-identity56.6%
metadata-eval56.6%
associate-/l*56.6%
associate-*r/56.6%
+-commutative56.6%
unsub-neg56.6%
fma-neg56.8%
associate-*l*56.8%
*-commutative56.8%
distribute-rgt-neg-in56.8%
metadata-eval56.8%
associate-/r*56.8%
metadata-eval56.8%
metadata-eval56.8%
Simplified56.8%
fma-udef56.6%
*-commutative56.6%
Applied egg-rr56.6%
Taylor expanded in b around inf 80.6%
+-commutative80.6%
mul-1-neg80.6%
unsub-neg80.6%
associate-*r/80.6%
neg-mul-180.6%
associate-/l*80.6%
unpow280.6%
Simplified80.6%
Final simplification80.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(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 56.6%
neg-sub056.6%
associate-+l-56.6%
sub0-neg56.6%
neg-mul-156.6%
associate-*l/56.6%
*-commutative56.6%
associate-/r*56.6%
/-rgt-identity56.6%
metadata-eval56.6%
Simplified56.6%
Taylor expanded in b around inf 63.3%
associate-*r/63.3%
neg-mul-163.3%
Simplified63.3%
Final simplification63.3%
(FPCore (a b c) :precision binary64 (/ 0.0 a))
double code(double a, double b, double c) {
return 0.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 = 0.0d0 / a
end function
public static double code(double a, double b, double c) {
return 0.0 / a;
}
def code(a, b, c): return 0.0 / a
function code(a, b, c) return Float64(0.0 / a) end
function tmp = code(a, b, c) tmp = 0.0 / a; end
code[a_, b_, c_] := N[(0.0 / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{0}{a}
\end{array}
Initial program 56.6%
add-log-exp52.2%
neg-mul-152.2%
fma-def52.2%
*-commutative52.2%
*-commutative52.2%
*-commutative52.2%
Applied egg-rr52.2%
Taylor expanded in c around 0 3.2%
associate-*r/3.2%
distribute-rgt1-in3.2%
metadata-eval3.2%
mul0-lft3.2%
metadata-eval3.2%
Simplified3.2%
Final simplification3.2%
herbie shell --seed 2023189
(FPCore (a b c)
:name "Quadratic roots, narrow range"
:precision binary64
:pre (and (and (and (< 1.0536712127723509e-8 a) (< a 94906265.62425156)) (and (< 1.0536712127723509e-8 b) (< b 94906265.62425156))) (and (< 1.0536712127723509e-8 c) (< c 94906265.62425156)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))