|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 | +# roundnf |
| 22 | + |
| 23 | +> Round a single-precision floating-point number to the nearest multiple of 10^n. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var roundnf = require( '@stdlib/math/base/special/roundnf' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### roundnf( x, n ) |
| 34 | + |
| 35 | +Rounds a single-precision floating-point number to the nearest multiple of `10^n`. |
| 36 | + |
| 37 | +```javascript |
| 38 | +// Round a value to 2 decimal places: |
| 39 | +var v = roundnf( 3.1415927410125732, -2 ); |
| 40 | +// returns ~3.14 |
| 41 | + |
| 42 | +// If n = 0, `roundnf` behaves like `roundf`: |
| 43 | +v = roundnf( 3.1415927410125732, 0 ); |
| 44 | +// returns 3.0 |
| 45 | + |
| 46 | +// Round a value to the nearest thousand: |
| 47 | +v = roundnf( 12368.0, 3 ); |
| 48 | +// returns ~12000.0 |
| 49 | +``` |
| 50 | + |
| 51 | +</section> |
| 52 | + |
| 53 | +<!-- /.usage --> |
| 54 | + |
| 55 | +<section class="notes"> |
| 56 | + |
| 57 | +## Notes |
| 58 | + |
| 59 | +- When operating on [floating-point numbers][ieee754] in bases other than `2`, rounding to specified digits can be **inexact**. For example, |
| 60 | + |
| 61 | + ```javascript |
| 62 | + var x = 0.2 + 0.1; |
| 63 | + // returns 0.30000000000000004 |
| 64 | + |
| 65 | + // Should round to 0.3... |
| 66 | + var v = roundnf( x, -16 ); |
| 67 | + // returns 0.30000001192092896 |
| 68 | + ``` |
| 69 | + |
| 70 | +- Ties are rounded toward positive infinity. |
| 71 | + |
| 72 | +</section> |
| 73 | + |
| 74 | +<!-- /.notes --> |
| 75 | + |
| 76 | +<section class="examples"> |
| 77 | + |
| 78 | +## Examples |
| 79 | + |
| 80 | +<!-- eslint no-undef: "error" --> |
| 81 | + |
| 82 | +```javascript |
| 83 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 84 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 85 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 86 | +var roundnf = require( '@stdlib/math/base/special/roundnf' ); |
| 87 | +
|
| 88 | +var x = uniform( 100, -50.0, 50.0, { |
| 89 | + 'dtype': 'float32' |
| 90 | +}); |
| 91 | +
|
| 92 | +var n = discreteUniform( 100, -5, 0, { |
| 93 | + 'dtype': 'int32' |
| 94 | +}); |
| 95 | +
|
| 96 | +logEachMap( 'x: %0.8f. Number of decimals: %d. Rounded: %0.8f', x, n, roundnf ); |
| 97 | +``` |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.examples --> |
| 102 | + |
| 103 | +<!-- C interface documentation. --> |
| 104 | + |
| 105 | +* * * |
| 106 | + |
| 107 | +<section class="c"> |
| 108 | + |
| 109 | +## C APIs |
| 110 | + |
| 111 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 112 | + |
| 113 | +<section class="intro"> |
| 114 | + |
| 115 | +</section> |
| 116 | + |
| 117 | +<!-- /.intro --> |
| 118 | + |
| 119 | +<!-- C usage documentation. --> |
| 120 | + |
| 121 | +<section class="usage"> |
| 122 | + |
| 123 | +### Usage |
| 124 | + |
| 125 | +```c |
| 126 | +#include "stdlib/math/base/special/roundnf.h" |
| 127 | +``` |
| 128 | + |
| 129 | +#### stdlib_base_roundnf( x, n ) |
| 130 | + |
| 131 | +Rounds a single-precision floating-point number to the nearest multiple of `10^n`. |
| 132 | + |
| 133 | +```c |
| 134 | +// Round a value to 2 decimal places: |
| 135 | +float y = stdlib_base_roundnf( 3.14159f, -2 ); |
| 136 | +// returns ~3.14f |
| 137 | +
|
| 138 | +// If n = 0, `roundnf` behaves like `roundf`: |
| 139 | +y = stdlib_base_roundnf( 3.14159f, 0 ); |
| 140 | +// returns 3.0f |
| 141 | +``` |
| 142 | + |
| 143 | +The function accepts the following arguments: |
| 144 | + |
| 145 | +- **x**: `[in] float` input value. |
| 146 | +- **n**: `[in] int32_t` integer power of 10. |
| 147 | + |
| 148 | +```c |
| 149 | +float stdlib_base_roundnf( const float x, const int32_t n ); |
| 150 | +``` |
| 151 | +
|
| 152 | +</section> |
| 153 | +
|
| 154 | +<!-- /.usage --> |
| 155 | +
|
| 156 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 157 | +
|
| 158 | +<section class="notes"> |
| 159 | +
|
| 160 | +</section> |
| 161 | +
|
| 162 | +<!-- /.notes --> |
| 163 | +
|
| 164 | +<!-- C API usage examples. --> |
| 165 | +
|
| 166 | +<section class="examples"> |
| 167 | +
|
| 168 | +### Examples |
| 169 | +
|
| 170 | +```c |
| 171 | +#include "stdlib/math/base/special/roundnf.h" |
| 172 | +#include <stdio.h> |
| 173 | + |
| 174 | +int main( void ) { |
| 175 | + const float x[] = { 3.14f, -3.14f, 0.0f, 0.0f/0.0f }; |
| 176 | + |
| 177 | + float y; |
| 178 | + int i; |
| 179 | + for ( i = 0; i < 4; i++ ) { |
| 180 | + y = stdlib_base_roundnf( x[ i ], -2 ); |
| 181 | + printf( "roundnf(%f, -2) = %f\n", x[ i ], y ); |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
| 185 | +
|
| 186 | +</section> |
| 187 | +
|
| 188 | +<!-- /.examples --> |
| 189 | +
|
| 190 | +</section> |
| 191 | +
|
| 192 | +<!-- /.c --> |
| 193 | +
|
| 194 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 195 | +
|
| 196 | +<section class="related"> |
| 197 | +
|
| 198 | +</section> |
| 199 | +
|
| 200 | +<!-- /.related --> |
| 201 | +
|
| 202 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 203 | +
|
| 204 | +<section class="links"> |
| 205 | +
|
| 206 | +[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 |
| 207 | +
|
| 208 | +<!-- <related-links> --> |
| 209 | +
|
| 210 | +<!-- </related-links> --> |
| 211 | +
|
| 212 | +</section> |
| 213 | +
|
| 214 | +<!-- /.links --> |
0 commit comments