Skip to content

Commit 0ec8264

Browse files
committed
feat: add the JS native files
1 parent d4bb8aa commit 0ec8264

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var stdev = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( stdev instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var len;
44+
var mu;
45+
var y;
46+
var i;
47+
48+
len = 100;
49+
mu = new Float64Array( len );
50+
for ( i = 0; i < len; i++ ) {
51+
mu[ i ] = ( randu() * 40.0 ) - 20.0;
52+
}
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = stdev( mu[ i % len ] );
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( y ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var addon = require( './../src/addon.node' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the standard deviation of a degenerate distribution centered at `mu`.
30+
*
31+
* @private
32+
* @param {number} mu - constant value of the distribution
33+
* @returns {number} standard deviation
34+
*
35+
* @example
36+
* var y = stdev( 2.0 );
37+
* // returns 0.0
38+
*
39+
* @example
40+
* var y = stdev( NaN );
41+
* // returns NaN
42+
*/
43+
function stdev( mu ) {
44+
return addon( mu );
45+
}
46+
47+
48+
// EXPORTS //
49+
50+
module.exports = stdev;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var tape = require( 'tape' );
25+
var tryRequire = require( '@stdlib/utils/try-require' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
28+
29+
// VARIABLES //
30+
31+
var stdev = tryRequire( resolve( __dirname, './../lib/native.js' ) );
32+
var opts = {
33+
'skip': ( stdev instanceof Error )
34+
};
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', opts, function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof stdev, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'if provided `NaN` for `mu`, the function returns `NaN`', opts, function test( t ) {
46+
var v = stdev( NaN );
47+
t.equal( isnan( v ), true, 'returns NaN' );
48+
t.end();
49+
});
50+
51+
tape( 'the function returns the standard deviation of a degenerate distribution', opts, function test( t ) {
52+
var v = stdev( 2.0 );
53+
t.equal( v, 0.0, 'returns 0.0' );
54+
55+
v = stdev( -5.0 );
56+
t.equal( v, 0.0, 'returns 0.0' );
57+
58+
v = stdev( 0.5 );
59+
t.equal( v, 0.0, 'returns 0.0' );
60+
t.end();
61+
});

0 commit comments

Comments
 (0)