Skip to content

Commit 20b4721

Browse files
vivekmaurya001gunjjoshistdlib-bot
authored
feat: add math/base/assert/is-probabilityf
PR-URL: #4214 Co-authored-by: Gunj Joshi <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Signed-off-by: Gunj Joshi <[email protected]>
1 parent ea9e425 commit 20b4721

File tree

24 files changed

+1850
-0
lines changed

24 files changed

+1850
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# isProbabilityf
22+
23+
> Test if a single-precision floating-point number is a probability.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isProbabilityf = require( '@stdlib/math/base/assert/is-probabilityf' );
31+
```
32+
33+
#### isProbabilityf( x )
34+
35+
Tests if a single-precision floating-point number is a probability.
36+
37+
```javascript
38+
var bool = isProbabilityf( 0.5 );
39+
// returns true
40+
41+
bool = isProbabilityf( 3.14 );
42+
// returns false
43+
44+
bool = isProbabilityf( NaN );
45+
// returns false
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var uniform = require( '@stdlib/random/array/uniform' );
60+
var isProbabilityf = require( '@stdlib/math/base/assert/is-probabilityf' );
61+
62+
var bool;
63+
var opts;
64+
var x;
65+
var i;
66+
67+
opts = {
68+
'dtype': 'float32'
69+
};
70+
x = uniform( 100, -1.0, 1.0, opts );
71+
72+
for ( i = 0; i < 100; i++ ) {
73+
bool = isProbabilityf( x[ i ] );
74+
console.log( '%d is %s', x[ i ], ( bool ) ? 'a probability' : 'not a probability' );
75+
}
76+
```
77+
78+
</section>
79+
80+
<!-- /.examples -->
81+
82+
<!-- C interface documentation. -->
83+
84+
* * *
85+
86+
<section class="c">
87+
88+
## C APIs
89+
90+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
91+
92+
<section class="intro">
93+
94+
</section>
95+
96+
<!-- /.intro -->
97+
98+
<!-- C usage documentation. -->
99+
100+
<section class="usage">
101+
102+
### Usage
103+
104+
```c
105+
#include "stdlib/math/base/assert/is_probabilityf.h"
106+
```
107+
108+
#### stdlib_base_is_probabilityf( x )
109+
110+
Tests if a single-precision floating-point number is a probability.
111+
112+
```c
113+
bool out = stdlib_base_is_probabilityf( 0.5f );
114+
// returns true
115+
116+
out = stdlib_base_is_probabilityf( 3.14f );
117+
// returns false
118+
```
119+
120+
The function accepts the following arguments:
121+
122+
- **x**: `[in] float` input value.
123+
124+
```c
125+
bool stdlib_base_is_probabilityf( const float x );
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="notes">
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<!-- C API usage examples. -->
141+
142+
<section class="examples">
143+
144+
### Examples
145+
146+
```c
147+
#include "stdlib/math/base/assert/is_probabilityf.h"
148+
#include <stdio.h>
149+
#include <stdlib.h>
150+
#include <stdbool.h>
151+
152+
int main( void ) {
153+
float x;
154+
bool v;
155+
int i;
156+
157+
for ( i = 0; i < 100; i++ ) {
158+
x = ( ( (float)rand() / (float)RAND_MAX ) * 2.0f ) - 1.0f;
159+
v = stdlib_base_is_probabilityf( x );
160+
printf( "%f is %sa probability\n", x, ( v ) ? "" : "not " );
161+
}
162+
}
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
</section>
170+
171+
<!-- /.c -->
172+
173+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
174+
175+
<section class="related">
176+
177+
</section>
178+
179+
<!-- /.related -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
</section>
186+
187+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var isProbabilityf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var len;
35+
var x;
36+
var y;
37+
var i;
38+
39+
len = 100;
40+
opts = {
41+
'dtype': 'float32'
42+
};
43+
x = uniform( len, -1.0, 1.0, opts );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = isProbabilityf( x[ i % len ] );
48+
if ( typeof y !== 'boolean' ) {
49+
b.fail( 'should return a boolean' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isBoolean( y ) ) {
54+
b.fail( 'should return a boolean' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/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 isProbabilityf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( isProbabilityf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var opts;
43+
var len;
44+
var x;
45+
var y;
46+
var i;
47+
48+
len = 100;
49+
opts = {
50+
'dtype': 'float32'
51+
};
52+
x = uniform( len, -1.0, 1.0, opts );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
y = isProbabilityf( x[ i % len ] );
57+
if ( typeof y !== 'boolean' ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isBoolean( y ) ) {
63+
b.fail( 'should return a boolean' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
 (0)