Skip to content

Commit e4475d5

Browse files
committed
Perform explicit casting for operations involving the number of indexed elements
Based on C99 conversion rules, int64_t (long long) is converted to a float when another operand is a float. For long arrays, this is problematic once the array length is greater than the maximum safe single-precision floating-point integer (16777215). For example, when calculating the arithmetic mean for long arrays, an odd length may be converted to an even single-precision floating-point number, thus leading to an inaccurate computation. This commit works around conversion issues by converting operands to double-precision before conversion to single-precision. This ensures that we can support array lengths of up to 2**53 - 1, matching the double-precision strided array variants.
1 parent 7397f3e commit e4475d5

File tree

52 files changed

+130
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+130
-95
lines changed

lib/node_modules/@stdlib/stats/base/dmeankbn/src/dmeankbn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ double stdlib_strided_dmeankbn( const int64_t N, const double *X, const int64_t
4343
if ( N == 1 || stride == 0 ) {
4444
return X[ 0 ];
4545
}
46-
return stdlib_strided_dsum( N, X, stride ) / N;
46+
return stdlib_strided_dsum( N, X, stride ) / (double)N;
4747
}

lib/node_modules/@stdlib/stats/base/dmeankbn2/src/dmeankbn2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ double stdlib_strided_dmeankbn2( const int64_t N, const double *X, const int64_t
4343
if ( N == 1 || stride == 0 ) {
4444
return X[ 0 ];
4545
}
46-
return stdlib_strided_dsumkbn2( N, X, stride ) / N;
46+
return stdlib_strided_dsumkbn2( N, X, stride ) / (double)N;
4747
}

lib/node_modules/@stdlib/stats/base/dmeanli/src/dmeanli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ double stdlib_strided_dmeanli( const int64_t N, const double *X, const int64_t s
4646
} else {
4747
ix = 0;
4848
}
49-
return X[ ix ] + ( stdlib_strided_dapxsum( N-1, -X[ ix ], X+( (stride > 0) ? stride : 0 ), stride ) / N );
49+
return X[ ix ] + ( stdlib_strided_dapxsum( N-1, -X[ ix ], X+( (stride > 0) ? stride : 0 ), stride ) / (double)N );
5050
}

lib/node_modules/@stdlib/stats/base/dmeanlipw/src/dmeanlipw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ double stdlib_strided_dmeanlipw( const int64_t N, const double *X, const int64_t
4646
} else {
4747
ix = 0;
4848
}
49-
return X[ ix ] + ( stdlib_strided_dapxsumpw( N-1, -X[ ix ], X+( (stride > 0) ? stride : 0 ), stride ) / N );
49+
return X[ ix ] + ( stdlib_strided_dapxsumpw( N-1, -X[ ix ], X+( (stride > 0) ? stride : 0 ), stride ) / (double)N );
5050
}

lib/node_modules/@stdlib/stats/base/dmeanors/src/dmeanors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ double stdlib_strided_dmeanors( const int64_t N, const double *X, const int64_t
3535
if ( N == 1 || stride == 0 ) {
3636
return X[ 0 ];
3737
}
38-
return stdlib_strided_dsumors( N, X, stride ) / N;
38+
return stdlib_strided_dsumors( N, X, stride ) / (double)N;
3939
}

lib/node_modules/@stdlib/stats/base/dmeanpn/src/dmeanpn.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @return output value
4040
*/
4141
double stdlib_strided_dmeanpn( const int64_t N, const double *X, const int64_t stride ) {
42+
double dN;
4243
double mu;
4344
double c;
4445

@@ -48,13 +49,15 @@ double stdlib_strided_dmeanpn( const int64_t N, const double *X, const int64_t s
4849
if ( N == 1 || stride == 0 ) {
4950
return X[ 0 ];
5051
}
52+
dN = (double)N;
53+
5154
// Compute an estimate for the mean:
5255
mu = stdlib_strided_dsumpw( N, X, stride );
53-
mu /= N;
56+
mu /= dN;
5457

5558
// Compute an error term:
5659
c = stdlib_strided_dapxsumpw( N, -mu, X, stride );
57-
c /= N;
60+
c /= dN;
5861

5962
return mu + c;
6063
}

lib/node_modules/@stdlib/stats/base/dmeanpw/src/dmeanpw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ double stdlib_strided_dmeanpw( const int64_t N, const double *X, const int64_t s
3535
if ( N == 1 || stride == 0 ) {
3636
return X[ 0 ];
3737
}
38-
return stdlib_strided_dsumpw( N, X, stride ) / N;
38+
return stdlib_strided_dsumpw( N, X, stride ) / (double)N;
3939
}

lib/node_modules/@stdlib/stats/base/dmeanwd/src/dmeanwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ double stdlib_strided_dmeanwd( const int64_t N, const double *X, const int64_t s
6666
n = 0;
6767
for ( i = 0; i < N; i++ ) {
6868
n += 1;
69-
mu += ( X[ix]-mu ) / n;
69+
mu += ( X[ix]-mu ) / (double)n;
7070
ix += stride;
7171
}
7272
return mu;

lib/node_modules/@stdlib/stats/base/dnanmeanors/src/dnanmeanors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ double stdlib_strided_dnanmeanors( const int64_t N, const double *X, const int64
5858
if ( n == 0 ) {
5959
return 0.0 / 0.0; // NaN
6060
}
61-
return s / n;
61+
return s / (double)n;
6262
}

lib/node_modules/@stdlib/stats/base/dnanmeanpn/src/dnanmeanpn.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ double stdlib_strided_dnanmeanpn( const int64_t N, const double *X, const int64_
4141
int64_t i;
4242
int64_t n;
4343
int64_t o;
44+
double dn;
4445
double s;
4546
double t;
4647
double v;
@@ -72,7 +73,8 @@ double stdlib_strided_dnanmeanpn( const int64_t N, const double *X, const int64_
7273
if ( n == 0 ) {
7374
return 0.0 / 0.0; // NaN
7475
}
75-
s /= n;
76+
dn = (double)n;
77+
s /= dn;
7678

7779
// Compute an error term...
7880
t = 0.0;
@@ -84,5 +86,5 @@ double stdlib_strided_dnanmeanpn( const int64_t N, const double *X, const int64_
8486
}
8587
ix += stride;
8688
}
87-
return s + (t/n);
89+
return s + (t/dn);
8890
}

0 commit comments

Comments
 (0)