-
-
Notifications
You must be signed in to change notification settings - Fork 960
feat: add number/float16/base/to-word
#8371
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
base: develop
Are you sure you want to change the base?
Changes from all commits
333ea76
33a3c9c
504950a
df92e7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # toWord | ||
|
|
||
| > Return an unsigned 16-bit integer corresponding to the [IEEE 754][ieee754] binary representation of a [half-precision floating-point number][ieee754]. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var toWord = require( '@stdlib/number/float16/base/to-word' ); | ||
| ``` | ||
|
|
||
| #### toWord( x ) | ||
|
|
||
| Returns an unsigned 16-bit `integer` corresponding to the [IEEE 754][ieee754] binary representation of a [half-precision floating-point number][ieee754]. | ||
|
|
||
| ```javascript | ||
| var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
|
|
||
| var f16 = float64ToFloat16( 1.05 ); | ||
| // returns 1.0498046875 | ||
|
|
||
| var w = toWord( f16 ); // => 0 01111 0000110011 | ||
| // returns 15411 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var toWord = require( '@stdlib/number/float16/base/to-word' ); | ||
|
|
||
| var word; | ||
| var f64; | ||
| var f16; | ||
| var i; | ||
|
|
||
| // Convert half-precision floating-point numbers to integers representing the binary literal... | ||
| for ( i = 0; i < 1000; i++ ) { | ||
| f64 = (randu()*100.0) - 50.0; | ||
| f16 = float64ToFloat16( f64 ); | ||
| word = toWord( f16 ); | ||
| console.log( 'float64: %d => float16: %d => word: %d', f64, f16, word ); | ||
| } | ||
| ``` | ||
|
|
||
| </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-1985 | ||
|
|
||
| <!-- <related-links> --> | ||
|
|
||
| <!-- </related-links> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
| 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var toWord = 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gururaj1512 Would you mind updating this file to use |
||
| y = toWord( toFloat16( 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(); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
|
|
||||||
| {{alias}}( x ) | ||||||
| Returns an unsigned 16-bit integer corresponding to the IEEE 754 binary | ||||||
| representation of a half-precision floating-point number. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| x: float | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| half-precision floating-point number. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| out: integer | ||||||
| Unsigned 16-bit integer. | ||||||
|
|
||||||
| Examples | ||||||
| -------- | ||||||
| > var f16 = {{alias:@stdlib/number/float64/base/to-float16}}( 1.05 ) | ||||||
| 1.0498046875 | ||||||
| > var w = {{alias}}( f16 ) | ||||||
| 15411 | ||||||
|
|
||||||
| See Also | ||||||
| -------- | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * @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 an unsigned 16-bit integer corresponding to the IEEE 754 binary representation of a half-precision floating-point number. | ||
| * | ||
| * @param x - half-precision floating-point number | ||
| * @returns unsigned 16-bit integer | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var f16 = float64ToFloat16( 1.05 ); | ||
| * // returns 1.0498046875 | ||
| * | ||
| * var w = toWord( f16 ); // => 0 01111 0000110011 | ||
| * // returns 15411 | ||
| */ | ||
| declare function toWord( x: number ): number; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = toWord; |
| 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 toWordf = require( './index' ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/toWordf/toWord/ here and below. |
||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a number... | ||
| { | ||
| toWordf( 3.14 ); // $ExpectType number | ||
| toWordf( -3.14 ); // $ExpectType number | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a value other than a number... | ||
| { | ||
| toWordf( true ); // $ExpectError | ||
| toWordf( false ); // $ExpectError | ||
| toWordf( 'abc' ); // $ExpectError | ||
| toWordf( [] ); // $ExpectError | ||
| toWordf( {} ); // $ExpectError | ||
| toWordf( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| toWordf(); // $ExpectError | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * @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 float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var toWord = require( './../lib' ); | ||
|
|
||
| var word; | ||
| var f64; | ||
| var f16; | ||
| var i; | ||
|
|
||
| // Convert half-precision floating-point numbers to integers representing the binary literal... | ||
| for ( i = 0; i < 1000; i++ ) { | ||
| f64 = (randu()*100.0) - 50.0; | ||
| f16 = float64ToFloat16( f64 ); | ||
| word = toWord( f16 ); | ||
| console.log( 'float64: %d => float16: %d => word: %d', f64, f16, word ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.