Skip to content

Commit 950141e

Browse files
committed
fix: update implementation to preserve signed zeros
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 38a7900 commit 950141e

File tree

4 files changed

+117
-11
lines changed

4 files changed

+117
-11
lines changed

lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn/lib/ndarray.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,67 @@ var abs = require( '@stdlib/math/base/special/abs' );
5757
*/
5858
function dnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
5959
var sum;
60+
var flg;
6061
var ix;
6162
var v;
6263
var t;
6364
var c;
6465
var n;
6566
var i;
6667

67-
sum = 0.0;
6868
if ( N <= 0 ) {
69-
out[ offsetOut ] = sum;
69+
out[ offsetOut ] = 0.0;
7070
out[ offsetOut+strideOut ] = 0;
7171
return out;
7272
}
7373
ix = offsetX;
7474
if ( strideX === 0 ) {
7575
if ( isnan( x[ ix ] ) ) {
76-
out[ offsetOut ] = sum;
76+
out[ offsetOut ] = 0.0;
7777
out[ offsetOut+strideOut ] = 0;
7878
return out;
7979
}
8080
out[ offsetOut ] = x[ ix ] * N;
8181
out[ offsetOut+strideOut ] = N;
8282
return out;
8383
}
84-
c = 0.0;
85-
n = 0;
84+
// Find the first non-NaN element...
8685
for ( i = 0; i < N; i++ ) {
86+
v = x[ ix ];
87+
if ( isnan( v ) === false ) {
88+
break;
89+
}
90+
ix += strideX;
91+
}
92+
if ( i === N ) {
93+
out[ offsetOut ] = 0.0;
94+
out[ offsetOut+strideOut ] = 0;
95+
return out;
96+
}
97+
n = 1;
98+
sum = v;
99+
ix += strideX;
100+
i += 1;
101+
102+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
103+
if ( sum === 0.0 ) {
104+
for ( ; i < N; i++ ) {
105+
v = x[ ix ];
106+
if ( isnan( v ) === false ) {
107+
if ( v !== 0.0 ) {
108+
flg = true;
109+
break;
110+
}
111+
sum += v;
112+
n += 1;
113+
}
114+
ix += strideX;
115+
}
116+
} else {
117+
flg = true;
118+
}
119+
c = 0.0;
120+
for ( ; i < N; i++ ) {
87121
v = x[ ix ];
88122
if ( isnan( v ) === false ) {
89123
t = sum + v;
@@ -97,7 +131,7 @@ function dnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
97131
}
98132
ix += strideX;
99133
}
100-
out[ offsetOut ] = sum + c;
134+
out[ offsetOut ] = ( flg ) ? sum+c : sum;
101135
out[ offsetOut+strideOut ] = n;
102136
return out;
103137
}

lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn/src/main.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,56 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn_ndarray)( const CBLAS_INT N, const
6969
double v;
7070
double t;
7171
double c;
72+
int flg;
7273

73-
sum = 0.0;
7474
*n = 0;
7575
if ( N <= 0 ) {
76-
return sum;
76+
return 0.0;
7777
}
7878
ix = offsetX;
7979
if ( strideX == 0 ) {
8080
if ( stdlib_base_is_nan( X[ ix ] ) ) {
81-
return sum;
81+
return 0.0;
8282
}
8383
*n += N;
8484
return X[ ix ] * N;
8585
}
86-
c = 0.0;
86+
// Find the first non-NaN element...
8787
for ( i = 0; i < N; i++ ) {
88+
v = X[ ix ];
89+
if ( !stdlib_base_is_nan( v ) ) {
90+
break;
91+
}
92+
ix += strideX;
93+
}
94+
if ( i == N ) {
95+
return 0.0;
96+
}
97+
*n += 1;
98+
sum = v;
99+
ix += strideX;
100+
i += 1;
101+
flg = 0;
102+
103+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
104+
if ( sum == 0.0 ) {
105+
for ( ; i < N; i++ ) {
106+
v = X[ ix ];
107+
if ( !stdlib_base_is_nan( v ) ) {
108+
if ( v != 0.0 ) {
109+
flg = 1;
110+
break;
111+
}
112+
sum += v;
113+
*n += 1;
114+
}
115+
ix += strideX;
116+
}
117+
} else {
118+
flg = 1;
119+
}
120+
c = 0.0;
121+
for ( ; i < N; i++ ) {
88122
v = X[ ix ];
89123
if ( !stdlib_base_is_nan( v ) ) {
90124
t = sum + v;
@@ -98,5 +132,5 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn_ndarray)( const CBLAS_INT N, const
98132
}
99133
ix += strideX;
100134
}
101-
return sum + c;
135+
return ( flg == 1 ) ? sum+c : sum;
102136
}

lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn/test/test.ndarray.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var Float64Array = require( '@stdlib/array/float64' );
25+
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
2526
var dnannsumkbn = require( './../lib/ndarray.js' );
2627

2728

@@ -119,6 +120,24 @@ tape( 'the function calculates the sum of strided array elements (ignoring NaN v
119120
t.end();
120121
});
121122

123+
tape( 'the function preserves the sign of zero', function test( t ) {
124+
var expected;
125+
var out;
126+
var x;
127+
var v;
128+
129+
x = new Float64Array( [ -0.0, -0.0, -0.0, -0.0, -0.0 ] );
130+
131+
out = new Float64Array( 2 );
132+
v = dnannsumkbn( x.length, x, 1, 0, out, 1, 0 );
133+
134+
expected = new Float64Array( [ -0.0, 5.0 ] );
135+
t.strictEqual( v, out, 'returns expected value' );
136+
t.strictEqual( isSameFloat64Array( v, expected ), true, 'returns expected value' );
137+
138+
t.end();
139+
});
140+
122141
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
123142
var expected;
124143
var out;

lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn/test/test.ndarray.native.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var resolve = require( 'path' ).resolve;
2424
var tape = require( 'tape' );
2525
var Float64Array = require( '@stdlib/array/float64' );
26+
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
2627
var tryRequire = require( '@stdlib/utils/try-require' );
2728

2829

@@ -128,6 +129,24 @@ tape( 'the function calculates the sum of strided array elements (ignoring NaN v
128129
t.end();
129130
});
130131

132+
tape( 'the function preserves the sign of zero', opts, function test( t ) {
133+
var expected;
134+
var out;
135+
var x;
136+
var v;
137+
138+
x = new Float64Array( [ -0.0, -0.0, -0.0, -0.0, -0.0 ] );
139+
140+
out = new Float64Array( 2 );
141+
v = dnannsumkbn( x.length, x, 1, 0, out, 1, 0 );
142+
143+
expected = new Float64Array( [ -0.0, 5.0 ] );
144+
t.strictEqual( v, out, 'returns expected value' );
145+
t.strictEqual( isSameFloat64Array( v, expected ), true, 'returns expected value' );
146+
147+
t.end();
148+
});
149+
131150
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', opts, function test( t ) {
132151
var expected;
133152
var out;

0 commit comments

Comments
 (0)