Skip to content

Commit 98a7ef6

Browse files
committed
feat: add cfloorf
1 parent f2a7164 commit 98a7ef6

File tree

24 files changed

+2041
-0
lines changed

24 files changed

+2041
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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 randu = require( '@stdlib/random/base/randu' );
92+
var cfloorf = require( '@stdlib/math/base/special/cfloorf' );
93+
94+
var re;
95+
var im;
96+
var z;
97+
var w;
98+
var i;
99+
100+
for ( i = 0; i < 100; i++ ) {
101+
re = ( randu()*100.0 ) - 50.0;
102+
im = ( randu()*100.0 ) - 50.0;
103+
z = new Complex64( re, im );
104+
w = cfloorf( z );
105+
console.log( 'floor(%s) = %s', z.toString(), w.toString() );
106+
}
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- C interface documentation. -->
114+
115+
* * *
116+
117+
<section class="c">
118+
119+
## C APIs
120+
121+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
122+
123+
<section class="intro">
124+
125+
</section>
126+
127+
<!-- /.intro -->
128+
129+
<!-- C usage documentation. -->
130+
131+
<section class="usage">
132+
133+
### Usage
134+
135+
```c
136+
#include "stdlib/math/base/special/cfloorf.h"
137+
```
138+
139+
#### stdlib_base_cfloorf( z )
140+
141+
Rounds a single-precision complex floating-point number toward negative infinity.
142+
143+
```c
144+
#include "stdlib/complex/float32/ctor.h"
145+
#include "stdlib/complex/float32/real.h"
146+
#include "stdlib/complex/float32/imag.h"
147+
148+
stdlib_complex64_t z = stdlib_complex64( 2.5, -1.5 );
149+
150+
stdlib_complex64_t out = stdlib_base_cfloorf( z );
151+
152+
float re = stdlib_complex64_real( out );
153+
// returns 2.0
154+
155+
float im = stdlib_complex64_imag( out );
156+
// returns -2.0
157+
```
158+
159+
The function accepts the following arguments:
160+
161+
- **z**: `[in] stdlib_complex64_t` input value.
162+
163+
```c
164+
stdlib_complex64_t stdlib_base_cfloorf( const stdlib_complex64_t z );
165+
```
166+
167+
</section>
168+
169+
<!-- /.usage -->
170+
171+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="notes">
174+
175+
</section>
176+
177+
<!-- /.notes -->
178+
179+
<!-- C API usage examples. -->
180+
181+
<section class="examples">
182+
183+
### Examples
184+
185+
```c
186+
#include "stdlib/math/base/special/cfloorf.h"
187+
#include "stdlib/complex/float32/ctor.h"
188+
#include "stdlib/complex/float32/reim.h"
189+
#include <stdio.h>
190+
191+
int main( void ) {
192+
const stdlib_complex64_t x[] = {
193+
stdlib_complex64( 3.14, 1.5 ),
194+
stdlib_complex64( -3.14, -1.5 ),
195+
stdlib_complex64( 0.0, 0.0 ),
196+
stdlib_complex64( 0.0/0.0, 0.0/0.0 )
197+
};
198+
199+
stdlib_complex64_t v;
200+
stdlib_complex64_t y;
201+
float re1;
202+
float im1;
203+
float re2;
204+
float im2;
205+
int i;
206+
for ( i = 0; i < 4; i++ ) {
207+
v = x[ i ];
208+
y = stdlib_base_cfloorf( v );
209+
stdlib_complex64_reim( v, &re1, &im1 );
210+
stdlib_complex64_reim( y, &re2, &im2 );
211+
printf( "cfloorf(%f + %fi) = %f + %fi\n", re1, im1, re2, im2 );
212+
}
213+
}
214+
```
215+
216+
</section>
217+
218+
<!-- /.examples -->
219+
220+
</section>
221+
222+
<!-- /.c -->
223+
224+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
225+
226+
<section class="related">
227+
228+
* * *
229+
230+
## See Also
231+
232+
- <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>
233+
- <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>
234+
- <span class="package-name">[`@stdlib/math/base/special/cround`][@stdlib/math/base/special/cround]</span><span class="delimiter">: </span><span class="description">round each component of a double-precision complex floating-point number to the nearest integer.</span>
235+
236+
</section>
237+
238+
<!-- /.related -->
239+
240+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
241+
242+
<section class="links">
243+
244+
<!-- <related-links> -->
245+
246+
[@stdlib/math/base/special/cceilf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cceilf
247+
248+
[@stdlib/math/base/special/cfloorn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cfloorn
249+
250+
[@stdlib/math/base/special/cround]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cround
251+
252+
<!-- </related-links> -->
253+
254+
</section>
255+
256+
<!-- /.links -->
Lines changed: 84 additions & 0 deletions
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 isnan = 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 floor = 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 ( isnan( real( y ) ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnan( 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 = [ floor( re ), floor( 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+
});

0 commit comments

Comments
 (0)