|
| 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 --> |
0 commit comments