
(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 (* c (/ 2.0 (- (- b) (sqrt (fma -4.0 (* c a) (pow b 2.0)))))))
double code(double a, double b, double c) {
return c * (2.0 / (-b - sqrt(fma(-4.0, (c * a), pow(b, 2.0)))));
}
function code(a, b, c) return Float64(c * Float64(2.0 / Float64(Float64(-b) - sqrt(fma(-4.0, Float64(c * a), (b ^ 2.0)))))) end
code[a_, b_, c_] := N[(c * N[(2.0 / N[((-b) - N[Sqrt[N[(-4.0 * N[(c * a), $MachinePrecision] + N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \frac{2}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, c \cdot a, {b}^{2}\right)}}
\end{array}
Initial program 59.9%
*-commutative59.9%
Simplified59.9%
add-sqr-sqrt59.9%
sqrt-unprod59.9%
pow259.9%
pow259.9%
pow-prod-up59.9%
metadata-eval59.9%
Applied egg-rr59.9%
flip-+59.5%
pow259.5%
sqrt-pow159.8%
metadata-eval59.8%
sqrt-pow159.7%
metadata-eval59.7%
add-sqr-sqrt61.2%
associate-*l*61.2%
Applied egg-rr61.2%
Taylor expanded in b around 0 99.3%
associate-*r*99.3%
*-commutative99.3%
*-commutative99.3%
Simplified99.3%
div-inv99.1%
associate-/l*99.2%
cancel-sign-sub-inv99.2%
unpow299.2%
metadata-eval99.2%
fma-define99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.2%
fma-undefine99.1%
unpow299.1%
+-commutative99.1%
fma-define99.1%
times-frac99.3%
*-rgt-identity99.3%
*-commutative99.3%
associate-*r/99.3%
associate-/r*99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (let* ((t_0 (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* 2.0 a)))) (if (<= t_0 -0.00066) t_0 (/ 1.0 (/ (- (* a (/ c b)) b) c)))))
double code(double a, double b, double c) {
double t_0 = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (2.0 * a);
double tmp;
if (t_0 <= -0.00066) {
tmp = t_0;
} else {
tmp = 1.0 / (((a * (c / b)) - b) / c);
}
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) / (2.0d0 * a)
if (t_0 <= (-0.00066d0)) then
tmp = t_0
else
tmp = 1.0d0 / (((a * (c / b)) - b) / c)
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) / (2.0 * a);
double tmp;
if (t_0 <= -0.00066) {
tmp = t_0;
} else {
tmp = 1.0 / (((a * (c / b)) - b) / c);
}
return tmp;
}
def code(a, b, c): t_0 = (math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (2.0 * a) tmp = 0 if t_0 <= -0.00066: tmp = t_0 else: tmp = 1.0 / (((a * (c / b)) - b) / c) return tmp
function code(a, b, c) t_0 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(2.0 * a)) tmp = 0.0 if (t_0 <= -0.00066) tmp = t_0; else tmp = Float64(1.0 / Float64(Float64(Float64(a * Float64(c / b)) - b) / c)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (2.0 * a); tmp = 0.0; if (t_0 <= -0.00066) tmp = t_0; else tmp = 1.0 / (((a * (c / b)) - b) / c); 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[(2.0 * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.00066], t$95$0, N[(1.0 / N[(N[(N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{2 \cdot a}\\
\mathbf{if}\;t\_0 \leq -0.00066:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{a \cdot \frac{c}{b} - b}{c}}\\
\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)) < -6.6e-4Initial program 80.2%
if -6.6e-4 < (/.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 47.5%
*-commutative47.5%
Simplified47.5%
Taylor expanded in b around inf 88.8%
mul-1-neg88.8%
unsub-neg88.8%
mul-1-neg88.8%
associate-/l*88.8%
Simplified88.8%
clear-num88.6%
inv-pow88.6%
div-inv88.6%
pow-flip88.6%
metadata-eval88.6%
Applied egg-rr88.6%
unpow-188.6%
cancel-sign-sub-inv88.6%
*-commutative88.6%
+-commutative88.6%
unsub-neg88.6%
distribute-rgt-neg-out88.6%
*-commutative88.6%
distribute-rgt-neg-in88.6%
*-commutative88.6%
distribute-rgt-neg-in88.6%
Simplified88.6%
Taylor expanded in c around 0 89.2%
neg-mul-189.2%
+-commutative89.2%
sub-neg89.2%
associate-/l*89.2%
Simplified89.2%
Final simplification85.8%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 4.0)) (- (- b) (sqrt (- (* b b) (* (* c a) 4.0))))) (* 2.0 a)))
double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (-b - sqrt(((b * b) - ((c * a) * 4.0))))) / (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 = ((c * (a * 4.0d0)) / (-b - sqrt(((b * b) - ((c * a) * 4.0d0))))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (-b - Math.sqrt(((b * b) - ((c * a) * 4.0))))) / (2.0 * a);
}
def code(a, b, c): return ((c * (a * 4.0)) / (-b - math.sqrt(((b * b) - ((c * a) * 4.0))))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 4.0)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(Float64(c * a) * 4.0))))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = ((c * (a * 4.0)) / (-b - sqrt(((b * b) - ((c * a) * 4.0))))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(c * a), $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 4\right)}{\left(-b\right) - \sqrt{b \cdot b - \left(c \cdot a\right) \cdot 4}}}{2 \cdot a}
\end{array}
Initial program 59.9%
*-commutative59.9%
Simplified59.9%
add-sqr-sqrt59.9%
sqrt-unprod59.9%
pow259.9%
pow259.9%
pow-prod-up59.9%
metadata-eval59.9%
Applied egg-rr59.9%
flip-+59.5%
pow259.5%
sqrt-pow159.8%
metadata-eval59.8%
sqrt-pow159.7%
metadata-eval59.7%
add-sqr-sqrt61.2%
associate-*l*61.2%
Applied egg-rr61.2%
Taylor expanded in b around 0 99.3%
associate-*r*99.3%
*-commutative99.3%
*-commutative99.3%
Simplified99.3%
unpow299.3%
Applied egg-rr99.3%
Final simplification99.3%
(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 59.9%
*-commutative59.9%
Simplified59.9%
Taylor expanded in b around inf 77.7%
mul-1-neg77.7%
unsub-neg77.7%
mul-1-neg77.7%
associate-/l*77.7%
Simplified77.7%
clear-num77.7%
inv-pow77.7%
div-inv77.7%
pow-flip77.7%
metadata-eval77.7%
Applied egg-rr77.7%
unpow-177.7%
cancel-sign-sub-inv77.7%
*-commutative77.7%
+-commutative77.7%
unsub-neg77.7%
distribute-rgt-neg-out77.7%
*-commutative77.7%
distribute-rgt-neg-in77.7%
*-commutative77.7%
distribute-rgt-neg-in77.7%
Simplified77.7%
Taylor expanded in a around 0 78.5%
neg-mul-178.5%
distribute-frac-neg278.5%
+-commutative78.5%
distribute-frac-neg278.5%
unsub-neg78.5%
Simplified78.5%
(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 59.9%
*-commutative59.9%
Simplified59.9%
Taylor expanded in b around inf 60.8%
associate-*r/60.8%
mul-1-neg60.8%
Simplified60.8%
Final simplification60.8%
herbie shell --seed 2024117
(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)))