Skip to content

Commit ecd017d

Browse files
gururaj1512gunjjoshistdlib-bot
authored
feat: add math/base/assert/is-oddf
PR-URL: #3125 Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com>
1 parent d7dc119 commit ecd017d

File tree

24 files changed

+1858
-0
lines changed

24 files changed

+1858
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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+
# isOddf
22+
23+
> Test if a finite single-precision floating-point number is an odd number.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isOddf = require( '@stdlib/math/base/assert/is-oddf' );
31+
```
32+
33+
#### isOddf( x )
34+
35+
Tests if a **finite** single-precision floating-point number is an odd number.
36+
37+
```javascript
38+
var bool = isOddf( 5.0 );
39+
// returns true
40+
41+
bool = isOddf( -2.0 );
42+
// returns false
43+
44+
bool = isOddf( 0.0 );
45+
// returns false
46+
47+
bool = isOddf( 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+
isOddf( 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 isOddf = require( '@stdlib/math/base/assert/is-oddf' );
90+
91+
var x = randu( 100, 0, 100 );
92+
93+
var i;
94+
for ( i = 0; i < 100; i++ ) {
95+
console.log( '%d is %s', x[ i ], ( isOddf( x[ i ] ) ) ? 'odd' : 'not odd' );
96+
}
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- C interface documentation. -->
104+
105+
* * *
106+
107+
<section class="c">
108+
109+
## C APIs
110+
111+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
112+
113+
<section class="intro">
114+
115+
</section>
116+
117+
<!-- /.intro -->
118+
119+
<!-- C usage documentation. -->
120+
121+
<section class="usage">
122+
123+
### Usage
124+
125+
```c
126+
#include "stdlib/math/base/assert/is_oddf.h"
127+
```
128+
129+
#### stdlib_base_is_oddf( x )
130+
131+
Tests if a finite single-precision floating-point number is an odd number.
132+
133+
```c
134+
#include <stdbool.h>
135+
136+
bool out = stdlib_base_is_oddf( 1.0 );
137+
// returns true
138+
139+
out = stdlib_base_is_oddf( 4.0 );
140+
// returns false
141+
```
142+
143+
The function accepts the following arguments:
144+
145+
- **x**: `[in] float` input value.
146+
147+
```c
148+
bool stdlib_base_is_oddf( const float x );
149+
```
150+
151+
</section>
152+
153+
<!-- /.usage -->
154+
155+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
156+
157+
<section class="notes">
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
163+
<!-- C API usage examples. -->
164+
165+
<section class="examples">
166+
167+
### Examples
168+
169+
```c
170+
#include "stdlib/math/base/assert/is_oddf.h"
171+
#include <stdio.h>
172+
#include <stdbool.h>
173+
174+
int main( void ) {
175+
const float x[] = { 5.0f, -5.0f, 3.14f, -3.14f, 0.0f, 0.0f / 0.0f };
176+
177+
bool b;
178+
int i;
179+
for ( i = 0; i < 6; i++ ) {
180+
b = stdlib_base_is_oddf( x[ i ] );
181+
printf( "Value: %f. Is Odd? %s.\n", x[ i ], ( b ) ? "True" : "False" );
182+
}
183+
}
184+
```
185+
186+
</section>
187+
188+
<!-- /.examples -->
189+
190+
</section>
191+
192+
<!-- /.c -->
193+
194+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
195+
196+
<section class="related">
197+
198+
* * *
199+
200+
## See Also
201+
202+
</section>
203+
204+
<!-- /.related -->
205+
206+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207+
208+
<section class="links">
209+
210+
<!-- <related-links> -->
211+
212+
<!-- </related-links> -->
213+
214+
</section>
215+
216+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
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 isOddf = 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 = isOddf( 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+
});
Lines changed: 61 additions & 0 deletions
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 isOddf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( isOddf 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 = isOddf( 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)