Skip to content

Commit d508751

Browse files
committed
feat: add number/float16/base/to-float32
1 parent 4eb40b3 commit d508751

36 files changed

+2374
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
# toFloat32
22+
23+
> Convert a [half-precision floating-point number][half-precision-floating-point-format] to the nearest [single-precision floating-point number][ieee754].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var float16ToFloat32 = require( '@stdlib/number/float16/base/to-float32' );
31+
```
32+
33+
#### float16ToFloat32( x )
34+
35+
Convert a [half-precision floating-point number][half-precision-floating-point-format] to the nearest [single-precision floating-point number][ieee754].
36+
37+
```javascript
38+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var y = float16ToFloat32( float64ToFloat16( 1.337 ) );
41+
// returns 1.3369140625
42+
```
43+
44+
</section>
45+
46+
<!-- /.usage -->
47+
48+
<section class="notes">
49+
50+
</section>
51+
52+
<!-- /.notes -->
53+
54+
<section class="examples">
55+
56+
## Examples
57+
58+
<!-- eslint no-undef: "error" -->
59+
60+
```javascript
61+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
62+
var uniform = require( '@stdlib/random/array/uniform' );
63+
var map = require( '@stdlib/array/base/map' );
64+
var naryFunction = require( '@stdlib/utils/nary-function' );
65+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
66+
var logEachMap = require( '@stdlib/console/log-each-map' );
67+
var float16ToFloat32 = require( '@stdlib/number/float16/base/to-float32' );
68+
69+
// Generate an array of random numbers:
70+
var f64 = uniform( 100, 0.0, 100.0 );
71+
72+
// Convert each value to a half-precision floating-point number:
73+
var f16 = map( f64, naryFunction( float64ToFloat16, 1 ) );
74+
75+
// Convert each half-precision floating-point number to the nearest single-precision floating-point number:
76+
logEachMap( 'float64: %f => float16: %f => float32: %f', f64, f16, pickArguments( float16ToFloat32, [ 1 ] ) );
77+
```
78+
79+
</section>
80+
81+
<!-- /.examples -->
82+
83+
<!-- C interface documentation. -->
84+
85+
* * *
86+
87+
<section class="c">
88+
89+
## C APIs
90+
91+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
92+
93+
<section class="intro">
94+
95+
</section>
96+
97+
<!-- /.intro -->
98+
99+
<!-- C usage documentation. -->
100+
101+
<section class="usage">
102+
103+
### Usage
104+
105+
```c
106+
#include "stdlib/number/float16/base/to_float32.h"
107+
```
108+
109+
#### stdlib_base_float16_to_float32( x )
110+
111+
Convert a [half-precision floating-point number][half-precision-floating-point-format] to the nearest [single-precision floating-point number][ieee754].
112+
113+
```c
114+
#include "stdlib/number/float16/ctor.h"
115+
116+
stdlib_float16_t v = stdlib_float16_from_bits( 51648 ); // => -11.5
117+
float x = stdlib_base_float16_to_float32( v );
118+
```
119+
120+
The function accepts the following arguments:
121+
122+
- **x**: `[in] stdlib_float16_t` half-precision floating-point number.
123+
124+
```c
125+
float stdlib_base_float16_to_float32( const stdlib_float16_t x );
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="notes">
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<!-- C API usage examples. -->
141+
142+
<section class="examples">
143+
144+
### Examples
145+
146+
```c
147+
#include "stdlib/number/float16/base/to_float32.h"
148+
#include "stdlib/number/float16/ctor.h"
149+
#include <stdint.h>
150+
#include <stdio.h>
151+
152+
int main( void ) {
153+
const stdlib_float16_t x[] = {
154+
stdlib_float16_from_bits( 51648 ), // -11.5
155+
stdlib_float16_from_bits( 18880 ) // 11.5
156+
};
157+
158+
float v;
159+
int i;
160+
for ( i = 0; i < 2; i++ ) {
161+
v = stdlib_base_float16_to_float32( x[ i ] );
162+
printf( "float16 bits: %u => float32: %f\n", stdlib_float16_to_bits( x[ i ] ), v );
163+
}
164+
}
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
</section>
172+
173+
<!-- /.c -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
</section>
180+
181+
<!-- /.related -->
182+
183+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="links">
186+
187+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
188+
189+
[half-precision-floating-point-format]: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
190+
191+
</section>
192+
193+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var map = require( '@stdlib/array/base/map' );
27+
var naryFunction = require( '@stdlib/utils/nary-function' );
28+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
29+
var pkg = require( './../package.json' ).name;
30+
var float16ToFloat32 = 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 = uniform( 100, -5.0e4, 5.0e4 );
41+
x = map( x, naryFunction( float64ToFloat16, 1 ) );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
y = float16ToFloat32( x[ i%x.length ] );
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
}
50+
b.toc();
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var map = require( '@stdlib/array/base/map' );
28+
var naryFunction = require( '@stdlib/utils/nary-function' );
29+
var float64ToFloat16 = require( '@stdlib/number/float64/base/to-float16' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var float16ToFloat32 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( float16ToFloat32 instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var x;
46+
var y;
47+
var i;
48+
49+
x = uniform( 100, -5.0e4, 5.0e4 );
50+
x = map( x, naryFunction( float64ToFloat16, 1 ) );
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = float16ToFloat32( x[ i%x.length ] );
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnanf( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

0 commit comments

Comments
 (0)