Skip to content

Commit 140b517

Browse files
feat: add math/base/assert/is-integerf
PR-URL: #2762 --------- Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 2837436 commit 140b517

File tree

24 files changed

+1931
-0
lines changed

24 files changed

+1931
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
# isIntegerf
22+
23+
> Test if a finite [single-precision floating-point number][ieee754] is an integer.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
31+
```
32+
33+
#### isIntegerf( x )
34+
35+
Tests if a finite [single-precision floating-point number][ieee754] is an integer.
36+
37+
```javascript
38+
var bool = isIntegerf( 1.0 );
39+
// returns true
40+
```
41+
42+
</section>
43+
44+
<!-- /.usage -->
45+
46+
<section class="notes">
47+
48+
## Notes
49+
50+
- 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:
51+
52+
```javascript
53+
function check( x ) {
54+
return (
55+
x < Infinity &&
56+
x > -Infinity &&
57+
isIntegerf( x )
58+
);
59+
}
60+
61+
var bool = check( Infinity );
62+
// returns false
63+
64+
bool = check( -Infinity );
65+
// returns false
66+
```
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
80+
81+
var bool = isIntegerf( -5.0 );
82+
// returns true
83+
84+
bool = isIntegerf( 3.14 );
85+
// returns false
86+
87+
bool = isIntegerf( NaN );
88+
// returns false
89+
```
90+
91+
</section>
92+
93+
<!-- /.examples -->
94+
95+
<!-- C interface documentation. -->
96+
97+
* * *
98+
99+
<section class="c">
100+
101+
## C APIs
102+
103+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
104+
105+
<section class="intro">
106+
107+
</section>
108+
109+
<!-- /.intro -->
110+
111+
<!-- C usage documentation. -->
112+
113+
<section class="usage">
114+
115+
### Usage
116+
117+
```c
118+
#include "stdlib/math/base/assert/is_integerf.h"
119+
```
120+
121+
#### stdlib_base_is_integerf( x )
122+
123+
Tests if a finite [single-precision floating-point number][ieee754] is an integer.
124+
125+
```c
126+
bool out = stdlib_base_is_integerf( 1.0 );
127+
// returns true
128+
129+
out = stdlib_base_is_integerf( 3.14 );
130+
// returns false
131+
```
132+
133+
The function accepts the following arguments:
134+
135+
- **x**: `[in] float` input value.
136+
137+
```c
138+
bool stdlib_base_is_integerf( const float x );
139+
```
140+
141+
</section>
142+
143+
<!-- /.usage -->
144+
145+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="notes">
148+
149+
</section>
150+
151+
<!-- /.notes -->
152+
153+
<!-- C API usage examples. -->
154+
155+
<section class="examples">
156+
157+
### Examples
158+
159+
```c
160+
#include "stdlib/math/base/assert/is_integerf.h"
161+
#include <stdio.h>
162+
#include <stdlib.h>
163+
#include <stdbool.h>
164+
165+
int main( void ) {
166+
float x;
167+
bool v;
168+
int i;
169+
170+
for ( i = 0; i < 100; i++ ) {
171+
x = ( ( (float)rand() / (float)RAND_MAX ) * 100.0f ) - 50.0f;
172+
v = stdlib_base_is_integerf( x );
173+
printf( "x = %f, is_integerf(x) = %s\n", x, ( v ) ? "true" : "false" );
174+
}
175+
}
176+
```
177+
178+
</section>
179+
180+
<!-- /.examples -->
181+
182+
</section>
183+
184+
<!-- /.c -->
185+
186+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
187+
188+
<section class="related">
189+
190+
</section>
191+
192+
<!-- /.related -->
193+
194+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
195+
196+
<section class="links">
197+
198+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
199+
200+
</section>
201+
202+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var isIntegerf = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg+'::true', function benchmark( b ) {
32+
var values;
33+
var bool;
34+
var i;
35+
36+
values = [
37+
0,
38+
1,
39+
2,
40+
3,
41+
4,
42+
5,
43+
6,
44+
7,
45+
8,
46+
9,
47+
1.0e38,
48+
10,
49+
-0,
50+
-1,
51+
-2,
52+
-3,
53+
-4,
54+
-5,
55+
-6,
56+
-7,
57+
-8,
58+
-9,
59+
-10,
60+
-1.0e38
61+
];
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
bool = isIntegerf( values[ i % values.length ] );
66+
if ( typeof bool !== 'boolean' ) {
67+
b.fail( 'should return a boolean' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isBoolean( bool ) && bool !== true ) {
72+
b.fail( 'should return true' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
77+
78+
bench( pkg+'::false', function benchmark( b ) {
79+
var values;
80+
var bool;
81+
var i;
82+
83+
values = [
84+
0.1,
85+
1.1,
86+
2.1,
87+
3.1,
88+
4.1,
89+
5.1,
90+
6.1,
91+
7.1,
92+
8.1,
93+
9.1,
94+
10.1,
95+
-0.1,
96+
-1.1,
97+
-2.1,
98+
-3.1,
99+
-4.1,
100+
-5.1,
101+
-6.1,
102+
-7.1,
103+
-8.1,
104+
-9.1,
105+
-10.1
106+
];
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
bool = isIntegerf( values[ i % values.length ] );
111+
if ( typeof bool !== 'boolean' ) {
112+
b.fail( 'should return a boolean' );
113+
}
114+
}
115+
b.toc();
116+
if ( !isBoolean( bool ) && bool !== false ) {
117+
b.fail( 'should return false' );
118+
}
119+
b.pass( 'benchmark finished' );
120+
b.end();
121+
});

0 commit comments

Comments
 (0)