Skip to content

Commit d9a93be

Browse files
feat: add math/base/special/croundf
PR-URL: #3061 Closes: #649 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Gururaj Gurram <[email protected]>
1 parent 824d519 commit d9a93be

File tree

26 files changed

+2248
-0
lines changed

26 files changed

+2248
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
# croundf
22+
23+
> Round each component of a single-precision complex floating-point number to the nearest integer.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var croundf = require( '@stdlib/math/base/special/croundf' );
31+
```
32+
33+
#### croundf( z )
34+
35+
Rounds each component of a single-precision complex floating-point number to the nearest integer.
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+
var v = croundf( new Complex64( -4.2, 5.5 ) );
43+
// returns <Complex64>
44+
45+
var re = real( v );
46+
// returns -4.0
47+
48+
var im = imag( v );
49+
// returns 6.0
50+
51+
v = croundf( new Complex64( 9.99999, 0.1 ) );
52+
// returns <Complex64>
53+
54+
re = real( v );
55+
// returns 10.0
56+
57+
im = imag( v );
58+
// returns 0.0
59+
60+
v = croundf( new Complex64( 0.0, 0.0 ) );
61+
// returns <Complex64>
62+
63+
re = real( v );
64+
// returns 0.0
65+
66+
im = imag( v );
67+
// returns 0.0
68+
69+
v = croundf( new Complex64( NaN, NaN ) );
70+
// returns <Complex64>
71+
72+
re = real( v );
73+
// returns NaN
74+
75+
im = imag( v );
76+
// returns NaN
77+
```
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
91+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
92+
var croundf = require( '@stdlib/math/base/special/croundf' );
93+
94+
var rand = uniform( -50.0, 50.0 );
95+
96+
var z;
97+
var i;
98+
for ( i = 0; i < 100; i++ ) {
99+
z = new Complex64( rand(), rand() );
100+
console.log( 'croundf(%s) = %s', z, croundf( z ) );
101+
}
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- C interface documentation. -->
109+
110+
* * *
111+
112+
<section class="c">
113+
114+
## C APIs
115+
116+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
117+
118+
<section class="intro">
119+
120+
</section>
121+
122+
<!-- /.intro -->
123+
124+
<!-- C usage documentation. -->
125+
126+
<section class="usage">
127+
128+
### Usage
129+
130+
```c
131+
#include "stdlib/math/base/special/croundf.h"
132+
```
133+
134+
#### stdlib_base_croundf( z )
135+
136+
Rounds each component of a single-precision complex floating-point number to the nearest integer.
137+
138+
```c
139+
#include "stdlib/complex/float32/ctor.h"
140+
#include "stdlib/complex/float32/real.h"
141+
#include "stdlib/complex/float32/imag.h"
142+
143+
stdlib_complex64_t z = stdlib_complex64( -4.2, 5.5 );
144+
145+
stdlib_complex64_t out = stdlib_base_croundf( z );
146+
147+
float re = stdlib_complex64_real( out );
148+
// returns -4.0
149+
150+
float im = stdlib_complex64_imag( out );
151+
// returns 6.0
152+
```
153+
154+
The function accepts the following arguments:
155+
156+
- **z**: `[in] stdlib_complex64_t` input value.
157+
158+
```c
159+
stdlib_complex64_t stdlib_base_croundf( const stdlib_complex64_t z );
160+
```
161+
162+
</section>
163+
164+
<!-- /.usage -->
165+
166+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
167+
168+
<section class="notes">
169+
170+
</section>
171+
172+
<!-- /.notes -->
173+
174+
<!-- C API usage examples. -->
175+
176+
<section class="examples">
177+
178+
### Examples
179+
180+
```c
181+
#include "stdlib/math/base/special/croundf.h"
182+
#include "stdlib/complex/float32/ctor.h"
183+
#include "stdlib/complex/float32/reim.h"
184+
#include <stdio.h>
185+
186+
int main( void ) {
187+
const stdlib_complex64_t x[] = {
188+
stdlib_complex64( 3.14, 1.5 ),
189+
stdlib_complex64( -3.14, -1.5 ),
190+
stdlib_complex64( 0.0, 0.0 ),
191+
stdlib_complex64( 0.0/0.0, 0.0/0.0 )
192+
};
193+
194+
stdlib_complex64_t v;
195+
stdlib_complex64_t y;
196+
float re1;
197+
float im1;
198+
float re2;
199+
float im2;
200+
int i;
201+
for ( i = 0; i < 4; i++ ) {
202+
v = x[ i ];
203+
y = stdlib_base_croundf( v );
204+
stdlib_complex64_reim( v, &re1, &im1 );
205+
stdlib_complex64_reim( y, &re2, &im2 );
206+
printf( "croundf(%f + %fi) = %f + %fi\n", re1, im1, re2, im2 );
207+
}
208+
}
209+
```
210+
211+
</section>
212+
213+
<!-- /.examples -->
214+
215+
</section>
216+
217+
<!-- /.c -->
218+
219+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
220+
221+
<section class="related">
222+
223+
* * *
224+
225+
## See Also
226+
227+
- <span class="package-name">[`@stdlib/math/base/special/cceilf`][@stdlib/math/base/special/cceilf]</span><span class="delimiter">: </span><span class="description">round a single-precision complex floating-point number toward positive infinity.</span>
228+
- <span class="package-name">[`@stdlib/math/base/special/cfloor`][@stdlib/math/base/special/cfloor]</span><span class="delimiter">: </span><span class="description">round a double-precision complex floating-point number toward negative infinity.</span>
229+
- <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>
230+
231+
</section>
232+
233+
<!-- /.related -->
234+
235+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
236+
237+
<section class="links">
238+
239+
<!-- <related-links> -->
240+
241+
[@stdlib/math/base/special/cceilf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cceilf
242+
243+
[@stdlib/math/base/special/cfloor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cfloor
244+
245+
[@stdlib/math/base/special/croundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/croundn
246+
247+
<!-- </related-links> -->
248+
249+
</section>
250+
251+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 uniform = require( '@stdlib/random/base/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 croundf = 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 = croundf( values[ i%values.length ] );
48+
if ( isnanf( real( y ) ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnanf( imag( y ) ) ) {
54+
b.fail( 'should not return not NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 uniform = require( '@stdlib/random/base/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
28+
var real = require( '@stdlib/complex/float32/real' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var croundf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( croundf instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var values;
45+
var y;
46+
var i;
47+
48+
values = [
49+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
50+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
51+
];
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
y = croundf( values[ i%values.length ] );
56+
if ( isnanf( real( y ) ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnanf( real( y ) ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});

0 commit comments

Comments
 (0)