-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add number/float16/base/to-int32
#9306
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
Open
Neerajpathak07
wants to merge
3
commits into
stdlib-js:develop
Choose a base branch
from
Neerajpathak07:float16/to-int32
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+735
−0
Open
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
110 changes: 110 additions & 0 deletions
110
lib/node_modules/@stdlib/number/float16/base/to-int32/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,110 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # toInt32 | ||
|
|
||
| > Convert a [half-precision floating-point number][ieee754] to a signed 32-bit integer. | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var float16ToInt32 = require( '@stdlib/number/float16/base/to-int32' ); | ||
| ``` | ||
|
|
||
| #### float16ToInt32( x ) | ||
|
|
||
| Converts a [half-precision floating-point number][ieee754] to a signed 32-bit integer. | ||
|
|
||
| ```javascript | ||
| var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
|
|
||
| var y = float16ToInt32( float64ToFloat16( 4294967295.0 ) ); | ||
| // returns 0 | ||
|
|
||
| y = float16ToInt32( float64ToFloat16( 3.14 ) ); | ||
| // returns 3 | ||
|
|
||
| y = float16ToInt32( float64ToFloat16( -3.14 ) ); | ||
| // returns -3 | ||
|
|
||
| y = float16ToInt32( float64ToFloat16( NaN ) ); | ||
| // returns 0 | ||
|
|
||
| y = float16ToInt32( float64ToFloat16( Infinity ) ); | ||
| // returns 0 | ||
|
|
||
| y = float16ToInt32( float64ToFloat16( -Infinity ) ); | ||
| // returns 0 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var pickArguments = require( '@stdlib/utils/pick-arguments' ); | ||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||
| var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var float16ToInt32 = require( '@stdlib/number/float16/base/to-int32' ); | ||
|
|
||
| // Generate an array of random numbers: | ||
| var f64 = uniform( 100, 0.0, 100.0, { | ||
| 'dtype': 'float64' | ||
| }); | ||
|
|
||
| // Convert each value to a half-precision floating-point number: | ||
| var f16 = map( f64, naryFunction( float64ToFloat16, 1 ) ); | ||
|
|
||
| // Convert each half-precision floating-point number to the nearest signed 32-bit integer: | ||
| logEachMap( 'float16: %f => int32: %d', f16, pickArguments( float16ToInt32, [ 1 ] ) ); | ||
| ``` | ||
|
|
||
| </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 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
55 changes: 55 additions & 0 deletions
55
lib/node_modules/@stdlib/number/float16/base/to-int32/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,55 @@ | ||
| /** | ||
| * @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 uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var toFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var float16ToInt32 = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = float16ToInt32( x[ i%x.length ] ); | ||
| 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(); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/number/float16/base/to-int32/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,32 @@ | ||
|
|
||
| {{alias}}( x ) | ||
| Converts a half-precision floating-point number to a signed 32-bit | ||
| integer. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x: number | ||
| Half-precision floating-point number. | ||
|
|
||
| Returns | ||
| ------- | ||
| out: integer | ||
| Signed 32-bit integer. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 4294967295.0 ) ) | ||
| 0 | ||
| > y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14 ) ) | ||
| 3 | ||
| > y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14 ) ) | ||
| -3 | ||
| > y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( NaN ) ) | ||
| 0 | ||
| > y = {{alias}}( {{alias:@stdlib/constants/float16/pinf}} ) | ||
| 0 | ||
| > y = {{alias}}( {{alias:@stdlib/constants/float16/ninf}} ) | ||
| 0 | ||
|
|
||
| See Also | ||
| -------- | ||
68 changes: 68 additions & 0 deletions
68
lib/node_modules/@stdlib/number/float16/base/to-int32/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,68 @@ | ||
| /* | ||
| * @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 | ||
|
|
||
| /** | ||
| * Converts a half-precision floating-point number to a signed 32-bit integer. | ||
| * | ||
| * @param x - half-precision floating-point number | ||
| * @returns signed 32-bit integer | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( 4294967295.0 ) ); | ||
| * // returns 0 | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( 3.14 ) ); | ||
| * // returns 3 | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( -3.14 ) ); | ||
| * // returns -3 | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( NaN ) ); | ||
| * // returns 0 | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( Infinity ) ); | ||
| * // returns 0 | ||
| * | ||
| * @example | ||
| * var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| * | ||
| * var y = float16ToInt32( float64ToFloat16( -Infinity ) ); | ||
| * // returns 0 | ||
| */ | ||
| declare function float16ToInt32( x: number ): number; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = float16ToInt32; |
43 changes: 43 additions & 0 deletions
43
lib/node_modules/@stdlib/number/float16/base/to-int32/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 float16ToInt32 = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a number... | ||
| { | ||
| float16ToInt32( 3.14 ); // $ExpectType number | ||
| float16ToInt32( -3.14 ); // $ExpectType number | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a value other than a number... | ||
| { | ||
| float16ToInt32( true ); // $ExpectError | ||
| float16ToInt32( false ); // $ExpectError | ||
| float16ToInt32( 'abc' ); // $ExpectError | ||
| float16ToInt32( [] ); // $ExpectError | ||
| float16ToInt32( {} ); // $ExpectError | ||
| float16ToInt32( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| float16ToInt32(); // $ExpectError | ||
| } |
38 changes: 38 additions & 0 deletions
38
lib/node_modules/@stdlib/number/float16/base/to-int32/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,38 @@ | ||
| /** | ||
| * @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 uniform = require( '@stdlib/random/array/uniform' ); | ||
| var map = require( '@stdlib/array/base/map' ); | ||
| var naryFunction = require( '@stdlib/utils/nary-function' ); | ||
| var pickArguments = require( '@stdlib/utils/pick-arguments' ); | ||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||
| var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' ); | ||
| var float16ToInt32 = require( './../lib' ); | ||
|
|
||
| // Generate an array of random numbers: | ||
| var f64 = uniform( 100, 0.0, 100.0, { | ||
| 'dtype': 'float64' | ||
| }); | ||
|
|
||
| // Convert each value to a half-precision floating-point number: | ||
| var f16 = map( f64, naryFunction( float64ToFloat16, 1 ) ); | ||
|
|
||
| // Convert each half-precision floating-point number to the nearest signed 32-bit integer: | ||
| logEachMap( 'float16: %f => int32: %d', f16, pickArguments( float16ToInt32, [ 1 ] ) ); |
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.