-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add number/float16/base/to-binary-string
#8575
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 3 commits into
stdlib-js:develop
from
gururaj1512:number-float16-base-to-binary-string
Dec 2, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
153 changes: 153 additions & 0 deletions
153
lib/node_modules/@stdlib/number/float16/base/to-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,153 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # toBinaryString | ||
|
|
||
| > Return a string giving the literal bit representation of a [half-precision floating-point number][ieee754]. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' ); | ||
| ``` | ||
|
|
||
| #### toBinaryString( x ) | ||
|
|
||
| Returns a string giving the literal bit representation of a [half-precision floating-point number][ieee754]. | ||
|
|
||
| ```javascript | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
|
|
||
| var str = toBinaryString( toFloat16( 4.0 ) ); | ||
| // returns '0100010000000000' | ||
|
|
||
| str = toBinaryString( toFloat16( 3.1415926 ) ); | ||
| // returns '0100001001001000' | ||
|
|
||
| str = toBinaryString( toFloat16( -1.0e3 ) ); | ||
| // returns '1110001111010000' | ||
| ``` | ||
|
|
||
| The function handles [subnormals][subnormals]. | ||
|
|
||
| ```javascript | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
|
|
||
| var str = toBinaryString( toFloat16( -3.14e-6 ) ); | ||
| // returns '1000000000110101' | ||
|
|
||
| str = toBinaryString( toFloat16( 1.4e-7 ) ); | ||
| // returns '0000000000000010' | ||
| ``` | ||
|
|
||
| The function handles special values. | ||
|
|
||
| ```javascript | ||
| var PINF = require( '@stdlib/constants/float16/pinf' ); | ||
| var NINF = require( '@stdlib/constants/float16/ninf' ); | ||
|
|
||
| var str = toBinaryString( 0.0 ); | ||
| // returns '0000000000000000' | ||
|
|
||
| str = toBinaryString( -0.0 ); | ||
| // returns '1000000000000000' | ||
|
|
||
| str = toBinaryString( NaN ); | ||
| // returns '0111111000000000' | ||
|
|
||
| str = toBinaryString( PINF ); | ||
| // returns '0111110000000000' | ||
|
|
||
| str = toBinaryString( NINF ); | ||
| // returns '1111110000000000' | ||
| ``` | ||
|
|
||
| </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 float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var toBinaryString = require( '@stdlib/number/float16/base/to-binary-string' ); | ||
|
|
||
| var frac; | ||
| var sign; | ||
| var exp; | ||
| var b; | ||
| var x; | ||
| var i; | ||
|
|
||
| // Convert random numbers to literal bit representations... | ||
| for ( i = 0; i < 100; i++ ) { | ||
| if ( randu() < 0.5 ) { | ||
| sign = -1.0; | ||
| } else { | ||
| sign = 1.0; | ||
| } | ||
| frac = randu() * 10.0; | ||
| exp = round( randu()*10.0 ); | ||
| if ( randu() < 0.5 ) { | ||
| exp = -exp; | ||
| } | ||
| x = sign * frac * pow( 2.0, exp ); | ||
| x = float64ToFloat16( x ); | ||
| b = toBinaryString( x ); | ||
| console.log( b ); | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </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-2008 | ||
|
|
||
| [subnormals]: https://en.wikipedia.org/wiki/Denormal_number | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/number/float16/base/to-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,52 @@ | ||
| /** | ||
| * @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var toBinaryString = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| x = ( randu()*1.0e7 ) - 5.0e6; | ||
| y = toBinaryString( toFloat16( x ) ); | ||
| if ( typeof y !== 'string' ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isString( y ) ) { | ||
| b.fail( 'should return a string' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/number/float16/base/to-binary-string/docs/repl.txt
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,41 @@ | ||
|
|
||
| {{alias}}( x ) | ||
| Returns a string giving the literal bit representation of a half-precision | ||
| floating-point number. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: float | ||
| Half-precision floating-point number. | ||
|
|
||
| Returns | ||
| ------- | ||
| str: string | ||
| Bit representation. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 4.0 ) ) | ||
| '0100010000000000' | ||
| > str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.1415926 ) ) | ||
| '0100001001001000' | ||
| > str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -1.0e3 ) ) | ||
| '1110001111010000' | ||
| > str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14e-6 ) ) | ||
| '1000000000110101' | ||
| > str = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 1.4e-7 ) ) | ||
| '0000000000000010' | ||
| > str = {{alias}}( 0.0 ) | ||
| '0000000000000000' | ||
| > str = {{alias}}( -0.0 ) | ||
| '1000000000000000' | ||
| > str = {{alias}}( NaN ) | ||
| '0111111000000000' | ||
| > str = {{alias}}( {{alias:@stdlib/constants/float16/pinf}} ) | ||
| '0111110000000000' | ||
| > str = {{alias}}( {{alias:@stdlib/constants/float16/ninf}} ) | ||
| '1111110000000000' | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
86 changes: 86 additions & 0 deletions
86
lib/node_modules/@stdlib/number/float16/base/to-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,86 @@ | ||
| /* | ||
| * @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 | ||
|
|
||
| /** | ||
| * Returns a string giving the literal bit representation of a half-precision floating-point number. | ||
| * | ||
| * @param x - input value | ||
| * @returns bit representation | ||
| * | ||
| * @example | ||
| * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var str = toBinaryString( toFloat16( 4.0 ) ); | ||
| * // returns '0100010000000000' | ||
| * | ||
| * @example | ||
| * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var str = toBinaryString( toFloat16( 3.1415926 ) ); | ||
| * // returns '0100001001001000' | ||
| * | ||
| * @example | ||
| * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var str = toBinaryString( toFloat16( -1.0e3 ) ); | ||
| * // returns '1110001111010000' | ||
| * | ||
| * @example | ||
| * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var str = toBinaryString( toFloat16( -3.14e-6 ) ); | ||
| * // returns '1000000000110101' | ||
| * | ||
| * @example | ||
| * var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var str = toBinaryString( toFloat16( 1.4e-7 ) ); | ||
| * // returns '0000000000000010' | ||
| * | ||
| * @example | ||
| * var str = toBinaryString( 0.0 ); | ||
| * // returns '0000000000000000' | ||
| * | ||
| * @example | ||
| * var str = toBinaryString( -0.0 ); | ||
| * // returns '1000000000000000' | ||
| * | ||
| * @example | ||
| * var str = toBinaryString( NaN ); | ||
| * // returns '0111111000000000' | ||
| * | ||
| * @example | ||
| * var PINF = require( '@stdlib/constants/float16/pinf' ); | ||
| * | ||
| * var str = toBinaryString( PINF ); | ||
| * // returns '0111110000000000' | ||
| * | ||
| * @example | ||
| * var NINF = require( '@stdlib/constants/float16/ninf' ); | ||
| * | ||
| * var str = toBinaryString( NINF ); | ||
| * // returns '1111110000000000' | ||
| */ | ||
| declare function toBinaryString( x: number ): string; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = toBinaryString; |
43 changes: 43 additions & 0 deletions
43
lib/node_modules/@stdlib/number/float16/base/to-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,43 @@ | ||
| /* | ||
| * @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 toBinaryString = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a string... | ||
| { | ||
| toBinaryString( 0.0 ); // $ExpectType string | ||
| toBinaryString( -0.0 ); // $ExpectType string | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a value other than a number... | ||
| { | ||
| toBinaryString( true ); // $ExpectError | ||
| toBinaryString( false ); // $ExpectError | ||
| toBinaryString( 'abc' ); // $ExpectError | ||
| toBinaryString( [] ); // $ExpectError | ||
| toBinaryString( {} ); // $ExpectError | ||
| toBinaryString( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| toBinaryString(); // $ExpectError | ||
| } |
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.