Skip to content

Commit c664da7

Browse files
authored
feat: add math/base/assert/is-evenf
PR-URL: #3077 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 893cb1b commit c664da7

File tree

24 files changed

+1896
-0
lines changed

24 files changed

+1896
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
# isEvenf
22+
23+
> Test if a finite single-precision floating-point number is an even number.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isEvenf = require( '@stdlib/math/base/assert/is-evenf' );
31+
```
32+
33+
#### isEvenf( x )
34+
35+
Tests if a **finite** single-precision floating-point number is an even number.
36+
37+
```javascript
38+
var bool = isEvenf( 5.0 );
39+
// returns false
40+
41+
bool = isEvenf( -2.0 );
42+
// returns true
43+
44+
bool = isEvenf( 0.0 );
45+
// returns true
46+
47+
bool = isEvenf( NaN );
48+
// returns false
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="notes">
56+
57+
## Notes
58+
59+
- The function assumes a **finite** `number`. If provided positive or negative `infinity`, the function will return `true`, when, in fact, the result is undefined. If `x` can be `infinite`, wrap the implementation as follows:
60+
61+
```javascript
62+
function check( x ) {
63+
return (
64+
x < Infinity &&
65+
x > -Infinity &&
66+
isEvenf( x )
67+
);
68+
}
69+
70+
var bool = check( Infinity );
71+
// returns false
72+
73+
bool = check( -Infinity );
74+
// returns false
75+
```
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var randu = require( '@stdlib/random/array/discrete-uniform' );
89+
var isEvenf = require( '@stdlib/math/base/assert/is-evenf' );
90+
91+
var bool;
92+
var x;
93+
var i;
94+
95+
x = randu( 100, 0, 100 );
96+
97+
for ( i = 0; i < 100; i++ ) {
98+
bool = isEvenf( x[ i ] );
99+
console.log( '%d is %s', x[ i ], ( bool ) ? 'even' : 'not even' );
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/assert/is_evenf.h"
131+
```
132+
133+
#### stdlib_base_is_evenf( x )
134+
135+
Tests if a finite single-precision floating-point number is an even number.
136+
137+
```c
138+
bool out = stdlib_base_is_evenf( 1.0f );
139+
// returns false
140+
141+
out = stdlib_base_is_evenf( 4.0f );
142+
// returns true
143+
```
144+
145+
The function accepts the following arguments:
146+
147+
- **x**: `[in] float` input value.
148+
149+
```c
150+
bool stdlib_base_is_evenf( const float x );
151+
```
152+
153+
</section>
154+
155+
<!-- /.usage -->
156+
157+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
158+
159+
<section class="notes">
160+
161+
</section>
162+
163+
<!-- /.notes -->
164+
165+
<!-- C API usage examples. -->
166+
167+
<section class="examples">
168+
169+
### Examples
170+
171+
```c
172+
#include <stdio.h>
173+
#include <stdlib.h>
174+
#include <stdbool.h>
175+
176+
int main( void ) {
177+
float x;
178+
bool v;
179+
int i;
180+
181+
for ( i = 0; i < 100; i++ ) {
182+
x = ( ( (float)rand() / (float)RAND_MAX ) * 100.0f );
183+
v = stdlib_base_is_evenf( x );
184+
printf( "x = %f, is_evenf(x) = %s\n", x, ( v ) ? "even" : "not even" );
185+
}
186+
}
187+
```
188+
189+
</section>
190+
191+
<!-- /.examples -->
192+
193+
</section>
194+
195+
<!-- /.c -->
196+
197+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
198+
199+
<section class="related">
200+
201+
<!-- /.related -->
202+
203+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
204+
205+
<section class="links">
206+
207+
<!-- <related-links> -->
208+
209+
<!-- </related-links> -->
210+
211+
</section>
212+
213+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/array/discrete-uniform' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var isEvenf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = randu( 100, -50, 50 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = isEvenf( x[ i % 100 ] );
42+
if ( typeof y !== 'boolean' ) {
43+
b.fail( 'should return a boolean' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isBoolean( y ) ) {
48+
b.fail( 'should return a boolean' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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/array/discrete-uniform' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var isEvenf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( isEvenf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg, opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = randu( 100, -50, 50 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = isEvenf( x[ i % 100 ] );
51+
if ( typeof y !== 'boolean' ) {
52+
b.fail( 'should return a boolean' );
53+
}
54+
}
55+
b.toc();
56+
if ( !isBoolean( y ) ) {
57+
b.fail( 'should return a boolean' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)