Skip to content

Commit 568285f

Browse files
committed
feat: add number/int16/base/mul
--- 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: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent baaed47 commit 568285f

13 files changed

Lines changed: 911 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
24+
var minstd = require( '@stdlib/random/base/minstd' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var mul = require( './../lib' );
29+
var polyfill = require( './../lib/polyfill.js' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var x;
36+
var y;
37+
var i;
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
x = minstd();
42+
y = mul( x|0, x|0 );
43+
if ( isnan( y ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
}
47+
b.toc();
48+
if ( isnan( y ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( format( '%s::polyfill', pkg ), function benchmark( b ) {
56+
var x;
57+
var y;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
x = minstd();
63+
y = polyfill( x|0, x|0 );
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
}
68+
b.toc();
69+
if ( isnan( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
});
75+
76+
bench( format( '%s::naive_multiplication', pkg ), function benchmark( b ) {
77+
var x;
78+
var y;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
x = minstd();
84+
y = ( x|0 ) * ( x|0 );
85+
if ( isnan( y ) ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
}
89+
b.toc();
90+
if ( isnan( y ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( a, b )
3+
Performs C-like multiplication of two signed 16-bit integers.
4+
5+
Parameters
6+
----------
7+
a: integer
8+
Signed 16-bit integer.
9+
10+
b: integer
11+
Signed 16-bit integer.
12+
13+
Returns
14+
-------
15+
out: integer
16+
Product.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( -10|0, 4|0 )
21+
-40
22+
23+
See Also
24+
--------
25+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Performs C-like multiplication of two signed 16-bit integers.
23+
*
24+
* @param a - signed 16-bit integer
25+
* @param b - signed 16-bit integer
26+
* @returns product
27+
*
28+
* @example
29+
* var v = mul( -10|0, 4|0 );
30+
* // returns -40
31+
*/
32+
declare function mul( a: number, b: number ): number;
33+
34+
35+
// EXPORTS //
36+
37+
export = mul;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
import mul = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
mul( -10, 4 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided values other than two numbers...
30+
{
31+
mul( true, 3 ); // $ExpectError
32+
mul( false, 2 ); // $ExpectError
33+
mul( '5', 1 ); // $ExpectError
34+
mul( [], 1 ); // $ExpectError
35+
mul( {}, 2 ); // $ExpectError
36+
mul( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
mul( 9, true ); // $ExpectError
39+
mul( 9, false ); // $ExpectError
40+
mul( 5, '5' ); // $ExpectError
41+
mul( 8, [] ); // $ExpectError
42+
mul( 9, {} ); // $ExpectError
43+
mul( 8, ( x: number ): number => x ); // $ExpectError
44+
mul( [], true ); // $ExpectError
45+
mul( {}, false ); // $ExpectError
46+
mul( false, '5' ); // $ExpectError
47+
mul( {}, [] ); // $ExpectError
48+
mul( '5', ( x: number ): number => x ); // $ExpectError
49+
}
50+
51+
// The compiler throws an error if the function is provided insufficient arguments...
52+
{
53+
mul(); // $ExpectError
54+
mul( 3 ); // $ExpectError
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22+
var INT16_MIN = require( '@stdlib/constants/int16/min' );
23+
var INT16_MAX = require( '@stdlib/constants/int16/max' );
24+
var mul = require( './../lib' );
25+
26+
var randi;
27+
var a;
28+
var b;
29+
var y;
30+
var i;
31+
32+
randi = discreteUniform( INT16_MIN, INT16_MAX );
33+
34+
for ( i = 0; i < 100; i++ ) {
35+
a = randi()|0;
36+
b = randi()|0;
37+
y = mul( a, b );
38+
console.log( '%d x %d = %d', a, b, y );
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/**
22+
* Perform C-like multiplication of two signed 16-bit integers.
23+
*
24+
* @module @stdlib/number/int16/base/mul
25+
*
26+
* @example
27+
* var mul = require( '@stdlib/number/int16/base/mul' );
28+
*
29+
* var v = mul( -10|0, 4|0 );
30+
* // returns -40
31+
*/
32+
33+
// MODULES //
34+
35+
var builtin = require( './main.js' );
36+
var polyfill = require( './polyfill.js' );
37+
38+
39+
// MAIN //
40+
41+
var main;
42+
if ( typeof builtin === 'function' ) {
43+
main = builtin;
44+
} else {
45+
main = polyfill;
46+
}
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = main;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// MAIN //
22+
23+
var mul = ( typeof Math.mul === 'function' ) ? Math.mul : null; // eslint-disable-line stdlib/no-builtin-math
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = mul;

0 commit comments

Comments
 (0)