Skip to content

Commit a47f394

Browse files
authored
Minor code clean-up. Don't alter the input vector. Use variables instead. GH-8748
1 parent 0041459 commit a47f394

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Modules/mathmodule.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ the *vec* is a NaN.
20562056
static inline double
20572057
vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
20582058
{
2059-
double x, csum = 0.0, oldcsum, frac = 0.0;
2059+
double x, csum = 0.0, oldcsum, frac = 0.0, last;
20602060
Py_ssize_t i;
20612061

20622062
if (Py_IS_INFINITY(max)) {
@@ -2069,20 +2069,21 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
20692069
return 0.0;
20702070
}
20712071
assert(n > 0);
2072+
last = vec[n-1];
20722073
for (i=0 ; i < n-1 ; i++) {
20732074
x = vec[i];
20742075
assert(Py_IS_FINITE(x) && x >= 0.0 && x <= max);
20752076
if (x == max) {
2076-
x = vec[n-1];
2077-
vec[n-1] = max;
2077+
x = last;
2078+
last = max;
20782079
}
20792080
x /= max;
20802081
x = x*x - frac;
20812082
oldcsum = csum;
20822083
csum += x;
20832084
frac = (csum - oldcsum) - x;
20842085
}
2085-
assert(vec[n-1] == max);
2086+
assert(last == max);
20862087
csum += 1.0 - frac;
20872088
return max * sqrt(csum);
20882089
}

0 commit comments

Comments
 (0)