Skip to content

Commit

Permalink
feat: add math/base/assert/is-integerf
Browse files Browse the repository at this point in the history
PR-URL: #2762

---------

Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
gunjjoshi and Planeshifter authored Aug 8, 2024
1 parent 2837436 commit 140b517
Show file tree
Hide file tree
Showing 24 changed files with 1,931 additions and 0 deletions.
202 changes: 202 additions & 0 deletions lib/node_modules/@stdlib/math/base/assert/is-integerf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# isIntegerf

> Test if a finite [single-precision floating-point number][ieee754] is an integer.
<section class="usage">

## Usage

```javascript
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
```

#### isIntegerf( x )

Tests if a finite [single-precision floating-point number][ieee754] is an integer.

```javascript
var bool = isIntegerf( 1.0 );
// returns true
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- 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:

```javascript
function check( x ) {
return (
x < Infinity &&
x > -Infinity &&
isIntegerf( x )
);
}

var bool = check( Infinity );
// returns false

bool = check( -Infinity );
// returns false
```

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var isIntegerf = require( '@stdlib/math/base/assert/is-integerf' );
var bool = isIntegerf( -5.0 );
// returns true
bool = isIntegerf( 3.14 );
// returns false
bool = isIntegerf( NaN );
// returns false
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/assert/is_integerf.h"
```

#### stdlib_base_is_integerf( x )

Tests if a finite [single-precision floating-point number][ieee754] is an integer.

```c
bool out = stdlib_base_is_integerf( 1.0 );

This comment has been minimized.

Copy link
@kgryte

kgryte Aug 8, 2024

Member

@gunjjoshi We need to add #include <stdbool.h> here.

// returns true
out = stdlib_base_is_integerf( 3.14 );
// returns false
```

The function accepts the following arguments:

- **x**: `[in] float` input value.

```c
bool stdlib_base_is_integerf( const float x );
```
</section>
<!-- /.usage -->
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- C API usage examples. -->
<section class="examples">
### Examples
```c
#include "stdlib/math/base/assert/is_integerf.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main( void ) {
float x;
bool v;
int i;

for ( i = 0; i < 100; i++ ) {
x = ( ( (float)rand() / (float)RAND_MAX ) * 100.0f ) - 50.0f;
v = stdlib_base_is_integerf( x );
printf( "x = %f, is_integerf(x) = %s\n", x, ( v ) ? "true" : "false" );
}
}
```
</section>
<!-- /.examples -->
</section>
<!-- /.c -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
</section>
<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var isIntegerf = require( './../lib' );


// MAIN //

bench( pkg+'::true', function benchmark( b ) {
var values;
var bool;
var i;

values = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
1.0e38,
10,
-0,
-1,
-2,
-3,
-4,
-5,
-6,
-7,
-8,
-9,
-10,
-1.0e38
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = isIntegerf( values[ i % values.length ] );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) && bool !== true ) {

This comment has been minimized.

Copy link
@kgryte

kgryte Aug 8, 2024

Member

@gunjjoshi I think this is incorrect. Instead of &&, it should be ||. If bool is not a boolean, the benchmark should fail OR if bool is not true, the benchmark should fail. Currently, this assertion can never be true. There is no circumstance in which bool is not a boolean and equal to true. Should be fixed below, as well, and we may need to fix in is-integer.

b.fail( 'should return true' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::false', function benchmark( b ) {
var values;
var bool;
var i;

values = [
0.1,
1.1,
2.1,
3.1,
4.1,
5.1,
6.1,
7.1,
8.1,
9.1,
10.1,
-0.1,
-1.1,
-2.1,
-3.1,
-4.1,
-5.1,
-6.1,
-7.1,
-8.1,
-9.1,
-10.1
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = isIntegerf( values[ i % values.length ] );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) && bool !== false ) {
b.fail( 'should return false' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading

1 comment on commit 140b517

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/base/assert/is-integerf $\color{green}145/145$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}145/145$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.