Skip to content

Commit 79f62f0

Browse files
sagar7162kgryte
andauthored
feat: add number/float16/base/mul
PR-URL: #9373 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 7be3235 commit 79f62f0

File tree

10 files changed

+740
-0
lines changed

10 files changed

+740
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# mul
22+
23+
> Multiply two half-precision floating-point numbers.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var mul = require( '@stdlib/number/float16/base/mul' );
41+
```
42+
43+
#### mul( x, y )
44+
45+
Multiplies two half-precision floating-point numbers.
46+
47+
```javascript
48+
var v = mul( -1.0, 5.0 );
49+
// returns -5.0
50+
51+
v = mul( 2.0, 5.0 );
52+
// returns 10.0
53+
54+
v = mul( 0.0, 5.0 );
55+
// returns 0.0
56+
57+
v = mul( -0.0, 0.0 );
58+
// returns -0.0
59+
60+
v = mul( NaN, NaN );
61+
// returns NaN
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
86+
var logEachMap = require( '@stdlib/console/log-each-map' );
87+
var mul = require( '@stdlib/number/float16/base/mul' );
88+
89+
var x = discreteUniform( 100, -50, 50 );
90+
var y = discreteUniform( 100, -50, 50 );
91+
92+
logEachMap( '%d x %d = %d', x, y, mul );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
</section>
112+
113+
<!-- /.links -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
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+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
x = discreteUniform( 100, -100, 100 );
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
y = mul( x[ i%x.length ], 5.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::inline', pkg ), function benchmark( b ) {
56+
var x;
57+
var y;
58+
var i;
59+
60+
x = discreteUniform( 100, -100, 100 );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
y = x[ i%x.length ] * 5.0;
65+
if ( isnan( y ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( y ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x, y )
3+
Multiplies two half-precision floating-point numbers `x` and `y`.
4+
5+
Parameters
6+
----------
7+
x: number
8+
First input value.
9+
10+
y: number
11+
Second input value.
12+
13+
Returns
14+
-------
15+
z: number
16+
Result.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( -1.0, 5.0 )
21+
-5.0
22+
> v = {{alias}}( 2.0, 5.0 )
23+
10.0
24+
> v = {{alias}}( 0.0, 5.0 )
25+
0.0
26+
> v = {{alias}}( -0.0, 0.0 )
27+
-0.0
28+
> v = {{alias}}( NaN, NaN )
29+
NaN
30+
31+
See Also
32+
--------
33+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Multiplies two half-precision floating-point numbers `x` and `y`.
23+
*
24+
* @param x - first input value
25+
* @param y - second input value
26+
* @returns result
27+
*
28+
* @example
29+
* var v = mul( -1.0, 5.0 );
30+
* // returns -5.0
31+
*
32+
* @example
33+
* var v = mul( 2.0, 5.0 );
34+
* // returns 10.0
35+
*
36+
* @example
37+
* var v = mul( 0.0, 5.0 );
38+
* // returns 0.0
39+
*
40+
* @example
41+
* var v = mul( -0.0, 0.0 );
42+
* // returns -0.0
43+
*
44+
* @example
45+
* var v = mul( NaN, NaN );
46+
* // returns NaN
47+
*/
48+
declare function mul( x: number, y: number ): number;
49+
50+
51+
// EXPORTS //
52+
53+
export = mul;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import mul = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
mul( 8.0, 8.0 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not a number...
30+
{
31+
mul( true, 5.0 ); // $ExpectError
32+
mul( false, 5.0 ); // $ExpectError
33+
mul( null, 5.0 ); // $ExpectError
34+
mul( undefined, 5.0 ); // $ExpectError
35+
mul( '5', 5.0 ); // $ExpectError
36+
mul( [], 5.0 ); // $ExpectError
37+
mul( {}, 5.0 ); // $ExpectError
38+
mul( ( x: number ): number => x, 5.0 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument which is not a number...
42+
{
43+
mul( 5.0, true ); // $ExpectError
44+
mul( 5.0, false ); // $ExpectError
45+
mul( 5.0, null ); // $ExpectError
46+
mul( 5.0, undefined ); // $ExpectError
47+
mul( 5.0, '5' ); // $ExpectError
48+
mul( 5.0, [] ); // $ExpectError
49+
mul( 5.0, {} ); // $ExpectError
50+
mul( 5.0, ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
mul(); // $ExpectError
56+
mul( 5.0 ); // $ExpectError
57+
mul( 5.0, 5.0, 5.0 ); // $ExpectError
58+
}
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) 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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var logEachMap = require( '@stdlib/console/log-each-map' );
23+
var mul = require( './../lib' );
24+
25+
var x = discreteUniform( 100, -50, 50 );
26+
var y = discreteUniform( 100, -50, 50 );
27+
28+
logEachMap( '%d x %d = %d', x, y, mul );

0 commit comments

Comments
 (0)