
(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 (/ (/ (* 4.0 (* c a)) (- (- b) (sqrt (- (* b b) (* c (* 4.0 a)))))) (* a 2.0)))
double code(double a, double b, double c) {
return ((4.0 * (c * a)) / (-b - sqrt(((b * b) - (c * (4.0 * a)))))) / (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)) / (-b - sqrt(((b * b) - (c * (4.0d0 * a)))))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return ((4.0 * (c * a)) / (-b - Math.sqrt(((b * b) - (c * (4.0 * a)))))) / (a * 2.0);
}
def code(a, b, c): return ((4.0 * (c * a)) / (-b - math.sqrt(((b * b) - (c * (4.0 * a)))))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(4.0 * Float64(c * a)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a)))))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = ((4.0 * (c * a)) / (-b - sqrt(((b * b) - (c * (4.0 * a)))))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(4.0 * a), $MachinePrecision]), $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{b \cdot b - c \cdot \left(4 \cdot a\right)}}}{a \cdot 2}
\end{array}
Initial program 56.7%
flip-+56.9%
pow256.9%
add-sqr-sqrt58.4%
*-commutative58.4%
*-commutative58.4%
*-commutative58.4%
*-commutative58.4%
Applied egg-rr58.4%
Taylor expanded in b around 0 99.2%
Final simplification99.2%
(FPCore (a b c) :precision binary64 (if (<= (/ (- (sqrt (- (* b b) (* c (* 4.0 a)))) b) (* a 2.0)) -0.0004) (/ (- (sqrt (+ (* b b) (* (* c a) -4.0))) b) (* a 2.0)) (- (/ (- c) b) (/ (* c (* c a)) (pow b 3.0)))))
double code(double a, double b, double c) {
double tmp;
if (((sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -0.0004) {
tmp = (sqrt(((b * b) + ((c * a) * -4.0))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((c * (c * a)) / pow(b, 3.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) :: tmp
if (((sqrt(((b * b) - (c * (4.0d0 * a)))) - b) / (a * 2.0d0)) <= (-0.0004d0)) then
tmp = (sqrt(((b * b) + ((c * a) * (-4.0d0)))) - b) / (a * 2.0d0)
else
tmp = (-c / b) - ((c * (c * a)) / (b ** 3.0d0))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (((Math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -0.0004) {
tmp = (Math.sqrt(((b * b) + ((c * a) * -4.0))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((c * (c * a)) / Math.pow(b, 3.0));
}
return tmp;
}
def code(a, b, c): tmp = 0 if ((math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -0.0004: tmp = (math.sqrt(((b * b) + ((c * a) * -4.0))) - b) / (a * 2.0) else: tmp = (-c / b) - ((c * (c * a)) / math.pow(b, 3.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.0004) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(Float64(c * a) * -4.0))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(c * Float64(c * a)) / (b ^ 3.0))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (((sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -0.0004) tmp = (sqrt(((b * b) + ((c * a) * -4.0))) - b) / (a * 2.0); else tmp = (-c / b) - ((c * (c * a)) / (b ^ 3.0)); end tmp_2 = 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.0004], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $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.0004:\\
\;\;\;\;\frac{\sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot \left(c \cdot a\right)}{{b}^{3}}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) < -4.00000000000000019e-4Initial program 78.5%
*-commutative78.5%
+-commutative78.5%
unsub-neg78.5%
fma-neg78.5%
associate-*l*78.5%
*-commutative78.5%
distribute-rgt-neg-in78.5%
metadata-eval78.5%
Simplified78.5%
fma-udef78.5%
*-commutative78.5%
Applied egg-rr78.5%
if -4.00000000000000019e-4 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) Initial program 43.5%
neg-sub043.5%
associate-+l-43.5%
sub0-neg43.5%
neg-mul-143.5%
associate-*l/43.5%
*-commutative43.5%
associate-/r*43.5%
/-rgt-identity43.5%
metadata-eval43.5%
Simplified43.5%
Taylor expanded in b around inf 90.6%
+-commutative90.6%
mul-1-neg90.6%
unsub-neg90.6%
associate-*r/90.6%
neg-mul-190.6%
unpow290.6%
associate-*l*90.6%
Simplified90.6%
Final simplification86.0%
(FPCore (a b c) :precision binary64 (if (<= b 2.6) (* (- (sqrt (+ (* b b) (* (* c a) -4.0))) b) (/ 0.5 a)) (- (/ (- c) b) (/ (* c (* c a)) (pow b 3.0)))))
double code(double a, double b, double c) {
double tmp;
if (b <= 2.6) {
tmp = (sqrt(((b * b) + ((c * a) * -4.0))) - b) * (0.5 / a);
} else {
tmp = (-c / b) - ((c * (c * a)) / pow(b, 3.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) :: tmp
if (b <= 2.6d0) then
tmp = (sqrt(((b * b) + ((c * a) * (-4.0d0)))) - b) * (0.5d0 / a)
else
tmp = (-c / b) - ((c * (c * a)) / (b ** 3.0d0))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 2.6) {
tmp = (Math.sqrt(((b * b) + ((c * a) * -4.0))) - b) * (0.5 / a);
} else {
tmp = (-c / b) - ((c * (c * a)) / Math.pow(b, 3.0));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 2.6: tmp = (math.sqrt(((b * b) + ((c * a) * -4.0))) - b) * (0.5 / a) else: tmp = (-c / b) - ((c * (c * a)) / math.pow(b, 3.0)) return tmp
function code(a, b, c) tmp = 0.0 if (b <= 2.6) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(Float64(c * a) * -4.0))) - b) * Float64(0.5 / a)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(c * Float64(c * a)) / (b ^ 3.0))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 2.6) tmp = (sqrt(((b * b) + ((c * a) * -4.0))) - b) * (0.5 / a); else tmp = (-c / b) - ((c * (c * a)) / (b ^ 3.0)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 2.6], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 2.6:\\
\;\;\;\;\left(\sqrt{b \cdot b + \left(c \cdot a\right) \cdot -4} - b\right) \cdot \frac{0.5}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{c \cdot \left(c \cdot a\right)}{{b}^{3}}\\
\end{array}
\end{array}
if b < 2.60000000000000009Initial program 81.5%
/-rgt-identity81.5%
metadata-eval81.5%
associate-/l*81.5%
associate-*r/81.4%
+-commutative81.4%
unsub-neg81.4%
fma-neg81.6%
associate-*l*81.6%
*-commutative81.6%
distribute-rgt-neg-in81.6%
metadata-eval81.6%
associate-/r*81.6%
metadata-eval81.6%
metadata-eval81.6%
Simplified81.6%
fma-udef81.4%
*-commutative81.4%
Applied egg-rr81.4%
if 2.60000000000000009 < b Initial program 51.4%
neg-sub051.4%
associate-+l-51.4%
sub0-neg51.4%
neg-mul-151.4%
associate-*l/51.4%
*-commutative51.4%
associate-/r*51.4%
/-rgt-identity51.4%
metadata-eval51.4%
Simplified51.4%
Taylor expanded in b around inf 84.9%
+-commutative84.9%
mul-1-neg84.9%
unsub-neg84.9%
associate-*r/84.9%
neg-mul-184.9%
unpow284.9%
associate-*l*84.9%
Simplified84.9%
Final simplification84.3%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (/ (* c (* c a)) (pow b 3.0))))
double code(double a, double b, double c) {
return (-c / b) - ((c * (c * a)) / 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) - ((c * (c * a)) / (b ** 3.0d0))
end function
public static double code(double a, double b, double c) {
return (-c / b) - ((c * (c * a)) / Math.pow(b, 3.0));
}
def code(a, b, c): return (-c / b) - ((c * (c * a)) / math.pow(b, 3.0))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(Float64(c * Float64(c * a)) / (b ^ 3.0))) end
function tmp = code(a, b, c) tmp = (-c / b) - ((c * (c * a)) / (b ^ 3.0)); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{c \cdot \left(c \cdot a\right)}{{b}^{3}}
\end{array}
Initial program 56.7%
neg-sub056.7%
associate-+l-56.7%
sub0-neg56.7%
neg-mul-156.7%
associate-*l/56.7%
*-commutative56.7%
associate-/r*56.7%
/-rgt-identity56.7%
metadata-eval56.7%
Simplified56.7%
Taylor expanded in b around inf 80.7%
+-commutative80.7%
mul-1-neg80.7%
unsub-neg80.7%
associate-*r/80.7%
neg-mul-180.7%
unpow280.7%
associate-*l*80.7%
Simplified80.7%
Final simplification80.7%
(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.7%
neg-sub056.7%
associate-+l-56.7%
sub0-neg56.7%
neg-mul-156.7%
associate-*l/56.7%
*-commutative56.7%
associate-/r*56.7%
/-rgt-identity56.7%
metadata-eval56.7%
Simplified56.7%
Taylor expanded in b around inf 63.1%
associate-*r/63.1%
neg-mul-163.1%
Simplified63.1%
Final simplification63.1%
(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.7%
+-commutative56.7%
add-log-exp30.3%
add-log-exp29.4%
sum-log29.4%
*-commutative29.4%
*-commutative29.4%
Applied egg-rr29.4%
Taylor expanded in c around 0 2.3%
associate-*r/2.3%
exp-neg1.7%
lft-mult-inverse3.2%
metadata-eval3.2%
metadata-eval3.2%
Simplified3.2%
Final simplification3.2%
herbie shell --seed 2023224
(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)))