Skip to content

Commit 582661f

Browse files
feat:- add-ceilnf
1 parent 8785e54 commit 582661f

File tree

24 files changed

+2399
-0
lines changed

24 files changed

+2399
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# ceilnf
22+
23+
> Round a numeric value to the nearest multiple of 10^n toward positive infinity.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var ceilnf = require( '@stdlib/math/base/special/ceilnf' );
31+
```
32+
33+
#### ceilnf( x, n )
34+
35+
Rounds a `numeric` value to the nearest multiple of `10^n` toward positive infinity.
36+
37+
```javascript
38+
// Round a value to 2 decimal places:
39+
var v = ceilnf( 3.141592653589793, -2 );
40+
// returns 3.15
41+
42+
// If n = 0, `ceiln` behaves like `ceil`:
43+
v = ceilnf( 3.141592653589793, 0 );
44+
// returns 4.0
45+
46+
// Round a value to the nearest thousand:
47+
v = ceilnf( 12368.0, 3 );
48+
// returns 13000.0
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
## Notes
58+
59+
- When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. For example,
60+
61+
```javascript
62+
var x = 0.2 + 0.1;
63+
// returns 0.30000000000000004
64+
65+
// Should round to 0.3:
66+
var v = ceilnf( x, -16 );
67+
// returns 0.3000000000000001
68+
```
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var randu = require( '@stdlib/random/base/randu' );
82+
var ceilnf = require( './../lib' );
83+
84+
var x;
85+
var n;
86+
var v;
87+
var i;
88+
89+
for ( i = 0; i < 100; i++ ) {
90+
x = (randu()*100.0) - 50.0;
91+
n = ceilnf( randu()*5.0, 0 );
92+
v = ceilnf( x, -n );
93+
console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v );
94+
}
95+
```
96+
97+
</section>
98+
99+
<!-- /.examples -->
100+
101+
<!-- C interface documentation. -->
102+
103+
* * *
104+
105+
<section class="c">
106+
107+
## C APIs
108+
109+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
110+
111+
<section class="intro">
112+
113+
</section>
114+
115+
<!-- /.intro -->
116+
117+
<!-- C usage documentation. -->
118+
119+
<section class="usage">
120+
121+
### Usage
122+
123+
```c
124+
#include "stdlib/math/base/special/ceilnf.h"
125+
```
126+
127+
#### stdlib_base_ceilnf( x, n )
128+
129+
Rounds a single-precision floating-point number to the nearest multiple of `10^n` toward positive infinity.
130+
131+
```c
132+
// Round a value to 2 decimal places:
133+
float y = stdlib_base_ceilnf( 3.141592653589793f, -2f );
134+
// returns 3.15f
135+
136+
// If n = 0, `ceilnf` behaves like `ceil`:
137+
float y = stdlib_base_ceilnf( 3.141592653589793f, 0f );
138+
// returns 4.0f
139+
140+
// Round a value to the nearest thousand:
141+
float y = stdlib_base_ceilnf( 12368.0f, 3f );
142+
// returns 13000.0f
143+
```
144+
145+
The function accepts the following arguments:
146+
147+
- **x**: `[in] float` input value.
148+
- **n**: `[in] int32_t` integer power of 10.
149+
150+
```c
151+
float stdlib_base_ceilnf( const float x, const int32_t n );
152+
```
153+
154+
</section>
155+
156+
<!-- /.usage -->
157+
158+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="notes">
161+
162+
</section>
163+
164+
<!-- /.notes -->
165+
166+
<!-- C API usage examples. -->
167+
168+
<section class="examples">
169+
170+
### Examples
171+
172+
```c
173+
#include "stdlib/math/base/special/ceilnf.h"
174+
#include <stdio.h>
175+
176+
int main( void ) {
177+
const float x[] = { 3.14f, -3.14f, 0.0f, 0.0/0.0 };
178+
179+
float y;
180+
int i;
181+
for ( i = 0; i < 4; i++ ) {
182+
y = stdlib_base_ceilnf( x[ i ], -2f );
183+
printf( "ceilnf(%f, -2f) = %f\n", x[ i ], y );
184+
}
185+
}
186+
```
187+
188+
</section>
189+
190+
<!-- /.examples -->
191+
192+
</section>
193+
194+
<!-- /.c -->
195+
196+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
197+
198+
<section class="related">
199+
200+
* * *
201+
202+
## See Also
203+
204+
- <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>
205+
- <span class="package-name">[`@stdlib/math/base/special/ceilb`][@stdlib/math/base/special/ceilb]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest multiple of b^n toward positive infinity.</span>
206+
- <span class="package-name">[`@stdlib/math/base/special/floorn`][@stdlib/math/base/special/floorn]</span><span class="delimiter">: </span><span class="description">round a double-precision floating-point number to the nearest multiple of 10^n toward negative infinity.</span>
207+
- <span class="package-name">[`@stdlib/math/base/special/roundn`][@stdlib/math/base/special/roundn]</span><span class="delimiter">: </span><span class="description">round a double-precision floating-point number to the nearest multiple of 10^n.</span>
208+
209+
</section>
210+
211+
<!-- /.related -->
212+
213+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
214+
215+
<section class="links">
216+
217+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
218+
219+
<!-- <related-links> -->
220+
221+
[@stdlib/math/base/special/ceil]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceil
222+
223+
[@stdlib/math/base/special/ceilb]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceilb
224+
225+
[@stdlib/math/base/special/floorn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floorn
226+
227+
[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundn
228+
229+
<!-- </related-links> -->
230+
231+
</section>
232+
233+
<!-- /.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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var ceilnf = 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 = ceilnf( x, -2 );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var ceilnf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( ceilnf 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 = ceilnf( x, -2 );
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)