Skip to content

Commit 6556a46

Browse files
authored
feat: add math/base/special/cfloorf
PR-URL: #3058 Closes: #649 Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Gunj Joshi <[email protected]>
1 parent 7fd112c commit 6556a46

File tree

24 files changed

+2058
-0
lines changed

24 files changed

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