Skip to content

Commit b22ed83

Browse files
feat: add number/float16/base/add
--- 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: passed - 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 da01e3d commit b22ed83

File tree

10 files changed

+726
-0
lines changed

10 files changed

+726
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# add
22+
23+
> Add 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 add = require( '@stdlib/number/float16/base/add' );
41+
```
42+
43+
#### add( x, y )
44+
45+
Adds two half-precision floating-point numbers.
46+
47+
```javascript
48+
var v = add( -1.0, 5.0 );
49+
// returns 4.0
50+
51+
v = add( 2.0, 5.0 );
52+
// returns 7.0
53+
54+
v = add( 0.0, 5.0 );
55+
// returns 5.0
56+
57+
v = add( -0.0, 0.0 );
58+
// returns 0.0
59+
60+
v = add( 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 toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
86+
var uniform = require( '@stdlib/random/array/uniform' );
87+
var map = require( '@stdlib/array/base/map' );
88+
var logEachMap = require( '@stdlib/console/log-each-map' );
89+
var add = require( '@stdlib/number/float16/base/add' );
90+
91+
var x = map( uniform( 100, -50.0, 50.0 ), toFloat16 );
92+
var y = map( uniform( 100, -50.0, 50.0 ), toFloat16 );
93+
94+
logEachMap( 'x: %f, y: %f => %f', x, y, add );
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
102+
103+
<section class="related">
104+
105+
</section>
106+
107+
<!-- /.related -->
108+
109+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
110+
111+
<section class="links">
112+
113+
</section>
114+
115+
<!-- /.links -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var map = require( '@stdlib/array/base/map' );
26+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var naryFunction = require( '@stdlib/utils/nary-function' );
29+
var pkg = require( './../package.json' ).name;
30+
var add = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var out;
37+
var x;
38+
var y;
39+
var i;
40+
41+
x = map( uniform( 100, -100, 100 ), naryFunction( toFloat16, 1 ) );
42+
y = map( uniform( 100, -100, 100 ), naryFunction( toFloat16, 1 ) );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = add( x[ i%x.length ], y[ i%y.length ]);
47+
if ( isnan( out ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnan( out ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( x, y )
3+
Adds 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+
out: number
16+
Result.
17+
18+
Examples
19+
--------
20+
> var v = {{alias}}( -1.0, 5.0 )
21+
4.0
22+
> v = {{alias}}( 2.0, 5.0 )
23+
7.0
24+
> v = {{alias}}( 0.0, 5.0 )
25+
5.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) 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+
* Adds 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 = add( -1.0, 5.0 );
30+
* // returns 4.0
31+
*
32+
* @example
33+
* var v = add( 2.0, 5.0 );
34+
* // returns 7.0
35+
*
36+
* @example
37+
* var v = add( 0.0, 5.0 );
38+
* // returns 5.0
39+
*
40+
* @example
41+
* var v = add( -0.0, 0.0 );
42+
* // returns 0.0
43+
*
44+
* @example
45+
* var v = add( NaN, NaN );
46+
* // returns NaN
47+
*/
48+
declare function add( x: number, y: number ): number;
49+
50+
51+
// EXPORTS //
52+
53+
export = add;
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) 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 add = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
add( 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+
add( true, 5.0 ); // $ExpectError
32+
add( false, 5.0 ); // $ExpectError
33+
add( null, 5.0 ); // $ExpectError
34+
add( undefined, 5.0 ); // $ExpectError
35+
add( '5', 5.0 ); // $ExpectError
36+
add( [], 5.0 ); // $ExpectError
37+
add( {}, 5.0 ); // $ExpectError
38+
add( ( 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+
add( 5.0, true ); // $ExpectError
44+
add( 5.0, false ); // $ExpectError
45+
add( 5.0, null ); // $ExpectError
46+
add( 5.0, undefined ); // $ExpectError
47+
add( 5.0, '5' ); // $ExpectError
48+
add( 5.0, [] ); // $ExpectError
49+
add( 5.0, {} ); // $ExpectError
50+
add( 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+
add(); // $ExpectError
56+
add( 5.0 ); // $ExpectError
57+
add( 5.0, 5.0, 5.0 ); // $ExpectError
58+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 uniform = require( '@stdlib/random/array/uniform' );
22+
var map = require( '@stdlib/array/base/map' );
23+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
24+
var logEachMap = require( '@stdlib/console/log-each-map' );
25+
var add = require( './../lib' );
26+
27+
var x = map( uniform( 100, -50.0, 50.0 ), toFloat16 );
28+
var y = map( uniform( 100, -50.0, 50.0 ), toFloat16 );
29+
30+
logEachMap( 'x: %f, y: %f => %f', x, y, add );

0 commit comments

Comments
 (0)