README.md (2144B)
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 # truncb 22 23 > Round a numeric value to the nearest multiple of b^n toward zero. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var truncb = require( '@stdlib/math/base/special/truncb' ); 31 ``` 32 33 #### truncb( x, n, b ) 34 35 Rounds a `numeric` value to the nearest multiple of `b^n` toward zero. 36 37 ```javascript 38 // Round a value to 4 decimal places: 39 var v = truncb( 3.141592653589793, -4, 10 ); 40 // returns 3.1415 41 42 // If n = 0 or b = 1, `truncb` behaves like `trunc`: 43 v = truncb( 3.141592653589793, 0, 2 ); 44 // returns 3.0 45 46 // Round a value to the nearest multiple of two toward zero: 47 v = truncb( 5.0, 1, 2 ); 48 // returns 4.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. 60 61 </section> 62 63 <!-- /.notes --> 64 65 <section class="examples"> 66 67 ## Examples 68 69 <!-- eslint no-undef: "error" --> 70 71 ```javascript 72 var randu = require( '@stdlib/random/base/randu' ); 73 var round = require( '@stdlib/math/base/special/round' ); 74 var pow = require( '@stdlib/math/base/special/pow' ); 75 var truncb = require( '@stdlib/math/base/special/truncb' ); 76 77 var x; 78 var n; 79 var b; 80 var v; 81 var i; 82 83 for ( i = 0; i < 100; i++ ) { 84 x = (randu()*100.0) - 50.0; 85 n = round( (randu()*10.0) - 5.0 ); 86 b = round( randu()*10.0 ); 87 v = truncb( x, n, b ); 88 console.log( 'x: %d. %d^%d: %d. Rounded: %d.', x, b, n, pow( b, n ), v ); 89 } 90 ``` 91 92 </section> 93 94 <!-- /.examples --> 95 96 <section class="links"> 97 98 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 99 100 </section> 101 102 <!-- /.links -->