Skip to content

Commit fd262b7

Browse files
feat: adding inital files
1 parent 4556b18 commit fd262b7

File tree

14 files changed

+732
-0
lines changed

14 files changed

+732
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
# toUint32
22+
23+
> Convert a [half-precision floating-point number][ieee754] to an unsigned 32-bit integer.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var float16ToUint32 = require( '@stdlib/number/float16/base/to-uint32' );
31+
```
32+
33+
#### float16ToUint32( x )
34+
35+
Converts a [half-precision floating-point number][ieee754] to an unsigned 32-bit integer.
36+
37+
```javascript
38+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var y = float16ToUint32( float64ToFloat16( 4294967297.0 ) );
41+
// returns 0
42+
43+
y = float16ToUint32( float64ToFloat16( 3.14 ) );
44+
// returns 3
45+
46+
y = float16ToUint32( float64ToFloat16( -3.14 ) );
47+
// returns 4294967293
48+
49+
y = float16ToUint32( float64ToFloat16( NaN ) );
50+
// returns 0
51+
52+
y = float16ToUint32( float64ToFloat16( Infinity ) );
53+
// returns 0
54+
55+
y = float16ToUint32( float64ToFloat16( -Infinity ) );
56+
// returns 0
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<section class="examples">
64+
65+
## Examples
66+
67+
<!-- eslint no-undef: "error" -->
68+
69+
```javascript
70+
var uniform = require( '@stdlib/random/array/uniform' );
71+
var map = require( '@stdlib/array/base/map' );
72+
var naryFunction = require( '@stdlib/utils/nary-function' );
73+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
74+
var logEachMap = require( '@stdlib/console/log-each-map' );
75+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
76+
var float16ToUint32 = require( '@stdlib/number/float16/base/to-uint32' );
77+
78+
// Generate an array of random numbers:
79+
var f64 = uniform( 100, 0.0, 100.0, {
80+
'dtype': 'float64'
81+
});
82+
83+
// Convert each value to a half-precision floating-point number:
84+
var f16 = map( f64, naryFunction( float64ToFloat16, 1 ) );
85+
86+
// Convert each half-precision floating-point number to the nearest unsigned 32-bit integer:
87+
logEachMap( 'float16: %f => uint32: %d', f16, pickArguments( float16ToUint32, [ 1 ] ) );
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
95+
96+
<section class="related">
97+
98+
</section>
99+
100+
<!-- /.related -->
101+
102+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="links">
105+
106+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
107+
108+
</section>
109+
110+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
27+
var map = require( '@stdlib/array/base/map' );
28+
var naryFunction = require( '@stdlib/utils/nary-function' );
29+
var pkg = require( './../package.json' ).name;
30+
var float16ToUint32 = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var x;
37+
var y;
38+
var i;
39+
40+
x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
y = float16ToUint32( x[ i%x.length ] );
45+
if ( isnan( y ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x )
3+
Converts a half-precision floating-point number to a unsigned 32-bit
4+
integer.
5+
6+
Parameters
7+
----------
8+
x: float
9+
Half-precision floating-point number.
10+
11+
Returns
12+
-------
13+
out: integer
14+
Unsigned 32-bit integer.
15+
16+
Examples
17+
--------
18+
> var y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 4294967297.0 ) )
19+
0
20+
> y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( 3.14 ) )
21+
3
22+
> y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( -3.14 ) )
23+
4294967293
24+
> y = {{alias}}( {{alias:@stdlib/number/float64/base/to-float16}}( NaN ) )
25+
0
26+
> y = {{alias}}( {{alias:@stdlib/constants/float16/pinf}} )
27+
0
28+
> y = {{alias}}( {{alias:@stdlib/constants/float16/ninf}} )
29+
0
30+
31+
See Also
32+
--------
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* Converts a half-precision floating-point number to an unsigned 32-bit integer.
23+
*
24+
* @param x - half-precision floating-point number
25+
* @returns unsigned 32-bit integer
26+
*
27+
* @example
28+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
29+
*
30+
* var y = float16ToUint32( float64ToFloat16( 4294967297.0 ) );
31+
* // returns 0
32+
*
33+
* @example
34+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
35+
*
36+
* var y = float16ToUint32( float64ToFloat16( 3.14 ) );
37+
* // returns 3
38+
*
39+
* @example
40+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
41+
*
42+
* var y = float16ToUint32( float64ToFloat16( -3.14 ) );
43+
* // returns 4294967293
44+
*
45+
* @example
46+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
47+
*
48+
* var y = float16ToUint32( float64ToFloat16( NaN ) );
49+
* // returns 0
50+
*
51+
* @example
52+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
53+
*
54+
* var y = float16ToUint32( float64ToFloat16( Infinity ) );
55+
* // returns 0
56+
*
57+
* @example
58+
* var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
59+
*
60+
* var y = float16ToUint32( float64ToFloat16( -Infinity ) );
61+
* // returns 0
62+
*/
63+
declare function float16ToUint32( x: number ): number;
64+
65+
66+
// EXPORTS //
67+
68+
export = float16ToUint32;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 float16ToUint32 = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
float16ToUint32( 3.14 ); // $ExpectType number
27+
float16ToUint32( -3.14 ); // $ExpectType number
28+
}
29+
30+
// The compiler throws an error if the function is provided a value other than a number...
31+
{
32+
float16ToUint32( true ); // $ExpectError
33+
float16ToUint32( false ); // $ExpectError
34+
float16ToUint32( 'abc' ); // $ExpectError
35+
float16ToUint32( [] ); // $ExpectError
36+
float16ToUint32( {} ); // $ExpectError
37+
float16ToUint32( ( x: number ): number => x ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided insufficient arguments...
41+
{
42+
float16ToUint32(); // $ExpectError
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 uniform = require( '@stdlib/random/array/uniform' );
22+
var map = require( '@stdlib/array/base/map' );
23+
var naryFunction = require( '@stdlib/utils/nary-function' );
24+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
25+
var logEachMap = require( '@stdlib/console/log-each-map' );
26+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
27+
var float16ToUint32 = require( './../lib' );
28+
29+
// Generate an array of random numbers:
30+
var f64 = uniform( 100, 0.0, 100.0, {
31+
'dtype': 'float64'
32+
});
33+
34+
// Convert each value to a half-precision floating-point number:
35+
var f16 = map( f64, naryFunction( float64ToFloat16, 1 ) );
36+
37+
// Convert each half-precision floating-point number to the nearest unsigned 32-bit integer:
38+
logEachMap( 'float16: %f => uint32: %d', f16, pickArguments( float16ToUint32, [ 1 ] ) );

0 commit comments

Comments
 (0)