Skip to content

Commit e80fff8

Browse files
gururaj1512kgryte
andauthored
feat: add number/float16/base/to-binary-string
PR-URL: #8575 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 6881387 commit e80fff8

File tree

18 files changed

+1220
-0
lines changed

18 files changed

+1220
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
# toBinaryString
22+
23+
> Return a string giving the literal bit representation of a [half-precision floating-point number][ieee754].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' );
31+
```
32+
33+
#### toBinaryString( x )
34+
35+
Returns a string giving the literal bit representation of a [half-precision floating-point number][ieee754].
36+
37+
```javascript
38+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var str = toBinaryString( toFloat16( 4.0 ) );
41+
// returns '0100010000000000'
42+
43+
str = toBinaryString( toFloat16( 3.1415926 ) );
44+
// returns '0100001001001000'
45+
46+
str = toBinaryString( toFloat16( -1.0e3 ) );
47+
// returns '1110001111010000'
48+
```
49+
50+
The function handles [subnormals][subnormals].
51+
52+
```javascript
53+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
54+
55+
var str = toBinaryString( toFloat16( -3.14e-6 ) );
56+
// returns '1000000000110101'
57+
58+
str = toBinaryString( toFloat16( 1.4e-7 ) );
59+
// returns '0000000000000010'
60+
```
61+
62+
The function handles special values.
63+
64+
```javascript
65+
var PINF = require( '@stdlib/constants/float16/pinf' );
66+
var NINF = require( '@stdlib/constants/float16/ninf' );
67+
68+
var str = toBinaryString( 0.0 );
69+
// returns '0000000000000000'
70+
71+
str = toBinaryString( -0.0 );
72+
// returns '1000000000000000'
73+
74+
str = toBinaryString( NaN );
75+
// returns '0111111000000000'
76+
77+
str = toBinaryString( PINF );
78+
// returns '0111110000000000'
79+
80+
str = toBinaryString( NINF );
81+
// returns '1111110000000000'
82+
```
83+
84+
</section>
85+
86+
<!-- /.usage -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var randu = require( '@stdlib/random/base/randu' );
96+
var round = require( '@stdlib/math/base/special/round' );
97+
var pow = require( '@stdlib/math/base/special/pow' );
98+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
99+
var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' );
100+
101+
var frac;
102+
var sign;
103+
var exp;
104+
var b;
105+
var x;
106+
var i;
107+
108+
// Convert random numbers to literal bit representations...
109+
for ( i = 0; i < 100; i++ ) {
110+
if ( randu() < 0.5 ) {
111+
sign = -1.0;
112+
} else {
113+
sign = 1.0;
114+
}
115+
frac = randu() * 10.0;
116+
exp = round( randu()*10.0 );
117+
if ( randu() < 0.5 ) {
118+
exp = -exp;
119+
}
120+
x = sign * frac * pow( 2.0, exp );
121+
x = float64ToFloat16( x );
122+
b = toBinaryString( x );
123+
console.log( b );
124+
}
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-2008
144+
145+
[subnormals]: https://en.wikipedia.org/wiki/Denormal_number
146+
147+
<!-- <related-links> -->
148+
149+
<!-- </related-links> -->
150+
151+
</section>
152+
153+
<!-- /.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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
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 toBinaryString = 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 = toBinaryString( x[ i%x.length ] );
45+
if ( typeof y !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( y ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( x )
3+
Returns a string giving the literal bit representation of a half-precision
4+
floating-point number.
5+
6+
Parameters
7+
----------
8+
x: float
9+
Half-precision floating-point number.
10+
11+
Returns
12+
-------
13+
str: string
14+
Bit representation.
15+
16+
Examples
17+
--------
18+
> var str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 4.0 ) )
19+
'0100010000000000'
20+
> str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.1415926 ) )
21+
'0100001001001000'
22+
> str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -1.0e3 ) )
23+
'1110001111010000'
24+
> str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14e-6 ) )
25+
'1000000000110101'
26+
> str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 1.4e-7 ) )
27+
'0000000000000010'
28+
> str = {{alias}}( 0.0 )
29+
'0000000000000000'
30+
> str = {{alias}}( -0.0 )
31+
'1000000000000000'
32+
> str = {{alias}}( NaN )
33+
'0111111000000000'
34+
> str = {{alias}}( {{alias:@stdlib/constants/float16/pinf}} )
35+
'0111110000000000'
36+
> str = {{alias}}( {{alias:@stdlib/constants/float16/ninf}} )
37+
'1111110000000000'
38+
39+
See Also
40+
--------
41+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 a string giving the literal bit representation of a half-precision floating-point number.
23+
*
24+
* @param x - input value
25+
* @returns bit representation
26+
*
27+
* @example
28+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
29+
*
30+
* var str = toBinaryString( toFloat16( 4.0 ) );
31+
* // returns '0100010000000000'
32+
*
33+
* @example
34+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
35+
*
36+
* var str = toBinaryString( toFloat16( 3.1415926 ) );
37+
* // returns '0100001001001000'
38+
*
39+
* @example
40+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
41+
*
42+
* var str = toBinaryString( toFloat16( -1.0e3 ) );
43+
* // returns '1110001111010000'
44+
*
45+
* @example
46+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
47+
*
48+
* var str = toBinaryString( toFloat16( -3.14e-6 ) );
49+
* // returns '1000000000110101'
50+
*
51+
* @example
52+
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
53+
*
54+
* var str = toBinaryString( toFloat16( 1.4e-7 ) );
55+
* // returns '0000000000000010'
56+
*
57+
* @example
58+
* var str = toBinaryString( 0.0 );
59+
* // returns '0000000000000000'
60+
*
61+
* @example
62+
* var str = toBinaryString( -0.0 );
63+
* // returns '1000000000000000'
64+
*
65+
* @example
66+
* var str = toBinaryString( NaN );
67+
* // returns '0111111000000000'
68+
*
69+
* @example
70+
* var PINF = require( '@stdlib/constants/float16/pinf' );
71+
*
72+
* var str = toBinaryString( PINF );
73+
* // returns '0111110000000000'
74+
*
75+
* @example
76+
* var NINF = require( '@stdlib/constants/float16/ninf' );
77+
*
78+
* var str = toBinaryString( NINF );
79+
* // returns '1111110000000000'
80+
*/
81+
declare function toBinaryString( x: number ): string;
82+
83+
84+
// EXPORTS //
85+
86+
export = toBinaryString;
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 toBinaryString = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
toBinaryString( 0.0 ); // $ExpectType string
27+
toBinaryString( -0.0 ); // $ExpectType string
28+
}
29+
30+
// The compiler throws an error if the function is provided a value other than a number...
31+
{
32+
toBinaryString( true ); // $ExpectError
33+
toBinaryString( false ); // $ExpectError
34+
toBinaryString( 'abc' ); // $ExpectError
35+
toBinaryString( [] ); // $ExpectError
36+
toBinaryString( {} ); // $ExpectError
37+
toBinaryString( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided insufficient arguments...
41+
{
42+
toBinaryString(); // $ExpectError
43+
}

0 commit comments

Comments
 (0)