Skip to content

Commit 3df19a2

Browse files
gururaj1512kgrytestdlib-bot
authored andcommitted
feat: add number/float16/base/to-word
PR-URL: #8371 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent d7f74ee commit 3df19a2

File tree

22 files changed

+1064
-0
lines changed

22 files changed

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

0 commit comments

Comments
 (0)