Skip to content

Commit b8104cd

Browse files
committed
feat: add round2f function
1 parent 11547d9 commit b8104cd

File tree

24 files changed

+2115
-0
lines changed

24 files changed

+2115
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
# round2f
22+
23+
> Round a single-precision floating-point number to the nearest power of two on a linear scale.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var round2f = require( '@stdlib/math/base/special/round2f' );
31+
```
32+
33+
#### round2f( x )
34+
35+
Rounds a single-precision floating-point number to the nearest power of two on a linear scale.
36+
37+
```javascript
38+
var v = round2f( -4.2 );
39+
// returns -4.0
40+
41+
v = round2f( -4.5 );
42+
// returns -4.0
43+
44+
v = round2f( -4.6 );
45+
// returns -4.0
46+
47+
v = round2f( 9.99999 );
48+
// returns 8.0
49+
50+
v = round2f( 9.5 );
51+
// returns 8.0
52+
53+
v = round2f( 13.0 );
54+
// returns 16.0
55+
56+
v = round2f( -13.0 );
57+
// returns -16.0
58+
59+
v = round2f( 0.0 );
60+
// returns 0.0
61+
62+
v = round2f( -0.0 );
63+
// returns -0.0
64+
65+
v = round2f( Infinity );
66+
// returns Infinity
67+
68+
v = round2f( -Infinity );
69+
// returns -Infinity
70+
71+
v = round2f( NaN );
72+
// returns NaN
73+
```
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var uniform = require( '@stdlib/random/array/uniform' );
87+
var logEachMap = require( '@stdlib/console/log-each-map' );
88+
var round2f = require( '@stdlib/math/base/special/round2f' );
89+
90+
var opts = {
91+
'dtype': 'float32'
92+
};
93+
var x = uniform( 100, -50.0, 50.0, opts );
94+
95+
logEachMap( 'x: %0.4f. Rounded: %0.4f.', x, round2f );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- C interface documentation. -->
103+
104+
* * *
105+
106+
<section class="c">
107+
108+
## C APIs
109+
110+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
111+
112+
<section class="intro">
113+
114+
</section>
115+
116+
<!-- /.intro -->
117+
118+
<!-- C usage documentation. -->
119+
120+
<section class="usage">
121+
122+
### Usage
123+
124+
```c
125+
#include "stdlib/math/base/special/round2f.h"
126+
```
127+
128+
#### stdlib_base_round2f( x )
129+
130+
Rounds a single-precision floating-point number to the nearest power of two on a linear scale.
131+
132+
```c
133+
float out = stdlib_base_round2f( -4.2 );
134+
// returns -4.0
135+
```
136+
137+
The function accepts the following arguments:
138+
139+
- **x**: `[in] float` input value.
140+
141+
```c
142+
float stdlib_base_round2f( const float x );
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<!-- C API usage examples. -->
158+
159+
<section class="examples">
160+
161+
### Examples
162+
163+
```c
164+
#include "stdlib/math/base/special/round2f.h"
165+
#include <stdio.h>
166+
167+
int main( void ) {
168+
const float x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };
169+
170+
float v;
171+
int i;
172+
for ( i = 0; i < 10; i++ ) {
173+
v = stdlib_base_round2f( x[ i ] );
174+
printf( "round2f(%lf) = %lf\n", x[ i ], v );
175+
}
176+
}
177+
```
178+
179+
</section>
180+
181+
<!-- /.examples -->
182+
183+
</section>
184+
185+
<!-- /.c -->
186+
187+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
188+
189+
<section class="related">
190+
191+
* * *
192+
193+
## See Also
194+
195+
- <span class="package-name">[`@stdlib/math/base/special/ceil2`][@stdlib/math/base/special/ceil2]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of two toward positive infinity.</span>
196+
- <span class="package-name">[`@stdlib/math/base/special/floor2`][@stdlib/math/base/special/floor2]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of two toward negative infinity.</span>
197+
- <span class="package-name">[`@stdlib/math/base/special/round`][@stdlib/math/base/special/round]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest integer.</span>
198+
- <span class="package-name">[`@stdlib/math/base/special/round10`][@stdlib/math/base/special/round10]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest power of 10 on a linear scale.</span>
199+
200+
</section>
201+
202+
<!-- /.related -->
203+
204+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
205+
206+
<section class="links">
207+
208+
<!-- <related-links> -->
209+
210+
[@stdlib/math/base/special/round2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round2
211+
212+
[@stdlib/math/base/special/roundf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundf
213+
214+
<!-- </related-links> -->
215+
216+
</section>
217+
218+
<!-- /.links -->
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 pkg = require( './../package.json' ).name;
27+
var round2f = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -5.0e6, 5.0e6, {
38+
'dtype': 'float32'
39+
});
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = round2f( x[ i%x.length ] );
44+
if ( isnanf( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnanf( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var round2f = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( round2f instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = uniform( 100, -5.0e6, 5.0e6, {
47+
'dtype': 'float32'
48+
});
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = round2f( x[ i%x.length ] );
53+
if ( isnanf( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnanf( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});

0 commit comments

Comments
 (0)