Skip to content

Commit

Permalink
Minor code clean-up. Don't alter the input vector. Use variables inst…
Browse files Browse the repository at this point in the history
…ead. GH-8748
  • Loading branch information
rhettinger committed Aug 12, 2018
1 parent 0041459 commit a47f394
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Modules/mathmodule.c
Expand Up @@ -2056,7 +2056,7 @@ the *vec* is a NaN.
static inline double
vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
{
double x, csum = 0.0, oldcsum, frac = 0.0;
double x, csum = 0.0, oldcsum, frac = 0.0, last;
Py_ssize_t i;

if (Py_IS_INFINITY(max)) {
Expand All @@ -2069,20 +2069,21 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
return 0.0;
}
assert(n > 0);
last = vec[n-1];
for (i=0 ; i < n-1 ; i++) {
x = vec[i];
assert(Py_IS_FINITE(x) && x >= 0.0 && x <= max);
if (x == max) {
x = vec[n-1];
vec[n-1] = max;
x = last;
last = max;
}
x /= max;
x = x*x - frac;
oldcsum = csum;
csum += x;
frac = (csum - oldcsum) - x;
}
assert(vec[n-1] == max);
assert(last == max);
csum += 1.0 - frac;
return max * sqrt(csum);
}
Expand Down

0 comments on commit a47f394

Please sign in to comment.