
(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 2.0) (/ (- (- b) (sqrt (fma c (* a -4.0) (* b b)))) (* a 4.0)))))
double code(double a, double b, double c) {
return c / ((a * 2.0) * ((-b - sqrt(fma(c, (a * -4.0), (b * b)))) / (a * 4.0)));
}
function code(a, b, c) return Float64(c / Float64(Float64(a * 2.0) * Float64(Float64(Float64(-b) - sqrt(fma(c, Float64(a * -4.0), Float64(b * b)))) / Float64(a * 4.0)))) end
code[a_, b_, c_] := N[(c / N[(N[(a * 2.0), $MachinePrecision] * N[(N[((-b) - N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(a \cdot 2\right) \cdot \frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}{a \cdot 4}}
\end{array}
Initial program 54.6%
flip-+54.3%
pow254.4%
add-sqr-sqrt55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
unpow255.9%
sqr-neg55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
Simplified55.9%
Taylor expanded in b around 0 99.4%
div-inv99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.4%
*-rgt-identity99.4%
associate-/l*99.5%
*-commutative99.5%
Simplified99.5%
*-un-lft-identity99.5%
associate-/l/99.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (a b c) :precision binary64 (if (<= (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0)) -0.1) (* (- (sqrt (+ (* b b) (* -4.0 (* c a)))) b) (/ 0.5 a)) (/ (/ c (+ (* -0.5 (/ b a)) (* 0.5 (/ c b)))) (* a 2.0))))
double code(double a, double b, double c) {
double tmp;
if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -0.1) {
tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) * (0.5 / a);
} else {
tmp = (c / ((-0.5 * (b / a)) + (0.5 * (c / 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) :: tmp
if (((sqrt(((b * b) - (c * (a * 4.0d0)))) - b) / (a * 2.0d0)) <= (-0.1d0)) then
tmp = (sqrt(((b * b) + ((-4.0d0) * (c * a)))) - b) * (0.5d0 / a)
else
tmp = (c / (((-0.5d0) * (b / a)) + (0.5d0 * (c / b)))) / (a * 2.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 * (a * 4.0)))) - b) / (a * 2.0)) <= -0.1) {
tmp = (Math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) * (0.5 / a);
} else {
tmp = (c / ((-0.5 * (b / a)) + (0.5 * (c / b)))) / (a * 2.0);
}
return tmp;
}
def code(a, b, c): tmp = 0 if ((math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -0.1: tmp = (math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) * (0.5 / a) else: tmp = (c / ((-0.5 * (b / a)) + (0.5 * (c / 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(a * 4.0)))) - b) / Float64(a * 2.0)) <= -0.1) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a)))) - b) * Float64(0.5 / a)); else tmp = Float64(Float64(c / Float64(Float64(-0.5 * Float64(b / a)) + Float64(0.5 * Float64(c / b)))) / Float64(a * 2.0)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -0.1) tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) * (0.5 / a); else tmp = (c / ((-0.5 * (b / a)) + (0.5 * (c / b)))) / (a * 2.0); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -0.1], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], N[(N[(c / N[(N[(-0.5 * N[(b / a), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[(c / b), $MachinePrecision]), $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(a \cdot 4\right)} - b}{a \cdot 2} \leq -0.1:\\
\;\;\;\;\left(\sqrt{b \cdot b + -4 \cdot \left(c \cdot a\right)} - b\right) \cdot \frac{0.5}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{c}{-0.5 \cdot \frac{b}{a} + 0.5 \cdot \frac{c}{b}}}{a \cdot 2}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) < -0.10000000000000001Initial program 83.3%
/-rgt-identity83.3%
metadata-eval83.3%
associate-/l*83.3%
associate-*r/83.3%
+-commutative83.3%
unsub-neg83.3%
fma-neg83.4%
associate-*l*83.4%
*-commutative83.4%
distribute-rgt-neg-in83.4%
metadata-eval83.4%
associate-/r*83.4%
metadata-eval83.4%
metadata-eval83.4%
Simplified83.4%
fma-udef83.3%
*-commutative83.3%
Applied egg-rr83.3%
if -0.10000000000000001 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) Initial program 47.8%
flip-+47.5%
pow247.6%
add-sqr-sqrt49.1%
*-commutative49.1%
*-commutative49.1%
*-commutative49.1%
*-commutative49.1%
Applied egg-rr49.1%
unpow249.1%
sqr-neg49.1%
sub-neg49.1%
+-commutative49.1%
distribute-rgt-neg-in49.1%
fma-def49.1%
distribute-rgt-neg-in49.1%
metadata-eval49.1%
sub-neg49.1%
+-commutative49.1%
distribute-rgt-neg-in49.1%
fma-def49.1%
distribute-rgt-neg-in49.1%
metadata-eval49.1%
Simplified49.1%
Taylor expanded in b around 0 99.4%
div-inv99.1%
*-commutative99.1%
associate-*r*99.1%
*-commutative99.1%
*-commutative99.1%
Applied egg-rr99.1%
associate-*r/99.4%
*-rgt-identity99.4%
associate-/l*99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in b around inf 86.9%
Final simplification86.2%
(FPCore (a b c) :precision binary64 (/ (* (/ c (- (- b) (sqrt (fma c (* a -4.0) (* b b))))) (* a 4.0)) (* a 2.0)))
double code(double a, double b, double c) {
return ((c / (-b - sqrt(fma(c, (a * -4.0), (b * b))))) * (a * 4.0)) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(c / Float64(Float64(-b) - sqrt(fma(c, Float64(a * -4.0), Float64(b * b))))) * Float64(a * 4.0)) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(c / N[((-b) - N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}} \cdot \left(a \cdot 4\right)}{a \cdot 2}
\end{array}
Initial program 54.6%
flip-+54.3%
pow254.4%
add-sqr-sqrt55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
unpow255.9%
sqr-neg55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
Simplified55.9%
Taylor expanded in b around 0 99.4%
div-inv99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.4%
*-rgt-identity99.4%
associate-/l*99.5%
*-commutative99.5%
Simplified99.5%
associate-/r/99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (a b c) :precision binary64 (/ (/ c (/ (- (- b) (sqrt (+ (* b b) (* c (* a -4.0))))) (* a 4.0))) (* a 2.0)))
double code(double a, double b, double c) {
return (c / ((-b - sqrt(((b * b) + (c * (a * -4.0))))) / (a * 4.0))) / (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 = (c / ((-b - sqrt(((b * b) + (c * (a * (-4.0d0)))))) / (a * 4.0d0))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (c / ((-b - Math.sqrt(((b * b) + (c * (a * -4.0))))) / (a * 4.0))) / (a * 2.0);
}
def code(a, b, c): return (c / ((-b - math.sqrt(((b * b) + (c * (a * -4.0))))) / (a * 4.0))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(c / Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(c * Float64(a * -4.0))))) / Float64(a * 4.0))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = (c / ((-b - sqrt(((b * b) + (c * (a * -4.0))))) / (a * 4.0))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(c / N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{\frac{\left(-b\right) - \sqrt{b \cdot b + c \cdot \left(a \cdot -4\right)}}{a \cdot 4}}}{a \cdot 2}
\end{array}
Initial program 54.6%
flip-+54.3%
pow254.4%
add-sqr-sqrt55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
unpow255.9%
sqr-neg55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
Simplified55.9%
Taylor expanded in b around 0 99.4%
div-inv99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.4%
*-rgt-identity99.4%
associate-/l*99.5%
*-commutative99.5%
Simplified99.5%
fma-udef99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (a b c) :precision binary64 (/ (/ c (+ (* -0.5 (/ b a)) (* 0.5 (/ c b)))) (* a 2.0)))
double code(double a, double b, double c) {
return (c / ((-0.5 * (b / a)) + (0.5 * (c / 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 = (c / (((-0.5d0) * (b / a)) + (0.5d0 * (c / b)))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (c / ((-0.5 * (b / a)) + (0.5 * (c / b)))) / (a * 2.0);
}
def code(a, b, c): return (c / ((-0.5 * (b / a)) + (0.5 * (c / b)))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(c / Float64(Float64(-0.5 * Float64(b / a)) + Float64(0.5 * Float64(c / b)))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = (c / ((-0.5 * (b / a)) + (0.5 * (c / b)))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(c / N[(N[(-0.5 * N[(b / a), $MachinePrecision]), $MachinePrecision] + N[(0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{-0.5 \cdot \frac{b}{a} + 0.5 \cdot \frac{c}{b}}}{a \cdot 2}
\end{array}
Initial program 54.6%
flip-+54.3%
pow254.4%
add-sqr-sqrt55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
unpow255.9%
sqr-neg55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
sub-neg55.9%
+-commutative55.9%
distribute-rgt-neg-in55.9%
fma-def55.9%
distribute-rgt-neg-in55.9%
metadata-eval55.9%
Simplified55.9%
Taylor expanded in b around 0 99.4%
div-inv99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.4%
*-rgt-identity99.4%
associate-/l*99.5%
*-commutative99.5%
Simplified99.5%
Taylor expanded in b around inf 81.3%
Final simplification81.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.6%
neg-sub054.6%
associate-+l-54.6%
sub0-neg54.6%
neg-mul-154.6%
associate-*l/54.6%
*-commutative54.6%
associate-/r*54.6%
/-rgt-identity54.6%
metadata-eval54.6%
Simplified54.6%
Taylor expanded in b around inf 64.4%
associate-*r/64.4%
neg-mul-164.4%
Simplified64.4%
Final simplification64.4%
(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 / 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.6%
neg-sub054.6%
associate-+l-54.6%
sub0-neg54.6%
neg-mul-154.6%
associate-*l/54.6%
*-commutative54.6%
associate-/r*54.6%
/-rgt-identity54.6%
metadata-eval54.6%
Simplified54.6%
Taylor expanded in b around -inf 11.6%
mul-1-neg11.6%
unsub-neg11.6%
Simplified11.6%
Taylor expanded in c around inf 1.6%
Final simplification1.6%
herbie shell --seed 2023172
(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)))