Skip to content

Commit 7797875

Browse files
committed
feat: add number/float16/base/from-binary-string
- Add function to create half-precision floating-point numbers from IEEE 754 literal bit representations - Implement main conversion logic with proper handling of sign, exponent, and fraction bits - Add helper function to convert fraction bit sequences to numeric values - Include comprehensive test suite covering special values, normal numbers, subnormals, and round-trip conversions - Add TypeScript definitions and type tests - Include documentation with usage examples - Add benchmark and examples files - Follows same structure and logic as float32/from-binary-string --- 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 d11ab68 commit 7797875

File tree

10 files changed

+874
-0
lines changed

10 files changed

+874
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
# From Binary String
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 toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var bstr = '0100010000000000';
41+
var v = fromBinaryString( bstr );
42+
// returns 4.0
43+
44+
bstr = '0100001001001000';
45+
v = fromBinaryString( bstr );
46+
// returns ~3.140625
47+
48+
bstr = '1110001111010000';
49+
v = fromBinaryString( bstr );
50+
// returns -1000.0
51+
```
52+
53+
The function handles [subnormals][subnormals].
54+
55+
```javascript
56+
var bstr = '1000000000000001';
57+
var val = fromBinaryString( bstr );
58+
// returns ~-5.96e-8
59+
60+
bstr = '0000000000000001';
61+
val = fromBinaryString( bstr );
62+
// returns ~5.96e-8
63+
```
64+
65+
The function handles special values.
66+
67+
```javascript
68+
var bstr = '0000000000000000';
69+
var val = fromBinaryString( bstr );
70+
// returns 0.0
71+
72+
bstr = '1000000000000000';
73+
val = fromBinaryString( bstr );
74+
// returns -0.0
75+
76+
bstr = '0111111000000000';
77+
val = fromBinaryString( bstr );
78+
// returns NaN
79+
80+
bstr = '0111110000000000';
81+
val = fromBinaryString( bstr );
82+
// returns Infinity
83+
84+
bstr = '1111110000000000';
85+
val = fromBinaryString( bstr );
86+
// returns -Infinity
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var randu = require( '@stdlib/random/base/randu' );
101+
var round = require( '@stdlib/math/base/special/round' );
102+
var pow = require( '@stdlib/math/base/special/pow' );
103+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
104+
var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' );
105+
var fromBinaryString = require( '@stdlib/number/float16/base/from-binary-string' );
106+
107+
var frac;
108+
var sign;
109+
var exp;
110+
var b;
111+
var x;
112+
var y;
113+
var i;
114+
115+
// Convert random numbers to IEEE 754 literal bit representations and then convert them back...
116+
for ( i = 0; i < 100; i++ ) {
117+
if ( randu() < 0.5 ) {
118+
sign = -1.0;
119+
} else {
120+
sign = 1.0;
121+
}
122+
frac = randu() * 10.0;
123+
exp = round( randu()*5.0 );
124+
if ( randu() < 0.5 ) {
125+
exp = -exp;
126+
}
127+
x = sign * frac * pow( 2.0, exp );
128+
x = toFloat16( x );
129+
130+
b = toBinaryString( x );
131+
y = fromBinaryString( b );
132+
133+
console.log( '%d => %s => %d', x, b, y );
134+
console.log( x === y );
135+
}
136+
```
137+
138+
</section>
139+
140+
<!-- /.examples -->
141+
142+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
143+
144+
<section class="related">
145+
146+
* * *
147+
148+
## See Also
149+
150+
- <span class="package-name">[`@stdlib/number/float16/base/to-binary-string`][@stdlib/number/float16/base/to-binary-string]</span><span class="delimiter">: </span><span class="description">return a string giving the literal bit representation of a half-precision floating-point number.</span>
151+
- <span class="package-name">[`@stdlib/number/float32/base/from-binary-string`][@stdlib/number/float32/base/from-binary-string]</span><span class="delimiter">: </span><span class="description">create a single-precision floating-point number from a literal bit representation.</span>
152+
- <span class="package-name">[`@stdlib/number/float64/base/from-binary-string`][@stdlib/number/float64/base/from-binary-string]</span><span class="delimiter">: </span><span class="description">create a double-precision floating-point number from a literal bit representation.</span>
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
163+
164+
[subnormals]: https://en.wikipedia.org/wiki/Denormal_number
165+
166+
[@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
167+
168+
<!-- <related-links> -->
169+
170+
[@stdlib/number/float32/base/from-binary-string]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float32/base/from-binary-string
171+
172+
[@stdlib/number/float64/base/from-binary-string]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/number/float64/base/from-binary-string
173+
174+
<!-- </related-links> -->
175+
176+
</section>
177+
178+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 randu = require( '@stdlib/random/base/randu' );
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 i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = '101010101010101'+( (randu() < 0.5 ) ? '0' : '1' );
40+
y = fromBinaryString( x );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
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+
}
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+
var randu = require( '@stdlib/random/base/randu' );
22+
var round = require( '@stdlib/math/base/special/round' );
23+
var pow = require( '@stdlib/math/base/special/pow' );
24+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
25+
var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' );
26+
var fromBinaryString = require( './../lib' );
27+
28+
var frac;
29+
var sign;
30+
var exp;
31+
var b;
32+
var x;
33+
var y;
34+
var i;
35+
36+
// Convert random numbers to IEEE 754 literal bit representations and then convert them back...
37+
for ( i = 0; i < 100; i++ ) {
38+
if ( randu() < 0.5 ) {
39+
sign = -1.0;
40+
} else {
41+
sign = 1.0;
42+
}
43+
frac = randu() * 10.0;
44+
exp = round( randu()*5.0 );
45+
if ( randu() < 0.5 ) {
46+
exp = -exp;
47+
}
48+
x = sign * frac * pow( 2.0, exp );
49+
x = toFloat16( x );
50+
51+
b = toBinaryString( x );
52+
y = fromBinaryString( b );
53+
54+
console.log( '%d => %s => %d', x, b, y );
55+
console.log( x === y );
56+
}

0 commit comments

Comments
 (0)