floorn.js (5815B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isnan = require( './../../../../base/assert/is-nan' ); 24 var isInfinite = require( './../../../../base/assert/is-infinite' ); 25 var pow = require( './../../../../base/special/pow' ); 26 var abs = require( './../../../../base/special/abs' ); 27 var floor = require( './../../../../base/special/floor' ); 28 var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); 29 var MAX_EXP = require( '@stdlib/constants/float64/max-base10-exponent' ); 30 var MIN_EXP = require( '@stdlib/constants/float64/min-base10-exponent' ); 31 var MIN_EXP_SUBNORMAL = require( '@stdlib/constants/float64/min-base10-exponent-subnormal' ); 32 var NINF = require( '@stdlib/constants/float64/ninf' ); 33 34 35 // VARIABLES // 36 37 var MAX_INT = MAX_SAFE_INTEGER + 1; 38 var HUGE = 1.0e+308; 39 40 41 // MAIN // 42 43 /** 44 * Rounds a numeric value to the nearest multiple of \\(10^n\\) toward negative infinity. 45 * 46 * ## Method 47 * 48 * 1. If \\(|x| <= 2^{53}\\) and \\(|n| <= 308\\), we can use the formula 49 * 50 * ```tex 51 * \operatorname{floorn}(x,n) = \frac{\operatorname{floor}(x \cdot 10^{-n})}{10^{-n}} 52 * ``` 53 * 54 * which shifts the decimal to the nearest multiple of \\(10^n\\), performs a standard \\(\mathrm{floor}\\) operation, and then shifts the decimal to its original position. 55 * 56 * <!-- <note> --> 57 * 58 * If \\(x \cdot 10^{-n}\\) overflows, \\(x\\) lacks a sufficient number of decimal digits to have any effect when rounding. Accordingly, the rounded value is \\(x\\). 59 * 60 * <!-- </note> --> 61 * 62 * <!-- <note> --> 63 * 64 * Note that rescaling \\(x\\) can result in unexpected behavior. For instance, the result of \\(\operatorname{floorn}(-0.2-0.1,-16)\\) is \\(-0.3000000000000001\\) and not \\(-0.3\\). While possibly unexpected, this is not a bug. The behavior stems from the fact that most decimal fractions cannot be exactly represented as floating-point numbers. And further, rescaling can lead to slightly different fractional values, which, in turn, affects the result of \\(\mathrm{floor}\\). 65 * 66 * <!-- </note> --> 67 * 68 * 2. If \\(n > 308\\), we recognize that the maximum absolute double-precision floating-point number is \\(\approx 1.8\mbox{e}308\\) and, thus, the result of rounding any possible negative finite number \\(x\\) to the nearest \\(10^n\\) is \\(-\infty\\) and any possible positive finite number \\(x\\) is \\(+0\\). To ensure consistent behavior with \\(\operatorname{floor}(x)\\), if \\(x > 0\\), the sign of \\(x\\) is preserved. 69 * 70 * 3. If \\(n < -324\\), \\(n\\) exceeds the maximum number of possible decimal places (such as with subnormal numbers), and, thus, the rounded value is \\(x\\). 71 * 72 * 4. If \\(x > 2^{53}\\), \\(x\\) is **always** an integer (i.e., \\(x\\) has no decimal digits). If \\(n <= 0\\), the rounded value is \\(x\\). 73 * 74 * 5. If \\(n < -308\\), we let \\(m = n + 308\\) and modify the above formula to avoid overflow. 75 * 76 * ```tex 77 * \operatorname{floorn}(x,n) = \frac{\biggl(\frac{\operatorname{floor}( (x \cdot 10^{308}) 10^{-m})}{10^{308}}\biggr)}{10^{-m}} 78 * ``` 79 * 80 * If overflow occurs, the rounded value is \\(x\\). 81 * 82 * 83 * ## Special Cases 84 * 85 * ```tex 86 * \begin{align*} 87 * \operatorname{floorn}(\mathrm{NaN}, n) &= \mathrm{NaN} \\ 88 * \operatorname{floorn}(x, \mathrm{NaN}) &= \mathrm{NaN} \\ 89 * \operatorname{floorn}(x, \pm\infty) &= \mathrm{NaN} \\ 90 * \operatorname{floorn}(\pm\infty, n) &= \pm\infty \\ 91 * \operatorname{floorn}(\pm 0, n) &= \pm 0 92 * \end{align*} 93 * ``` 94 * 95 * 96 * @param {number} x - input value 97 * @param {integer} n - integer power of 10 98 * @returns {number} rounded value 99 * 100 * @example 101 * // Round a value to 4 decimal places: 102 * var v = floorn( 3.141592653589793, -4 ); 103 * // returns 3.1415 104 * 105 * @example 106 * // If n = 0, `floorn` behaves like `floor`: 107 * var v = floorn( 3.141592653589793, 0 ); 108 * // returns 3.0 109 * 110 * @example 111 * // Round a value to the nearest thousand: 112 * var v = floorn( 12368.0, 3 ); 113 * // returns 12000.0 114 */ 115 function floorn( x, n ) { 116 var s; 117 var y; 118 if ( 119 isnan( x ) || 120 isnan( n ) || 121 isInfinite( n ) 122 ) { 123 return NaN; 124 } 125 if ( 126 // Handle infinities... 127 isInfinite( x ) || 128 129 // Handle +-0... 130 x === 0.0 || 131 132 // If `n` exceeds the maximum number of feasible decimal places (such as with subnormal numbers), nothing to round... 133 n < MIN_EXP_SUBNORMAL || 134 135 // If `|x|` is large enough, no decimals to round... 136 ( abs( x ) > MAX_INT && n <= 0 ) 137 ) { 138 return x; 139 } 140 // The maximum absolute double is ~1.8e308. Accordingly, any possible positive finite `x` rounded to the nearest >=10^309 is infinity and any negative finite `x` is zero. 141 if ( n > MAX_EXP ) { 142 if ( x >= 0.0 ) { 143 return 0.0; // preserve the sign (same behavior as floor) 144 } 145 return NINF; 146 } 147 // If we overflow, return `x`, as the number of digits to the right of the decimal is too small (i.e., `x` is too large / lacks sufficient fractional precision) for there to be any effect when rounding... 148 if ( n < MIN_EXP ) { 149 s = pow( 10.0, -(n + MAX_EXP) ); 150 y = (x*HUGE) * s; // order of operation matters! 151 if ( isInfinite( y ) ) { 152 return x; 153 } 154 return ( floor(y)/HUGE ) / s; 155 } 156 s = pow( 10.0, -n ); 157 y = x * s; 158 if ( isInfinite( y ) ) { 159 return x; 160 } 161 return floor( y ) / s; 162 } 163 164 165 // EXPORTS // 166 167 module.exports = floorn;