Skip to content

Commit 08611d0

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 52b14da commit 08611d0

File tree

4 files changed

+117
-11
lines changed

4 files changed

+117
-11
lines changed

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ var abs = require( '@stdlib/math/base/special/abs' );
5858
function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
5959
var sum;
6060
var ccs;
61+
var flg;
6162
var cs;
6263
var cc;
6364
var ix;
@@ -68,28 +69,61 @@ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
6869
var n;
6970
var i;
7071

71-
sum = 0.0;
7272
io = offsetOut;
7373
if ( N <= 0 ) {
74-
out[ io ] = sum;
74+
out[ io ] = 0.0;
7575
out[ io+strideOut ] = 0;
7676
return out;
7777
}
7878
ix = offsetX;
7979
if ( strideX === 0 ) {
8080
if ( isnan( x[ ix ] ) ) {
81-
out[ io ] = sum;
81+
out[ io ] = 0.0;
8282
out[ io+strideOut ] = 0;
8383
return out;
8484
}
8585
out[ io ] = x[ ix ] * N;
8686
out[ io+strideOut ] = N;
8787
return out;
8888
}
89+
// Find the first non-NaN element...
90+
for ( i = 0; i < N; i++ ) {
91+
v = x[ ix ];
92+
if ( isnan( v ) === false ) {
93+
break;
94+
}
95+
ix += strideX;
96+
}
97+
if ( i === N ) {
98+
out[ io ] = 0.0;
99+
out[ io+strideOut ] = 0;
100+
return out;
101+
}
102+
n = 1;
103+
sum = v;
104+
ix += strideX;
105+
i += 1;
106+
107+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
108+
if ( sum === 0.0 ) {
109+
for ( ; i < N; i++ ) {
110+
v = x[ ix ];
111+
if ( isnan( v ) === false ) {
112+
if ( v !== 0.0 ) {
113+
flg = true;
114+
break;
115+
}
116+
sum += v;
117+
n += 1;
118+
}
119+
ix += strideX;
120+
}
121+
} else {
122+
flg = true;
123+
}
89124
ccs = 0.0; // second order correction term for lost low order bits
90125
cs = 0.0; // first order correction term for lost low order bits
91-
n = 0;
92-
for ( i = 0; i < N; i++ ) {
126+
for ( ; i < N; i++ ) {
93127
v = x[ ix ];
94128
if ( isnan( v ) === false ) {
95129
t = sum + v;
@@ -111,7 +145,7 @@ function dnannsumkbn2( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
111145
}
112146
ix += strideX;
113147
}
114-
out[ io ] = sum + cs + ccs;
148+
out[ io ] = ( flg ) ? sum+cs+ccs : sum;
115149
out[ io+strideOut ] = n;
116150
return out;
117151
}

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,57 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const
7272
double v;
7373
double t;
7474
double c;
75+
int flg;
7576

76-
sum = 0.0;
7777
*n = 0;
7878
if ( N <= 0 ) {
79-
return sum;
79+
return 0.0;
8080
}
8181
ix = offsetX;
8282
if ( strideX == 0 ) {
8383
if ( stdlib_base_is_nan( X[ ix ] ) ) {
84-
return sum;
84+
return 0.0;
8585
}
8686
*n += N;
8787
return X[ ix ] * N;
8888
}
89+
// Find the first non-NaN element...
90+
for ( i = 0; i < N; i++ ) {
91+
v = X[ ix ];
92+
if ( !stdlib_base_is_nan( v ) ) {
93+
break;
94+
}
95+
ix += strideX;
96+
}
97+
if ( i == N ) {
98+
return 0.0;
99+
}
100+
*n += 1;
101+
sum = v;
102+
ix += strideX;
103+
i += 1;
104+
flg = 0;
105+
106+
// In order to preserve the sign of zero which can be lost during compensated summation below, find the first non-zero element...
107+
if ( sum == 0.0 ) {
108+
for ( ; i < N; i++ ) {
109+
v = X[ ix ];
110+
if ( !stdlib_base_is_nan( v ) ) {
111+
if ( v != 0.0 ) {
112+
flg = 1;
113+
break;
114+
}
115+
sum += v;
116+
*n += 1;
117+
}
118+
ix += strideX;
119+
}
120+
} else {
121+
flg = 1;
122+
}
89123
ccs = 0.0; // second order correction term for lost lower order bits
90124
cs = 0.0; // first order correction term for lost low order bits
91-
for ( i = 0; i < N; i++ ) {
125+
for ( ; i < N; i++ ) {
92126
v = X[ ix ];
93127
if ( !stdlib_base_is_nan( v ) ) {
94128
t = sum + v;
@@ -110,5 +144,5 @@ double API_SUFFIX(stdlib_strided_dnannsumkbn2_ndarray)( const CBLAS_INT N, const
110144
}
111145
ix += strideX;
112146
}
113-
return sum + cs + ccs;
147+
return ( flg == 1 ) ? sum+cs+ccs : sum;
114148
}

lib/node_modules/@stdlib/blas/ext/base/dnannsumkbn2/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 dnannsumkbn2 = 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 = dnannsumkbn2( 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/dnannsumkbn2/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 = dnannsumkbn2( 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)