
(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 (let* ((t_0 (* (* c a) -4.0))) (* (/ t_0 (+ b (sqrt (fma b b t_0)))) (/ 0.5 a))))
double code(double a, double b, double c) {
double t_0 = (c * a) * -4.0;
return (t_0 / (b + sqrt(fma(b, b, t_0)))) * (0.5 / a);
}
function code(a, b, c) t_0 = Float64(Float64(c * a) * -4.0) return Float64(Float64(t_0 / Float64(b + sqrt(fma(b, b, t_0)))) * Float64(0.5 / a)) end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]}, N[(N[(t$95$0 / N[(b + N[Sqrt[N[(b * b + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(c \cdot a\right) \cdot -4\\
\frac{t_0}{b + \sqrt{\mathsf{fma}\left(b, b, t_0\right)}} \cdot \frac{0.5}{a}
\end{array}
\end{array}
Initial program 54.8%
/-rgt-identity54.8%
metadata-eval54.8%
associate-/l*54.8%
associate-*r/54.8%
+-commutative54.8%
unsub-neg54.8%
fma-neg54.9%
associate-*l*54.9%
*-commutative54.9%
distribute-rgt-neg-in54.9%
metadata-eval54.9%
associate-/r*54.9%
metadata-eval54.9%
metadata-eval54.9%
Simplified54.9%
fma-udef54.8%
*-commutative54.8%
metadata-eval54.8%
cancel-sign-sub-inv54.8%
associate-*l*54.8%
add-sqr-sqrt53.4%
pow253.4%
pow1/253.4%
sqrt-pow153.5%
associate-*l*53.5%
cancel-sign-sub-inv53.5%
metadata-eval53.5%
*-commutative53.5%
fma-udef53.6%
associate-*l*53.6%
metadata-eval53.6%
Applied egg-rr53.6%
flip--53.7%
pow-pow54.8%
metadata-eval54.8%
pow-pow54.8%
metadata-eval54.8%
pow-pow54.8%
metadata-eval54.8%
Applied egg-rr54.8%
pow-sqr56.0%
metadata-eval56.0%
unpow156.0%
associate-*r*56.0%
*-commutative56.0%
+-commutative56.0%
unpow1/256.0%
associate-*r*56.0%
*-commutative56.0%
Simplified56.0%
Taylor expanded in b around 0 99.4%
*-commutative99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (if (<= b 13.6) (* (/ 0.5 a) (- (sqrt (fma b b (* (* c a) -4.0))) b)) (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 13.6) {
tmp = (0.5 / a) * (sqrt(fma(b, b, ((c * a) * -4.0))) - b);
} else {
tmp = (-c / b) - ((c * c) / (pow(b, 3.0) / a));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 13.6) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(fma(b, b, Float64(Float64(c * a) * -4.0))) - b)); 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, 13.6], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(b * b + N[(N[(c * a), $MachinePrecision] * -4.0), $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 13.6:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -4\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 13.5999999999999996Initial program 78.8%
/-rgt-identity78.8%
metadata-eval78.8%
associate-/l*78.8%
associate-*r/78.8%
+-commutative78.8%
unsub-neg78.8%
fma-neg79.1%
associate-*l*79.1%
*-commutative79.1%
distribute-rgt-neg-in79.1%
metadata-eval79.1%
associate-/r*79.1%
metadata-eval79.1%
metadata-eval79.1%
Simplified79.1%
if 13.5999999999999996 < b Initial program 48.2%
/-rgt-identity48.2%
metadata-eval48.2%
associate-/l*48.2%
associate-*r/48.2%
+-commutative48.2%
unsub-neg48.2%
fma-neg48.3%
associate-*l*48.3%
*-commutative48.3%
distribute-rgt-neg-in48.3%
metadata-eval48.3%
associate-/r*48.3%
metadata-eval48.3%
metadata-eval48.3%
Simplified48.3%
Taylor expanded in b around inf 87.7%
+-commutative87.7%
mul-1-neg87.7%
unsub-neg87.7%
mul-1-neg87.7%
associate-/l*87.7%
unpow287.7%
Simplified87.7%
Final simplification85.9%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* a (* c -4.0))))
(if (<= b 13.6)
(* (/ 0.5 a) (- (sqrt (+ (* 2.0 t_0) (- (* b b) t_0))) b))
(- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a))))))
double code(double a, double b, double c) {
double t_0 = a * (c * -4.0);
double tmp;
if (b <= 13.6) {
tmp = (0.5 / a) * (sqrt(((2.0 * t_0) + ((b * b) - t_0))) - 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) :: t_0
real(8) :: tmp
t_0 = a * (c * (-4.0d0))
if (b <= 13.6d0) then
tmp = (0.5d0 / a) * (sqrt(((2.0d0 * t_0) + ((b * b) - t_0))) - 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 t_0 = a * (c * -4.0);
double tmp;
if (b <= 13.6) {
tmp = (0.5 / a) * (Math.sqrt(((2.0 * t_0) + ((b * b) - t_0))) - b);
} else {
tmp = (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
return tmp;
}
def code(a, b, c): t_0 = a * (c * -4.0) tmp = 0 if b <= 13.6: tmp = (0.5 / a) * (math.sqrt(((2.0 * t_0) + ((b * b) - t_0))) - b) else: tmp = (-c / b) - ((c * c) / (math.pow(b, 3.0) / a)) return tmp
function code(a, b, c) t_0 = Float64(a * Float64(c * -4.0)) tmp = 0.0 if (b <= 13.6) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(Float64(2.0 * t_0) + Float64(Float64(b * b) - t_0))) - 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) t_0 = a * (c * -4.0); tmp = 0.0; if (b <= 13.6) tmp = (0.5 / a) * (sqrt(((2.0 * t_0) + ((b * b) - t_0))) - b); else tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, 13.6], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(N[(2.0 * t$95$0), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] - t$95$0), $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}
t_0 := a \cdot \left(c \cdot -4\right)\\
\mathbf{if}\;b \leq 13.6:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{2 \cdot t_0 + \left(b \cdot b - t_0\right)} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 13.5999999999999996Initial program 78.8%
/-rgt-identity78.8%
metadata-eval78.8%
associate-/l*78.8%
associate-*r/78.8%
+-commutative78.8%
unsub-neg78.8%
fma-neg79.1%
associate-*l*79.1%
*-commutative79.1%
distribute-rgt-neg-in79.1%
metadata-eval79.1%
associate-/r*79.1%
metadata-eval79.1%
metadata-eval79.1%
Simplified79.1%
fma-udef78.8%
*-commutative78.8%
metadata-eval78.8%
cancel-sign-sub-inv78.8%
associate-*l*78.8%
*-un-lft-identity78.8%
prod-diff79.1%
Applied egg-rr79.0%
+-commutative79.0%
fma-udef79.0%
*-rgt-identity79.0%
*-rgt-identity79.0%
count-279.0%
*-rgt-identity79.0%
fma-neg79.1%
Simplified79.1%
if 13.5999999999999996 < b Initial program 48.2%
/-rgt-identity48.2%
metadata-eval48.2%
associate-/l*48.2%
associate-*r/48.2%
+-commutative48.2%
unsub-neg48.2%
fma-neg48.3%
associate-*l*48.3%
*-commutative48.3%
distribute-rgt-neg-in48.3%
metadata-eval48.3%
associate-/r*48.3%
metadata-eval48.3%
metadata-eval48.3%
Simplified48.3%
Taylor expanded in b around inf 87.7%
+-commutative87.7%
mul-1-neg87.7%
unsub-neg87.7%
mul-1-neg87.7%
associate-/l*87.7%
unpow287.7%
Simplified87.7%
Final simplification85.9%
(FPCore (a b c) :precision binary64 (if (<= b 14.5) (* (/ 0.5 a) (- (sqrt (- (* b b) (* (* c a) 4.0))) b)) (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 14.5) {
tmp = (0.5 / a) * (sqrt(((b * b) - ((c * a) * 4.0))) - 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 <= 14.5d0) then
tmp = (0.5d0 / a) * (sqrt(((b * b) - ((c * a) * 4.0d0))) - 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 <= 14.5) {
tmp = (0.5 / a) * (Math.sqrt(((b * b) - ((c * a) * 4.0))) - b);
} else {
tmp = (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 14.5: tmp = (0.5 / a) * (math.sqrt(((b * b) - ((c * a) * 4.0))) - 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 <= 14.5) tmp = Float64(Float64(0.5 / a) * Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(c * a) * 4.0))) - 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 <= 14.5) tmp = (0.5 / a) * (sqrt(((b * b) - ((c * a) * 4.0))) - b); else tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 14.5], N[(N[(0.5 / a), $MachinePrecision] * N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(c * a), $MachinePrecision] * 4.0), $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 14.5:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(\sqrt{b \cdot b - \left(c \cdot a\right) \cdot 4} - b\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}\\
\end{array}
\end{array}
if b < 14.5Initial program 78.8%
/-rgt-identity78.8%
metadata-eval78.8%
associate-/l*78.8%
associate-*r/78.8%
+-commutative78.8%
unsub-neg78.8%
fma-neg79.1%
associate-*l*79.1%
*-commutative79.1%
distribute-rgt-neg-in79.1%
metadata-eval79.1%
associate-/r*79.1%
metadata-eval79.1%
metadata-eval79.1%
Simplified79.1%
fma-udef78.8%
*-commutative78.8%
metadata-eval78.8%
cancel-sign-sub-inv78.8%
associate-*l*78.8%
*-un-lft-identity78.8%
prod-diff79.1%
Applied egg-rr79.0%
*-rgt-identity79.0%
fma-neg79.1%
fma-udef79.1%
*-rgt-identity79.1%
*-rgt-identity79.1%
associate--r-78.8%
associate--r+78.8%
+-inverses78.8%
neg-sub078.8%
*-commutative78.8%
*-commutative78.8%
associate-*r*78.8%
*-commutative78.8%
distribute-lft-neg-in78.8%
metadata-eval78.8%
*-commutative78.8%
Simplified78.8%
if 14.5 < b Initial program 48.2%
/-rgt-identity48.2%
metadata-eval48.2%
associate-/l*48.2%
associate-*r/48.2%
+-commutative48.2%
unsub-neg48.2%
fma-neg48.3%
associate-*l*48.3%
*-commutative48.3%
distribute-rgt-neg-in48.3%
metadata-eval48.3%
associate-/r*48.3%
metadata-eval48.3%
metadata-eval48.3%
Simplified48.3%
Taylor expanded in b around inf 87.7%
+-commutative87.7%
mul-1-neg87.7%
unsub-neg87.7%
mul-1-neg87.7%
associate-/l*87.7%
unpow287.7%
Simplified87.7%
Final simplification85.8%
(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 54.8%
/-rgt-identity54.8%
metadata-eval54.8%
associate-/l*54.8%
associate-*r/54.8%
+-commutative54.8%
unsub-neg54.8%
fma-neg54.9%
associate-*l*54.9%
*-commutative54.9%
distribute-rgt-neg-in54.9%
metadata-eval54.9%
associate-/r*54.9%
metadata-eval54.9%
metadata-eval54.9%
Simplified54.9%
Taylor expanded in b around inf 82.3%
+-commutative82.3%
mul-1-neg82.3%
unsub-neg82.3%
mul-1-neg82.3%
associate-/l*82.3%
unpow282.3%
Simplified82.3%
Final simplification82.3%
(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 54.8%
/-rgt-identity54.8%
metadata-eval54.8%
associate-/l*54.8%
associate-*r/54.8%
+-commutative54.8%
unsub-neg54.8%
fma-neg54.9%
associate-*l*54.9%
*-commutative54.9%
distribute-rgt-neg-in54.9%
metadata-eval54.9%
associate-/r*54.9%
metadata-eval54.9%
metadata-eval54.9%
Simplified54.9%
Taylor expanded in b around inf 65.2%
mul-1-neg65.2%
Simplified65.2%
Final simplification65.2%
herbie shell --seed 2023196
(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)))