
(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 (/ (/ (* 4.0 (* c a)) (- (/ (pow b 2.0) (- b)) (sqrt (fma b b (* (* c a) -4.0))))) (* a 2.0)))
double code(double a, double b, double c) {
return ((4.0 * (c * a)) / ((pow(b, 2.0) / -b) - sqrt(fma(b, b, ((c * a) * -4.0))))) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(Float64((b ^ 2.0) / Float64(-b)) - sqrt(fma(b, b, Float64(Float64(c * a) * -4.0))))) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Power[b, 2.0], $MachinePrecision] / (-b)), $MachinePrecision] - N[Sqrt[N[(b * b + N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{4 \cdot \left(c \cdot a\right)}{\frac{{b}^{2}}{-b} - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -4\right)}}}{a \cdot 2}
\end{array}
Initial program 56.1%
*-commutative56.1%
Simplified56.1%
add-sqr-sqrt55.1%
distribute-rgt-neg-in55.1%
Applied egg-rr55.1%
flip-+55.0%
distribute-rgt-neg-out55.0%
add-sqr-sqrt55.8%
distribute-rgt-neg-out55.8%
add-sqr-sqrt56.0%
pow256.0%
add-sqr-sqrt57.5%
pow257.5%
associate-*l*57.5%
Applied egg-rr57.5%
associate--r-99.2%
unpow299.2%
unpow299.2%
difference-of-squares99.2%
neg-mul-199.2%
distribute-lft1-in99.2%
metadata-eval99.2%
mul0-lft99.2%
unpow299.2%
fmm-def99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
neg-sub099.3%
flip--99.3%
metadata-eval99.3%
pow299.3%
add-sqr-sqrt99.2%
sqrt-prod99.3%
sqr-neg99.3%
sqrt-unprod0.0%
add-sqr-sqrt1.4%
sub-neg1.4%
neg-sub01.4%
add-sqr-sqrt0.0%
sqrt-unprod99.3%
sqr-neg99.3%
sqrt-prod99.2%
add-sqr-sqrt99.3%
Applied egg-rr99.3%
neg-sub099.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (if (<= (/ (- (sqrt (- (* b b) (* c (* 4.0 a)))) b) (* a 2.0)) -0.2) (/ (- (sqrt (fma b b (* c (* a -4.0)))) b) (* a 2.0)) (/ (/ (* 4.0 (* c a)) (* 2.0 (- (* a (/ c b)) b))) (* a 2.0))))
double code(double a, double b, double c) {
double tmp;
if (((sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -0.2) {
tmp = (sqrt(fma(b, b, (c * (a * -4.0)))) - b) / (a * 2.0);
} else {
tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0);
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a)))) - b) / Float64(a * 2.0)) <= -0.2) tmp = Float64(Float64(sqrt(fma(b, b, Float64(c * Float64(a * -4.0)))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(2.0 * Float64(Float64(a * Float64(c / b)) - b))) / Float64(a * 2.0)); end return tmp end
code[a_, b_, c_] := If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(4.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -0.2], N[(N[(N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(2.0 * N[(N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} - b}{a \cdot 2} \leq -0.2:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{4 \cdot \left(c \cdot a\right)}{2 \cdot \left(a \cdot \frac{c}{b} - b\right)}}{a \cdot 2}\\
\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)) < -0.20000000000000001Initial program 84.4%
*-commutative84.4%
Simplified84.5%
if -0.20000000000000001 < (/.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%
add-sqr-sqrt46.6%
distribute-rgt-neg-in46.6%
Applied egg-rr46.6%
flip-+46.5%
distribute-rgt-neg-out46.5%
add-sqr-sqrt47.2%
distribute-rgt-neg-out47.2%
add-sqr-sqrt47.3%
pow247.3%
add-sqr-sqrt48.8%
pow248.8%
associate-*l*48.8%
Applied egg-rr48.8%
associate--r-99.3%
unpow299.3%
unpow299.3%
difference-of-squares99.3%
neg-mul-199.3%
distribute-lft1-in99.3%
metadata-eval99.3%
mul0-lft99.3%
unpow299.3%
fmm-def99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in a around 0 88.4%
distribute-lft-out--88.4%
associate-*r/88.4%
Simplified88.4%
Final simplification87.5%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (/ (- (sqrt (- (* b b) (* c (* 4.0 a)))) b) (* a 2.0))))
(if (<= t_0 -0.2)
t_0
(/ (/ (* 4.0 (* c a)) (* 2.0 (- (* a (/ c b)) b))) (* a 2.0)))))
double code(double a, double b, double c) {
double t_0 = (sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0);
double tmp;
if (t_0 <= -0.2) {
tmp = t_0;
} else {
tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0);
}
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 * (4.0d0 * a)))) - b) / (a * 2.0d0)
if (t_0 <= (-0.2d0)) then
tmp = t_0
else
tmp = ((4.0d0 * (c * a)) / (2.0d0 * ((a * (c / b)) - b))) / (a * 2.0d0)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = (Math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0);
double tmp;
if (t_0 <= -0.2) {
tmp = t_0;
} else {
tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0);
}
return tmp;
}
def code(a, b, c): t_0 = (math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0) tmp = 0 if t_0 <= -0.2: tmp = t_0 else: tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0) return tmp
function code(a, b, c) t_0 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a)))) - b) / Float64(a * 2.0)) tmp = 0.0 if (t_0 <= -0.2) tmp = t_0; else tmp = Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(2.0 * Float64(Float64(a * Float64(c / b)) - b))) / Float64(a * 2.0)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0); tmp = 0.0; if (t_0 <= -0.2) tmp = t_0; else tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0); 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[(4.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.2], t$95$0, N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(2.0 * N[(N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} - b}{a \cdot 2}\\
\mathbf{if}\;t\_0 \leq -0.2:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{4 \cdot \left(c \cdot a\right)}{2 \cdot \left(a \cdot \frac{c}{b} - b\right)}}{a \cdot 2}\\
\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)) < -0.20000000000000001Initial program 84.4%
if -0.20000000000000001 < (/.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%
add-sqr-sqrt46.6%
distribute-rgt-neg-in46.6%
Applied egg-rr46.6%
flip-+46.5%
distribute-rgt-neg-out46.5%
add-sqr-sqrt47.2%
distribute-rgt-neg-out47.2%
add-sqr-sqrt47.3%
pow247.3%
add-sqr-sqrt48.8%
pow248.8%
associate-*l*48.8%
Applied egg-rr48.8%
associate--r-99.3%
unpow299.3%
unpow299.3%
difference-of-squares99.3%
neg-mul-199.3%
distribute-lft1-in99.3%
metadata-eval99.3%
mul0-lft99.3%
unpow299.3%
fmm-def99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in a around 0 88.4%
distribute-lft-out--88.4%
associate-*r/88.4%
Simplified88.4%
Final simplification87.4%
(FPCore (a b c) :precision binary64 (/ (/ (* 4.0 (* c a)) (- (- b) (sqrt (fma b b (* (* c a) -4.0))))) (* a 2.0)))
double code(double a, double b, double c) {
return ((4.0 * (c * a)) / (-b - sqrt(fma(b, b, ((c * a) * -4.0))))) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(Float64(c * a) * -4.0))))) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{4 \cdot \left(c \cdot a\right)}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -4\right)}}}{a \cdot 2}
\end{array}
Initial program 56.1%
*-commutative56.1%
Simplified56.1%
add-sqr-sqrt55.1%
distribute-rgt-neg-in55.1%
Applied egg-rr55.1%
flip-+55.0%
distribute-rgt-neg-out55.0%
add-sqr-sqrt55.8%
distribute-rgt-neg-out55.8%
add-sqr-sqrt56.0%
pow256.0%
add-sqr-sqrt57.5%
pow257.5%
associate-*l*57.5%
Applied egg-rr57.5%
associate--r-99.2%
unpow299.2%
unpow299.2%
difference-of-squares99.2%
neg-mul-199.2%
distribute-lft1-in99.2%
metadata-eval99.2%
mul0-lft99.2%
unpow299.2%
fmm-def99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (/ (/ (* 4.0 (* c a)) (* 2.0 (- (* a (/ c b)) b))) (* a 2.0)))
double code(double a, double b, double c) {
return ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((4.0d0 * (c * a)) / (2.0d0 * ((a * (c / b)) - b))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0);
}
def code(a, b, c): return ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(2.0 * Float64(Float64(a * Float64(c / b)) - b))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = ((4.0 * (c * a)) / (2.0 * ((a * (c / b)) - b))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(2.0 * N[(N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{4 \cdot \left(c \cdot a\right)}{2 \cdot \left(a \cdot \frac{c}{b} - b\right)}}{a \cdot 2}
\end{array}
Initial program 56.1%
*-commutative56.1%
Simplified56.1%
add-sqr-sqrt55.1%
distribute-rgt-neg-in55.1%
Applied egg-rr55.1%
flip-+55.0%
distribute-rgt-neg-out55.0%
add-sqr-sqrt55.8%
distribute-rgt-neg-out55.8%
add-sqr-sqrt56.0%
pow256.0%
add-sqr-sqrt57.5%
pow257.5%
associate-*l*57.5%
Applied egg-rr57.5%
associate--r-99.2%
unpow299.2%
unpow299.2%
difference-of-squares99.2%
neg-mul-199.2%
distribute-lft1-in99.2%
metadata-eval99.2%
mul0-lft99.2%
unpow299.2%
fmm-def99.3%
*-commutative99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in a around 0 80.2%
distribute-lft-out--80.2%
associate-*r/80.2%
Simplified80.2%
(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 56.1%
*-commutative56.1%
Simplified56.2%
Taylor expanded in b around inf 63.7%
associate-*r/63.7%
mul-1-neg63.7%
Simplified63.7%
Final simplification63.7%
(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.1%
*-commutative56.1%
Simplified56.1%
add-sqr-sqrt55.1%
distribute-rgt-neg-in55.1%
Applied egg-rr55.1%
Taylor expanded in a around 0 3.2%
associate-*r/3.2%
distribute-rgt1-in3.2%
metadata-eval3.2%
mul0-lft3.2%
metadata-eval3.2%
Simplified3.2%
herbie shell --seed 2024188
(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)))