floorsd.js (2756B)
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 log10 = require( './../../../../base/special/log10' ); 27 var ln = require( './../../../../base/special/ln' ); 28 var abs = require( './../../../../base/special/abs' ); 29 var floor = require( './../../../../base/special/floor' ); 30 var exponent = require( '@stdlib/number/float64/base/exponent' ); 31 32 33 // MAIN // 34 35 /** 36 * Rounds a numeric value to the nearest number toward negative infinity with \\(N\\) significant figures. 37 * 38 * @param {number} x - input value 39 * @param {PositiveInteger} n - number of significant figures 40 * @param {PositiveInteger} [b=10] - base 41 * @returns {number} rounded value 42 * 43 * @example 44 * var v = floorsd( 3.141592653589793, 5 ); 45 * // returns 3.1415 46 * 47 * @example 48 * var v = floorsd( 3.141592653589793, 1 ); 49 * // returns 3.0 50 * 51 * @example 52 * var v = floorsd( 12368.0, 2 ); 53 * // returns 12000.0 54 * 55 * @example 56 * var v = floorsd( 0.0313, 2, 2 ); 57 * // returns 0.03125 58 */ 59 function floorsd( x, n, b ) { 60 var base; 61 var exp; 62 var s; 63 var y; 64 if ( 65 isnan( x ) || 66 isnan( n ) || 67 n < 1 || 68 isInfinite( n ) 69 ) { 70 return NaN; 71 } 72 if ( arguments.length > 2 ) { 73 if ( 74 isnan( b ) || 75 b <= 0 || 76 isInfinite( b ) 77 ) { 78 return NaN; 79 } 80 base = b; 81 } else { 82 base = 10; 83 } 84 if ( isInfinite( x ) || x === 0.0 ) { 85 return x; 86 } 87 if ( base === 10 ) { 88 exp = log10( abs( x ) ); 89 } 90 else if ( base === 2 ) { 91 exp = exponent( abs( x ) ); 92 } 93 else { 94 exp = ln( abs(x) ) / ln( base ); 95 } 96 exp = floor( exp - n + 1.0 ); 97 s = pow( base, abs( exp ) ); 98 99 // Check for overflow: 100 if ( isInfinite( s ) ) { 101 return x; 102 } 103 // To avoid numerical stability issues due to floating-point rounding error (e.g., 3.55/0.1-35.5 = -7.105427357601e-15 and 3.55*10-35.5 = 0), we must treat positive and negative exponents separately. 104 if ( exp < 0 ) { 105 y = floor( x * s ) / s; 106 } else { 107 y = floor( x / s ) * s; 108 } 109 // Check for overflow: 110 if ( isInfinite( y ) ) { 111 return x; 112 } 113 return y; 114 } 115 116 117 // EXPORTS // 118 119 module.exports = floorsd;