Skip to content

Commit c896dc1

Browse files
add math/base/assert/is-probabilityf
1 parent 04fda1b commit c896dc1

File tree

24 files changed

+1890
-0
lines changed

24 files changed

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