Skip to content

Commit 84ad75c

Browse files
feat: add-ceil2f
1 parent 8785e54 commit 84ad75c

File tree

24 files changed

+2074
-0
lines changed

24 files changed

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

0 commit comments

Comments
 (0)