|
| 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 | +# wrapf |
| 22 | + |
| 23 | +> Wrap a single-precision floating-point value to the half-open interval `[min,max)`. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var wrapf = require( '@stdlib/math/base/special/wrapf' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### wrapf( v, min, max ) |
| 44 | + |
| 45 | +Wraps a single-precision floating-point value to the half-open interval `[min,max)`. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var v = wrapf( 3.14, 0.0, 5.0 ); |
| 49 | +// returns ~3.14 |
| 50 | + |
| 51 | +v = wrapf( -3.14, 0.0, 5.0 ); |
| 52 | +// returns ~1.86 |
| 53 | + |
| 54 | +v = wrapf( 10.0, 0.0, 5.0 ); |
| 55 | +// returns 0.0 |
| 56 | + |
| 57 | +v = wrapf( -0.0, 0.0, 5.0 ); |
| 58 | +// returns 0.0 |
| 59 | + |
| 60 | +v = wrapf( 0.0, -3.14, -0.0 ); |
| 61 | +// returns ~-3.14 |
| 62 | +``` |
| 63 | + |
| 64 | +If provided `NaN` for any argument, the function returns `NaN`. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var v = wrapf( NaN, 0.0, 5.0 ); |
| 68 | +// returns NaN |
| 69 | + |
| 70 | +v = wrapf( 0.0, NaN, 5.0 ); |
| 71 | +// returns NaN |
| 72 | + |
| 73 | +v = wrapf( 3.14, 0.0, NaN ); |
| 74 | +// returns NaN |
| 75 | +``` |
| 76 | + |
| 77 | +If provided `min == max`, the function returns `NaN`. |
| 78 | + |
| 79 | +```javascript |
| 80 | +var v = wrapf( 3.14, 3.0, 3.0 ); |
| 81 | +// returns NaN |
| 82 | +``` |
| 83 | + |
| 84 | +</section> |
| 85 | + |
| 86 | +<!-- /.usage --> |
| 87 | + |
| 88 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 89 | + |
| 90 | +<section class="notes"> |
| 91 | + |
| 92 | +## Notes |
| 93 | + |
| 94 | +- The function does **not** distinguish between positive and negative zero. Where appropriate, the function returns positive zero. |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.notes --> |
| 99 | + |
| 100 | +<!-- Package usage examples. --> |
| 101 | + |
| 102 | +<section class="examples"> |
| 103 | + |
| 104 | +## Examples |
| 105 | + |
| 106 | +<!-- eslint no-undef: "error" --> |
| 107 | + |
| 108 | +```javascript |
| 109 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 110 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 111 | +var wrapf = require( '@stdlib/math/base/special/wrapf' ); |
| 112 | + |
| 113 | +var opts = { |
| 114 | + 'dtype': 'float32' |
| 115 | +}; |
| 116 | +var min = uniform( 100, 0.0, 10.0, opts ); |
| 117 | +var max = uniform( 100, 5.0, 15.0, opts ); |
| 118 | +var v = uniform( 100, -20.0, 20.0, opts ); |
| 119 | + |
| 120 | +logEachMap( 'wrapf(%0.4f,%0.4f,%0.4f) => %0.4f', v, min, max, wrapf ); |
| 121 | +``` |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.examples --> |
| 126 | + |
| 127 | +<!-- C interface documentation. --> |
| 128 | + |
| 129 | +* * * |
| 130 | + |
| 131 | +<section class="c"> |
| 132 | + |
| 133 | +## C APIs |
| 134 | + |
| 135 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 136 | + |
| 137 | +<section class="intro"> |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.intro --> |
| 142 | + |
| 143 | +<!-- C usage documentation. --> |
| 144 | + |
| 145 | +<section class="usage"> |
| 146 | + |
| 147 | +### Usage |
| 148 | + |
| 149 | +```c |
| 150 | +#include "stdlib/math/base/special/wrapf.h" |
| 151 | +``` |
| 152 | + |
| 153 | +#### stdlib_base_wrapf( v, min, max ) |
| 154 | + |
| 155 | +Wraps a single-precision floating-point value to the half-open interval `[min,max)`. |
| 156 | + |
| 157 | +```c |
| 158 | +float v = stdlib_base_wrapf( 3.14f, 0.0f, 5.0f ); |
| 159 | +// returns ~3.14f |
| 160 | + |
| 161 | +v = stdlib_base_wrapf( -3.14f, 0.0f, 5.0f ); |
| 162 | +// returns ~1.86f |
| 163 | +``` |
| 164 | + |
| 165 | +The function accepts the following arguments: |
| 166 | + |
| 167 | +- **v**: `[in] float` input value to wrap. |
| 168 | +- **min**: `[in] float` minimum value. |
| 169 | +- **max**: `[in] float` maximum value. |
| 170 | + |
| 171 | +```c |
| 172 | +float stdlib_base_wrapf( const float v, const float min, const float max ) |
| 173 | +``` |
| 174 | +
|
| 175 | +</section> |
| 176 | +
|
| 177 | +<!-- /.usage --> |
| 178 | +
|
| 179 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 180 | +
|
| 181 | +<section class="notes"> |
| 182 | +
|
| 183 | +</section> |
| 184 | +
|
| 185 | +<!-- /.notes --> |
| 186 | +
|
| 187 | +<!-- C API usage examples. --> |
| 188 | +
|
| 189 | +<section class="examples"> |
| 190 | +
|
| 191 | +### Examples |
| 192 | +
|
| 193 | +```c |
| 194 | +#include "stdlib/math/base/special/wrapf.h" |
| 195 | +#include <stdio.h> |
| 196 | +
|
| 197 | +int main( void ) { |
| 198 | + const float min[] = { 0.0f, 0.0f, 0.0f, 0.0f, -3.14f }; |
| 199 | + const float max[] = { 5.0f, 5.0f, 5.0f, 5.0f, -0.0f }; |
| 200 | + const float v[] = { 3.14f, -3.14f, 10.0f, -0.0f, 0.0f }; |
| 201 | +
|
| 202 | + float out; |
| 203 | + int i; |
| 204 | + for ( i = 0; i < 5; i++ ) { |
| 205 | + out = stdlib_base_wrapf( v[i], min[i], max[i] ); |
| 206 | + printf( "wrapf(%f,%f,%f) => %f\n", v[i], min[i], max[i], out ); |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.examples --> |
| 214 | + |
| 215 | +</section> |
| 216 | + |
| 217 | +<!-- /.c --> |
| 218 | + |
| 219 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 220 | + |
| 221 | +<section class="references"> |
| 222 | + |
| 223 | +</section> |
| 224 | + |
| 225 | +<!-- /.references --> |
| 226 | + |
| 227 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 228 | + |
| 229 | +<section class="related"> |
| 230 | + |
| 231 | +</section> |
| 232 | + |
| 233 | +<!-- /.related --> |
| 234 | + |
| 235 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 236 | + |
| 237 | +<section class="links"> |
| 238 | + |
| 239 | +<!-- <related-links> --> |
| 240 | + |
| 241 | +<!-- </related-links> --> |
| 242 | + |
| 243 | +</section> |
| 244 | + |
| 245 | +<!-- /.links --> |
0 commit comments