Skip to content

Commit 2839a44

Browse files
feat: add-cceilnf
1 parent f175cf9 commit 2839a44

File tree

24 files changed

+2605
-0
lines changed

24 files changed

+2605
-0
lines changed
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
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+
# cceiln
22+
23+
> Round each component of a single-precision complex floating-point number to the nearest multiple of `10^n` toward positive infinity.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var cceilnf = require( '@stdlib/math/base/special/cceilnf' );
31+
```
32+
33+
#### cceilnf( z, n )
34+
35+
Rounds each component of a single-precision complex floating-point number to the nearest multiple of `10^n` toward positive infinity.
36+
37+
```javascript
38+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
39+
var real = require( '@stdlib/complex/float32/real' );
40+
var imag = require( '@stdlib/complex/float32/imag' );
41+
42+
// Round components to 2 decimal places:
43+
var z = new Complex64(-3.1415927, 3.1415927)
44+
var v = cceilnf( z, -2 );
45+
// returns <Complex128>
46+
47+
var re = real( v );
48+
// returns -3.14
49+
50+
var im = imag( v );
51+
// returns 3.15
52+
53+
// If n = 0, `cceilnf` behaves like `cceilf`:
54+
z = new Complex64( 9.99999, 0.1 );
55+
v = cceilnf( z, 0 );
56+
// returns <Complex64>
57+
58+
re = real( v );
59+
// returns 10.0
60+
61+
im = imag( v );
62+
// returns 1.0
63+
64+
// Round components to the nearest thousand:
65+
z = new Complex64( 12368.0, -12368.0 );
66+
v = cceilnf( z, 3 );
67+
// returns <Complex64>
68+
69+
re = real( v );
70+
// returns 13000.0
71+
72+
im = imag( v );
73+
// returns -12000.0
74+
75+
v = cceilnf( new Complex64( NaN, NaN ), 2 );
76+
// returns <Complex64>
77+
78+
re = real( v );
79+
// returns NaN
80+
81+
im = imag( v );
82+
// returns NaN
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. For example,
94+
95+
```javascript
96+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
97+
var real = require( '@stdlib/complex/float32/real' );
98+
var imag = require( '@stdlib/complex/float32/imag' );
99+
100+
var x = 0.2 + 0.1;
101+
// returns 0.3
102+
103+
// Should round components to 0.3:
104+
var v = cceilnf( new Complex64( x, x ), -16 );
105+
// returns <Complex64>
106+
107+
var re = real( v );
108+
// returns 0.3
109+
110+
var im = imag( v );
111+
// returns 0.3
112+
```
113+
114+
</section>
115+
116+
<!-- /.notes -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint no-undef: "error" -->
123+
124+
```javascript
125+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
126+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
127+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
128+
var cceilnf = require( '@stdlib/math/base/special/cceilnf' );
129+
130+
var rand1 = uniform( -50.0, 50.0 );
131+
var rand2 = discreteUniform( -5.0, 0.0 );
132+
133+
var z;
134+
var i;
135+
var n;
136+
for ( i = 0; i < 100; i++ ) {
137+
n = rand2();
138+
z = new Complex64( rand1(), rand1() );
139+
console.log( 'cceilnf(%s, %s) = %s', z, n, cceilnf( z, n ) );
140+
}
141+
```
142+
143+
</section>
144+
145+
<!-- /.examples -->
146+
147+
<!-- C interface documentation. -->
148+
149+
* * *
150+
151+
<section class="c">
152+
153+
## C APIs
154+
155+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
156+
157+
<section class="intro">
158+
159+
</section>
160+
161+
<!-- /.intro -->
162+
163+
<!-- C usage documentation. -->
164+
165+
<section class="usage">
166+
167+
### Usage
168+
169+
```c
170+
#include "stdlib/math/base/special/cceiln.h"
171+
```
172+
173+
#### stdlib_base_cceilnf( z, n )
174+
175+
Rounds each component of a single-precision complex floating-point number to the nearest multiple of `10^n` toward positive infinity.
176+
177+
```c
178+
#include "stdlib/complex/float32/ctor.h"
179+
#include "stdlib/complex/float32/real.h"
180+
#include "stdlib/complex/float32/imag.h"
181+
182+
stdlib_complex64_t z = stdlib_complex64( -3.1415927, 3.1415927 );
183+
184+
stdlib_complex64_t out = stdlib_base_cceilnf( z, -2 );
185+
186+
float re = stdlib_complex64_real( out );
187+
// returns -3.14
188+
189+
float im = stdlib_complex64_imag( out );
190+
// returns 3.15
191+
```
192+
193+
The function accepts the following arguments:
194+
195+
- **z**: `[in] stdlib_complex64_t` input value.
196+
- **n**: `[in] int32_t` integer power of 10.
197+
198+
```c
199+
stdlib_complex64_t stdlib_base_cceilnf( const stdlib_complex64_t z, int32_t n );
200+
```
201+
202+
</section>
203+
204+
<!-- /.usage -->
205+
206+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207+
208+
<section class="notes">
209+
210+
</section>
211+
212+
<!-- /.notes -->
213+
214+
<!-- C API usage examples. -->
215+
216+
<section class="examples">
217+
218+
### Examples
219+
220+
```c
221+
#include "stdlib/math/base/special/cceilnf.h"
222+
#include "stdlib/complex/float32/ctor.h"
223+
#include "stdlib/complex/float32/reim.h"
224+
#include <stdio.h>
225+
226+
int main() {
227+
const stdlib_complex64_t x[] = {
228+
stdlib_complex64( 3.14, 1.5 ),
229+
stdlib_complex64( -3.14, -1.5 ),
230+
stdlib_complex64( 0.0, 0.0 ),
231+
stdlib_complex64( 0.0/0.0, 0.0/0.0 )
232+
};
233+
234+
stdlib_complex64_t v;
235+
stdlib_complex64_t y;
236+
float re1;
237+
float im1;
238+
float re2;
239+
float im2;
240+
int i;
241+
for ( i = 0; i < 4; i++ ) {
242+
v = x[ i ];
243+
y = stdlib_base_cceilnf( v, -2 );
244+
stdlib_complex64_reim( v, &re1, &im1 );
245+
stdlib_complex64_reim( y, &re2, &im2 );
246+
printf( "cceilnf(%f + %fi, -2) = %f + %fi\n", re1, im1, re2, im2 );
247+
}
248+
}
249+
```
250+
251+
</section>
252+
253+
<!-- /.examples -->
254+
255+
</section>
256+
257+
<!-- /.c -->
258+
259+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
260+
261+
<section class="related">
262+
263+
* * *
264+
265+
## See Also
266+
267+
- <span class="package-name">[`@stdlib/math/base/special/cceilf`][@stdlib/math/base/special/cceilf]</span><span class="delimiter">: </span><span class="description">round each component of a single-precision complex floating-point number toward positive infinity.</span>
268+
- <span class="package-name">[`@stdlib/math/base/special/cfloorn`][@stdlib/math/base/special/cfloorn]</span><span class="delimiter">: </span><span class="description">round each component of a double-precision complex floating-point number to the nearest multiple of 10^n toward negative infinity.</span>
269+
- <span class="package-name">[`@stdlib/math/base/special/croundn`][@stdlib/math/base/special/croundn]</span><span class="delimiter">: </span><span class="description">round each component of a double-precision complex floating-point number to the nearest multiple of 10^n.</span>
270+
271+
</section>
272+
273+
<!-- /.related -->
274+
275+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
276+
277+
<section class="links">
278+
279+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
280+
281+
<!-- <related-links> -->
282+
283+
[@stdlib/math/base/special/cceilf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cceilf
284+
285+
[@stdlib/math/base/special/cfloorn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cfloorn
286+
287+
[@stdlib/math/base/special/croundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/croundn
288+
289+
<!-- </related-links> -->
290+
291+
</section>
292+
293+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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/base/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
27+
var real = require( '@stdlib/complex/float32/real' );
28+
var imag = require( '@stdlib/complex/float32/imag' );
29+
var pkg = require( './../package.json' ).name;
30+
var cceilnf = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var values;
37+
var y;
38+
var i;
39+
40+
values = [
41+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
42+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = cceilnf( values[ i % values.length ], -2 );
48+
if ( isnan( real( y ) ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnan( imag( y ) ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});

0 commit comments

Comments
 (0)