|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 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 | +# roundn |
| 22 | + |
| 23 | +> Round a double-precision floating-point number to the nearest multiple of 10^n. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var roundn = require( '@stdlib/math/base/special/roundn' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### roundn( x, n ) |
| 34 | + |
| 35 | +Rounds a double-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 = roundn( 3.141592653589793, -2 ); |
| 40 | +// returns 3.14 |
| 41 | + |
| 42 | +// If n = 0, `roundn` behaves like `round`: |
| 43 | +v = roundn( 3.141592653589793, 0 ); |
| 44 | +// returns 3.0 |
| 45 | + |
| 46 | +// Round a value to the nearest thousand: |
| 47 | +v = roundn( 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 = roundn( x, -16 ); |
| 67 | + // returns 0.3000000000000001 |
| 68 | + ``` |
| 69 | + |
| 70 | +</section> |
| 71 | + |
| 72 | +<!-- /.notes --> |
| 73 | + |
| 74 | +<section class="examples"> |
| 75 | + |
| 76 | +## Examples |
| 77 | + |
| 78 | +<!-- eslint no-undef: "error" --> |
| 79 | + |
| 80 | +```javascript |
| 81 | +var randu = require( '@stdlib/random/base/randu' ); |
| 82 | +var roundn = require( '@stdlib/math/base/special/roundn' ); |
| 83 | +
|
| 84 | +var x; |
| 85 | +var n; |
| 86 | +var v; |
| 87 | +var i; |
| 88 | +
|
| 89 | +for ( i = 0; i < 100; i++ ) { |
| 90 | + x = (randu()*100.0) - 50.0; |
| 91 | + n = roundn( randu()*5.0, 0 ); |
| 92 | + v = roundn( x, -n ); |
| 93 | + console.log( 'x: %d. Number of decimals: %d. Rounded: %d.', x, n, v ); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.examples --> |
| 100 | + |
| 101 | +<!-- C interface documentation. --> |
| 102 | + |
| 103 | +* * * |
| 104 | + |
| 105 | +<section class="c"> |
| 106 | + |
| 107 | +## C APIs |
| 108 | + |
| 109 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 110 | + |
| 111 | +<section class="intro"> |
| 112 | + |
| 113 | +</section> |
| 114 | + |
| 115 | +<!-- /.intro --> |
| 116 | + |
| 117 | +<!-- C usage documentation. --> |
| 118 | + |
| 119 | +<section class="usage"> |
| 120 | + |
| 121 | +### Usage |
| 122 | + |
| 123 | +```c |
| 124 | +#include "stdlib/math/base/special/roundn.h" |
| 125 | +``` |
| 126 | + |
| 127 | +#### stdlib_base_roundn( x, n ) |
| 128 | + |
| 129 | +Rounds a double-precision floating-point number to the nearest multiple of `10^n`. |
| 130 | + |
| 131 | +```c |
| 132 | +double y = stdlib_base_roundn( 3.14, -2 ); |
| 133 | +// returns 3.14 |
| 134 | +``` |
| 135 | + |
| 136 | +The function accepts the following arguments: |
| 137 | + |
| 138 | +- **x**: `[in] double` input value. |
| 139 | +- **n**: `[in] int32_t` integer power of 10. |
| 140 | + |
| 141 | +```c |
| 142 | +double stdlib_base_roundn( const double x, const int32_t n ); |
| 143 | +``` |
| 144 | +
|
| 145 | +</section> |
| 146 | +
|
| 147 | +<!-- /.usage --> |
| 148 | +
|
| 149 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 150 | +
|
| 151 | +<section class="notes"> |
| 152 | +
|
| 153 | +</section> |
| 154 | +
|
| 155 | +<!-- /.notes --> |
| 156 | +
|
| 157 | +<!-- C API usage examples. --> |
| 158 | +
|
| 159 | +<section class="examples"> |
| 160 | +
|
| 161 | +### Examples |
| 162 | +
|
| 163 | +```c |
| 164 | +#include "stdlib/math/base/special/roundn.h" |
| 165 | +#include <stdio.h> |
| 166 | + |
| 167 | +int main( void ) { |
| 168 | + const double x[] = { 3.14, -3.14, 0.0, 0.0/0.0 }; |
| 169 | + |
| 170 | + double y; |
| 171 | + int i; |
| 172 | + for ( i = 0; i < 4; i++ ) { |
| 173 | + y = stdlib_base_roundn( x[ i ], -2 ); |
| 174 | + printf( "roundn(%lf) = %lf\n", x[ i ], y ); |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | +
|
| 179 | +</section> |
| 180 | +
|
| 181 | +<!-- /.examples --> |
| 182 | +
|
| 183 | +</section> |
| 184 | +
|
| 185 | +<!-- /.c --> |
| 186 | +
|
| 187 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 188 | +
|
| 189 | +<section class="related"> |
| 190 | +
|
| 191 | +* * * |
| 192 | +
|
| 193 | +## See Also |
| 194 | +
|
| 195 | +- <span class="package-name">[`@stdlib/math/base/special/ceiln`][@stdlib/math/base/special/ceiln]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest multiple of 10^n toward positive infinity.</span> |
| 196 | +- <span class="package-name">[`@stdlib/math/base/special/floorn`][@stdlib/math/base/special/floorn]</span><span class="delimiter">: </span><span class="description">round a double-precision floating-point number to the nearest multiple of 10^n toward negative infinity.</span> |
| 197 | +- <span class="package-name">[`@stdlib/math/base/special/round`][@stdlib/math/base/special/round]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest integer.</span> |
| 198 | +- <span class="package-name">[`@stdlib/math/base/special/roundb`][@stdlib/math/base/special/roundb]</span><span class="delimiter">: </span><span class="description">round a numeric value to the nearest multiple of b^n on a linear scale.</span> |
| 199 | +
|
| 200 | +</section> |
| 201 | +
|
| 202 | +<!-- /.related --> |
| 203 | +
|
| 204 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 205 | +
|
| 206 | +<section class="links"> |
| 207 | +
|
| 208 | +[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 |
| 209 | +
|
| 210 | +<!-- <related-links> --> |
| 211 | +
|
| 212 | +[@stdlib/math/base/special/ceiln]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/ceiln |
| 213 | +
|
| 214 | +[@stdlib/math/base/special/floorn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/floorn |
| 215 | +
|
| 216 | +[@stdlib/math/base/special/round]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/round |
| 217 | +
|
| 218 | +[@stdlib/math/base/special/roundb]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundb |
| 219 | +
|
| 220 | +<!-- </related-links> --> |
| 221 | +
|
| 222 | +</section> |
| 223 | +
|
| 224 | +<!-- /.links --> |
0 commit comments