Skip to content

bpo-41513: Improve hypot() accuracy with three separate accumulators #22032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ Given that csum >= 1.0, we have:
Since lo**2 is less than 1/2 ulp(csum), we have csum+lo*lo == csum.

To minimize loss of information during the accumulation of fractional
values, the lo**2 term has a separate accumulator.
values, each term has a separate accumulator.

The square root differential correction is needed because a
correctly rounded square root of a correctly rounded sum of
Expand Down Expand Up @@ -2487,7 +2487,7 @@ static inline double
vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
{
const double T27 = 134217729.0; /* ldexp(1.0, 27)+1.0) */
double x, csum = 1.0, oldcsum, frac = 0.0, frac_lo = 0.0, scale;
double x, csum = 1.0, oldcsum, scale, frac=0.0, frac_mid=0.0, frac_lo=0.0;
double t, hi, lo, h;
int max_e;
Py_ssize_t i;
Expand Down Expand Up @@ -2529,12 +2529,12 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
assert(fabs(csum) >= fabs(x));
oldcsum = csum;
csum += x;
frac += (oldcsum - csum) + x;
frac_mid += (oldcsum - csum) + x;

assert(csum + lo * lo == csum);
frac_lo += lo * lo;
}
frac += frac_lo;
frac += frac_lo + frac_mid;
h = sqrt(csum - 1.0 + frac);

x = h;
Expand Down