All files / math/base/special/floornf/lib main.js

98.8% Statements 165/167
95% Branches 19/20
100% Functions 1/1
98.8% Lines 165/167

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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