Skip to content

Commit 4f41dcf

Browse files
committed
feat: add number/float16/base/from-word
--- 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: na - 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 387d027 commit 4f41dcf

File tree

21 files changed

+918
-0
lines changed

21 files changed

+918
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
# fromWord
22+
23+
> Create a [half-precision floating-point number][ieee754] from an unsigned integer corresponding to an [IEEE 754][ieee754] binary representation.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var fromWord = require( '@stdlib/number/float16/base/from-word' );
31+
```
32+
33+
#### fromWord( word )
34+
35+
Creates a [half-precision floating-point number][ieee754] from an unsigned `integer` corresponding to an [IEEE 754][ieee754] binary representation.
36+
37+
```javascript
38+
var word = 15411; // => 0 01111 0000110011
39+
40+
var f16 = fromWord( word ); // when printed, implicitly promoted to float64
41+
// returns 1.0498046875
42+
```
43+
44+
</section>
45+
46+
<!-- /.usage -->
47+
48+
<section class="notes">
49+
50+
</section>
51+
52+
<!-- /.notes -->
53+
54+
<section class="examples">
55+
56+
## Examples
57+
58+
<!-- eslint no-undef: "error" -->
59+
60+
```javascript
61+
var uniform = require( '@stdlib/random/array/uniform' );
62+
var round = require( '@stdlib/math/base/special/round' );
63+
var map = require( '@stdlib/array/base/map' );
64+
var naryFunction = require( '@stdlib/utils/nary-function' );
65+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
66+
var logEachMap = require( '@stdlib/console/log-each-map' );
67+
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
68+
var fromWord = require( '@stdlib/number/float16/base/from-word' );
69+
70+
// Generate an array of random numbers:
71+
var arr = uniform( 1000, 0.0, MAX_UINT16 );
72+
73+
// Round each number:
74+
var word = map( arr, naryFunction( round, 1 ) );
75+
76+
// Create half-precision floating-point numbers from unsigned integers...
77+
logEachMap( 'word: %d => float16: %f', word, pickArguments( fromWord, [ 0 ] ) );
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
85+
86+
<section class="related">
87+
88+
</section>
89+
90+
<!-- /.related -->
91+
92+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
93+
94+
<section class="links">
95+
96+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
97+
98+
<!-- <related-links> -->
99+
100+
<!-- </related-links> -->
101+
102+
</section>
103+
104+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var round = require( '@stdlib/math/base/special/round' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var map = require( '@stdlib/array/base/map' );
28+
var naryFunction = require( '@stdlib/utils/nary-function' );
29+
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
30+
var pkg = require( './../package.json' ).name;
31+
var fromWord = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg, function benchmark( b ) {
37+
var word;
38+
var x;
39+
var y;
40+
var i;
41+
42+
x = uniform( 100, 0.0, MAX_UINT16 );
43+
word = map( x, naryFunction( round, 1 ) );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = fromWord( word );
48+
if ( isnan( y ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
* Creates a half-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.
23+
*
24+
* @param word - unsigned integer
25+
* @returns half-precision floating-point number
26+
*
27+
* @example
28+
* var word = 15411; // => 0 01111 0000110011
29+
*
30+
* var f16 = fromWord( word ); // when printed, implicitly promoted to float64
31+
* // returns 1.0498046875
32+
*/
33+
declare function fromWord( word: number ): number;
34+
35+
36+
// EXPORTS //
37+
38+
export = fromWord;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 fromWord = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
fromWord( 15411 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
fromWord( true ); // $ExpectError
32+
fromWord( false ); // $ExpectError
33+
fromWord( 'abc' ); // $ExpectError
34+
fromWord( [] ); // $ExpectError
35+
fromWord( {} ); // $ExpectError
36+
fromWord( ( x: number ): number => x ); // $ExpectError
37+
}
38+
39+
// The compiler throws an error if the function is provided insufficient arguments...
40+
{
41+
fromWord(); // $ExpectError
42+
}
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) 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+
var uniform = require( '@stdlib/random/array/uniform' );
22+
var round = require( '@stdlib/math/base/special/round' );
23+
var map = require( '@stdlib/array/base/map' );
24+
var naryFunction = require( '@stdlib/utils/nary-function' );
25+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
26+
var logEachMap = require( '@stdlib/console/log-each-map' );
27+
var MAX_UINT16 = require( '@stdlib/constants/uint16/max' );
28+
var fromWord = require( './../lib' );
29+
30+
// Generate an array of random numbers:
31+
var arr = uniform( 1000, 0.0, MAX_UINT16 );
32+
33+
// Round each number:
34+
var word = map( arr, naryFunction( round, 1 ) );
35+
36+
// Create half-precision floating-point numbers from unsigned integers...
37+
logEachMap( 'word: %d => float16: %f', word, pickArguments( fromWord, [ 0 ] ) );
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
/**
22+
* Create a half-precision floating-point number from an unsigned integer corresponding to an IEEE 754 binary representation.
23+
*
24+
* @module @stdlib/number/float16/base/from-word
25+
*
26+
* @example
27+
* var fromWord = require( '@stdlib/number/float16/base/from-word' );
28+
*
29+
* var word = 15411; // => 0 01111 0000110011
30+
*
31+
* var f16 = fromWord( word ); // when printed, implicitly promoted to float64
32+
* // returns 1.0498046875
33+
*/
34+
35+
// MODULES //
36+
37+
var main = require( './main.js' );
38+
39+
40+
// EXPORTS //
41+
42+
module.exports = main;

0 commit comments

Comments
 (0)