-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add number/float16/base/from-binary-string
#9322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 12 commits into
stdlib-js:develop
from
Erennn7:feat/float16-from-binary-string
Dec 26, 2025
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7797875
feat: add number/float16/base/from-binary-string
Erennn7 1b76633
Update lib/node_modules/@stdlib/number/float16/base/from-binary-strin…
Erennn7 151ce68
Update lib/node_modules/@stdlib/number/float16/base/from-binary-strin…
Erennn7 b29c3e2
Update lib/node_modules/@stdlib/number/float16/base/from-binary-strin…
Erennn7 46ed9f6
Update lib/node_modules/@stdlib/number/float16/base/from-binary-strin…
Erennn7 5cda6a2
test: add fixtures and repl.txt
Erennn7 2d74c8b
fix: extra tests removed
Erennn7 3466a6a
fix: update test fixtures
Erennn7 073db35
fix: update test variable names
Erennn7 f60b895
docs: update heading
kgryte 7157f34
docs: remove related section
kgryte a094272
bench: refactor random value generation
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
lib/node_modules/@stdlib/number/float16/base/from-binary-string/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2025 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # From Binary String | ||
|
|
||
| > Create a [half-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float16/base/to-binary-string]. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var fromBinaryString = require( '@stdlib/number/float16/base/from-binary-string' ); | ||
| ``` | ||
|
|
||
| #### fromBinaryString( bstr ) | ||
|
|
||
| Creates a [half-precision floating-point number][ieee754] from an [IEEE 754 literal bit representation][@stdlib/number/float16/base/to-binary-string]. | ||
|
|
||
| ```javascript | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
|
|
||
| var bstr = '0100010000000000'; | ||
|
Erennn7 marked this conversation as resolved.
Outdated
|
||
| var v = fromBinaryString( bstr ); | ||
| // returns 4.0 | ||
|
|
||
| bstr = '0100001001001000'; | ||
| v = fromBinaryString( bstr ); | ||
| // returns ~3.140625 | ||
|
|
||
| bstr = '1110001111010000'; | ||
| v = fromBinaryString( bstr ); | ||
| // returns -1000.0 | ||
| ``` | ||
|
|
||
| The function handles [subnormals][subnormals]. | ||
|
|
||
| ```javascript | ||
| var bstr = '1000000000000001'; | ||
| var val = fromBinaryString( bstr ); | ||
| // returns ~-5.96e-8 | ||
|
|
||
| bstr = '0000000000000001'; | ||
| val = fromBinaryString( bstr ); | ||
| // returns ~5.96e-8 | ||
| ``` | ||
|
|
||
| The function handles special values. | ||
|
|
||
| ```javascript | ||
| var bstr = '0000000000000000'; | ||
| var val = fromBinaryString( bstr ); | ||
| // returns 0.0 | ||
|
|
||
| bstr = '1000000000000000'; | ||
| val = fromBinaryString( bstr ); | ||
| // returns -0.0 | ||
|
|
||
| bstr = '0111111000000000'; | ||
| val = fromBinaryString( bstr ); | ||
| // returns NaN | ||
|
|
||
| bstr = '0111110000000000'; | ||
| val = fromBinaryString( bstr ); | ||
| // returns Infinity | ||
|
|
||
| bstr = '1111110000000000'; | ||
| val = fromBinaryString( bstr ); | ||
| // returns -Infinity | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var round = require( '@stdlib/math/base/special/round' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' ); | ||
| var fromBinaryString = require( '@stdlib/number/float16/base/from-binary-string' ); | ||
|
|
||
| var frac; | ||
| var sign; | ||
| var exp; | ||
| var b; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| // Convert random numbers to IEEE 754 literal bit representations and then convert them back... | ||
| for ( i = 0; i < 100; i++ ) { | ||
| if ( randu() < 0.5 ) { | ||
| sign = -1.0; | ||
| } else { | ||
| sign = 1.0; | ||
| } | ||
| frac = randu() * 10.0; | ||
| exp = round( randu()*5.0 ); | ||
| if ( randu() < 0.5 ) { | ||
| exp = -exp; | ||
| } | ||
| x = sign * frac * pow( 2.0, exp ); | ||
| x = toFloat16( x ); | ||
|
|
||
| b = toBinaryString( x ); | ||
| y = fromBinaryString( b ); | ||
|
|
||
| console.log( '%d => %s => %d', x, b, y ); | ||
| console.log( x === y ); | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| * * * | ||
|
|
||
| ## See Also | ||
|
|
||
| - <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> | ||
| - <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> | ||
| - <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> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 | ||
|
|
||
| [subnormals]: https://en.wikipedia.org/wiki/Denormal_number | ||
|
|
||
| [@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 | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| [@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 | ||
|
|
||
| [@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 | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/number/float16/base/from-binary-string/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var fromBinaryString = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| x = '101010101010101'+( (randu() < 0.5 ) ? '0' : '1' ); | ||
| y = fromBinaryString( x ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
58 changes: 58 additions & 0 deletions
58
lib/node_modules/@stdlib/number/float16/base/from-binary-string/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /** | ||
| * Creates a half-precision floating-point number from an IEEE 754 literal bit representation. | ||
| * | ||
| * @param bstr - string which is a literal bit representation | ||
| * @throws must provide a string with a length equal to `16` | ||
| * @returns half-precision floating-point number | ||
| * | ||
| * @example | ||
| * var bstr = '0100010000000000'; | ||
| * var v = fromBinaryString( bstr ); | ||
| * // returns 4.0 | ||
| * | ||
| * @example | ||
| * var bstr = '0100001001001000'; | ||
| * var v = fromBinaryString( bstr ); | ||
| * // returns ~3.140625 | ||
| * | ||
| * @example | ||
| * var bstr = '1110001111010000'; | ||
| * var v = fromBinaryString( bstr ); | ||
| * // returns -1000.0 | ||
| * | ||
| * @example | ||
| * var bstr = '0000000000000000'; | ||
| * var v = fromBinaryString( bstr ); | ||
| * // returns 0.0 | ||
| * | ||
| * @example | ||
| * var bstr = '1000000000000000'; | ||
| * var v = fromBinaryString( bstr ); | ||
| * // returns -0.0 | ||
| */ | ||
| declare function fromBinaryString( bstr: string ): number; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = fromBinaryString; |
44 changes: 44 additions & 0 deletions
44
lib/node_modules/@stdlib/number/float16/base/from-binary-string/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import fromBinaryString = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a number... | ||
| { | ||
| fromBinaryString( '0100010000000000' ); // $ExpectType number | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a value other than a string... | ||
| { | ||
| fromBinaryString( true ); // $ExpectError | ||
| fromBinaryString( false ); // $ExpectError | ||
| fromBinaryString( null ); // $ExpectError | ||
| fromBinaryString( undefined ); // $ExpectError | ||
| fromBinaryString( 5 ); // $ExpectError | ||
| fromBinaryString( [] ); // $ExpectError | ||
| fromBinaryString( {} ); // $ExpectError | ||
| fromBinaryString( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| fromBinaryString(); // $ExpectError | ||
| } |
56 changes: 56 additions & 0 deletions
56
lib/node_modules/@stdlib/number/float16/base/from-binary-string/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var round = require( '@stdlib/math/base/special/round' ); | ||
| var pow = require( '@stdlib/math/base/special/pow' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' ); | ||
| var fromBinaryString = require( './../lib' ); | ||
|
|
||
| var frac; | ||
| var sign; | ||
| var exp; | ||
| var b; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| // Convert random numbers to IEEE 754 literal bit representations and then convert them back... | ||
| for ( i = 0; i < 100; i++ ) { | ||
| if ( randu() < 0.5 ) { | ||
| sign = -1.0; | ||
| } else { | ||
| sign = 1.0; | ||
| } | ||
| frac = randu() * 10.0; | ||
| exp = round( randu()*5.0 ); | ||
| if ( randu() < 0.5 ) { | ||
| exp = -exp; | ||
| } | ||
| x = sign * frac * pow( 2.0, exp ); | ||
| x = toFloat16( x ); | ||
|
|
||
| b = toBinaryString( x ); | ||
| y = fromBinaryString( b ); | ||
|
|
||
| console.log( '%d => %s => %d', x, b, y ); | ||
| console.log( x === y ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.