Skip to content

Commit fcd9463

Browse files
Erennn7kgrytegururaj1512
authored
feat: add number/float16/base/from-binary-string
PR-URL: #9322 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Gururaj Gurram <gururajgurram1512@gmail.com> Reviewed-by: Gururaj Gurram <gururajgurram1512@gmail.com>
1 parent 154665b commit fcd9463

File tree

17 files changed

+967
-0
lines changed

17 files changed

+967
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
# fromBinaryString
22+
23+
> Create a [half-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float16/base/to-binary-string].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var fromBinaryString = require( '@stdlib/number/float16/base/from-binary-string' );
31+
```
32+
33+
#### fromBinaryString( bstr )
34+
35+
Creates a [half-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float16/base/to-binary-string].
36+
37+
```javascript
38+
var bstr = '0100010000000000';
39+
var v = fromBinaryString( bstr );
40+
// returns 4.0
41+
42+
bstr = '0100001001001000';
43+
v = fromBinaryString( bstr );
44+
// returns ~3.140625
45+
46+
bstr = '1110001111010000';
47+
v = fromBinaryString( bstr );
48+
// returns -1000.0
49+
```
50+
51+
The function handles [subnormals][subnormals].
52+
53+
```javascript
54+
var bstr = '1000000000000001';
55+
var val = fromBinaryString( bstr );
56+
// returns ~-5.96e-8
57+
58+
bstr = '0000000000000001';
59+
val = fromBinaryString( bstr );
60+
// returns ~5.96e-8
61+
```
62+
63+
The function handles special values.
64+
65+
```javascript
66+
var bstr = '0000000000000000';
67+
var val = fromBinaryString( bstr );
68+
// returns 0.0
69+
70+
bstr = '1000000000000000';
71+
val = fromBinaryString( bstr );
72+
// returns -0.0
73+
74+
bstr = '0111111000000000';
75+
val = fromBinaryString( bstr );
76+
// returns NaN
77+
78+
bstr = '0111110000000000';
79+
val = fromBinaryString( bstr );
80+
// returns Infinity
81+
82+
bstr = '1111110000000000';
83+
val = fromBinaryString( bstr );
84+
// returns -Infinity
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var randu = require( '@stdlib/random/base/randu' );
99+
var round = require( '@stdlib/math/base/special/round' );
100+
var pow = require( '@stdlib/math/base/special/pow' );
101+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
102+
var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' );
103+
var fromBinaryString = require( '@stdlib/number/float16/base/from-binary-string' );
104+
105+
var frac;
106+
var sign;
107+
var exp;
108+
var b;
109+
var x;
110+
var y;
111+
var i;
112+
113+
// Convert random numbers to IEEE 754 literal bit representations and then convert them back...
114+
for ( i = 0; i < 100; i++ ) {
115+
if ( randu() < 0.5 ) {
116+
sign = -1.0;
117+
} else {
118+
sign = 1.0;
119+
}
120+
frac = randu() * 10.0;
121+
exp = round( randu()*5.0 );
122+
if ( randu() < 0.5 ) {
123+
exp = -exp;
124+
}
125+
x = sign * frac * pow( 2.0, exp );
126+
x = toFloat16( x );
127+
128+
b = toBinaryString( x );
129+
y = fromBinaryString( b );
130+
131+
console.log( '%d => %s => %d', x, b, y );
132+
console.log( x === y );
133+
}
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
</section>
145+
146+
<!-- /.related -->
147+
148+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149+
150+
<section class="links">
151+
152+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
153+
154+
[subnormals]: https://en.wikipedia.org/wiki/Denormal_number
155+
156+
[@stdlib/number/float16/base/to-binary-string]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float16/base/to-binary-string
157+
158+
</section>
159+
160+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var fromBinaryString = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var r;
36+
var i;
37+
38+
r = bernoulli( 100, 0.5, {
39+
'dtype': 'generic'
40+
});
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
x = '101010101010101'+( (r[ i%r.length ] < 1 ) ? '0' : '1' );
45+
y = fromBinaryString( x );
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnan( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
{{alias}}( bstr )
3+
Creates a half-precision floating-point number from an IEEE 754 literal
4+
bit representation.
5+
6+
Parameters
7+
----------
8+
bstr: string
9+
Literal bit representation.
10+
11+
Returns
12+
-------
13+
out: float
14+
Half-precision floating-point number.
15+
16+
Examples
17+
--------
18+
> var bstr = '0100010000000000';
19+
> var val = {{alias}}( bstr )
20+
4.0
21+
> bstr = '0100001001001000';
22+
> val = {{alias}}( bstr )
23+
~3.140625
24+
> bstr = '1110001111010000';
25+
> val = {{alias}}( bstr )
26+
-1000.0
27+
28+
// The function handles subnormals:
29+
> bstr = '0000000000000001';
30+
> val = {{alias}}( bstr )
31+
~5.96e-8
32+
> bstr = '1000000000000001';
33+
> val = {{alias}}( bstr )
34+
~-5.96e-8
35+
36+
// The function handles special values:
37+
> bstr = '0000000000000000';
38+
> val = {{alias}}( bstr )
39+
0.0
40+
> bstr = '1000000000000000';
41+
> val = {{alias}}( bstr )
42+
-0.0
43+
> bstr = '0111110000000001';
44+
> val = {{alias}}( bstr )
45+
NaN
46+
> bstr = '0111110000000000';
47+
> val = {{alias}}( bstr )
48+
Infinity
49+
> bstr = '1111110000000000';
50+
> val = {{alias}}( bstr )
51+
-Infinity
52+
53+
See Also
54+
--------
55+
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Creates a half-precision floating-point number from an IEEE 754 literal bit representation.
23+
*
24+
* @param bstr - string which is a literal bit representation
25+
* @throws must provide a string with a length equal to `16`
26+
* @returns half-precision floating-point number
27+
*
28+
* @example
29+
* var bstr = '0100010000000000';
30+
* var v = fromBinaryString( bstr );
31+
* // returns 4.0
32+
*
33+
* @example
34+
* var bstr = '0100001001001000';
35+
* var v = fromBinaryString( bstr );
36+
* // returns ~3.140625
37+
*
38+
* @example
39+
* var bstr = '1110001111010000';
40+
* var v = fromBinaryString( bstr );
41+
* // returns -1000.0
42+
*
43+
* @example
44+
* var bstr = '0000000000000000';
45+
* var v = fromBinaryString( bstr );
46+
* // returns 0.0
47+
*
48+
* @example
49+
* var bstr = '1000000000000000';
50+
* var v = fromBinaryString( bstr );
51+
* // returns -0.0
52+
*/
53+
declare function fromBinaryString( bstr: string ): number;
54+
55+
56+
// EXPORTS //
57+
58+
export = fromBinaryString;
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+
import fromBinaryString = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
fromBinaryString( '0100010000000000' ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
fromBinaryString( true ); // $ExpectError
32+
fromBinaryString( false ); // $ExpectError
33+
fromBinaryString( null ); // $ExpectError
34+
fromBinaryString( undefined ); // $ExpectError
35+
fromBinaryString( 5 ); // $ExpectError
36+
fromBinaryString( [] ); // $ExpectError
37+
fromBinaryString( {} ); // $ExpectError
38+
fromBinaryString( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
fromBinaryString(); // $ExpectError
44+
}

0 commit comments

Comments
 (0)