README.md (2339B)
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 # roundb 22 23 > Round a numeric value to the nearest multiple of b^n on a linear scale. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var roundb = require( '@stdlib/math/base/special/roundb' ); 31 ``` 32 33 #### roundb( x, n, b ) 34 35 Rounds a `numeric` value to the nearest multiple of `b^n` on a linear scale. 36 37 ```javascript 38 // Round a value to 2 decimal places: 39 var v = roundb( 3.141592653589793, -2, 10 ); 40 // returns 3.14 41 42 // If n = 0 or b = 1, `roundb` behaves like `round`: 43 v = roundb( 3.141592653589793, 0, 2 ); 44 // returns 3.0 45 46 // Round a value to the nearest multiple of two: 47 v = roundb( 5.0, 1, 2 ); 48 // returns 6.0 49 ``` 50 51 </section> 52 53 <!-- /.usage --> 54 55 <section class="notes"> 56 57 ## Notes 58 59 - Due to rounding error in [floating-point numbers][ieee754], rounding may **not** be exact. 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 = roundb( x, -16, 10 ); 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 round = require( '@stdlib/math/base/special/round' ); 83 var pow = require( '@stdlib/math/base/special/pow' ); 84 var roundb = require( '@stdlib/math/base/special/roundb' ); 85 86 var x; 87 var n; 88 var b; 89 var v; 90 var i; 91 92 for ( i = 0; i < 100; i++ ) { 93 x = (randu()*100.0) - 50.0; 94 n = round( (randu()*10.0) - 5.0 ); 95 b = round( randu()*10.0 ); 96 v = roundb( x, n, b ); 97 console.log( 'x: %d. %d^%d: %d. Rounded: %d.', x, b, n, pow( b, n ), v ); 98 } 99 ``` 100 101 </section> 102 103 <!-- /.examples --> 104 105 <section class="links"> 106 107 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 108 109 </section> 110 111 <!-- /.links -->