Skip to content

Commit d125530

Browse files
authored
feat: add math/base/assert/is-nonnegative-integerf
PR-URL: #2851 Ref: #649 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent b0f3086 commit d125530

File tree

24 files changed

+1898
-0
lines changed

24 files changed

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