floor.js (5246B)
1 "use strict"; 2 3 var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); 4 5 Object.defineProperty(exports, "__esModule", { 6 value: true 7 }); 8 exports.createFloor = void 0; 9 10 var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); 11 12 var _decimal = _interopRequireDefault(require("decimal.js")); 13 14 var _factory = require("../../utils/factory.js"); 15 16 var _collection = require("../../utils/collection.js"); 17 18 var _number = require("../../utils/number.js"); 19 20 var _nearlyEqual = require("../../utils/bignumber/nearlyEqual.js"); 21 22 var _algorithm = require("../../type/matrix/utils/algorithm11.js"); 23 24 var _algorithm2 = require("../../type/matrix/utils/algorithm14.js"); 25 26 var name = 'floor'; 27 var dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar']; 28 var createFloor = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 29 var typed = _ref.typed, 30 config = _ref.config, 31 round = _ref.round, 32 matrix = _ref.matrix, 33 equalScalar = _ref.equalScalar; 34 var algorithm11 = (0, _algorithm.createAlgorithm11)({ 35 typed: typed, 36 equalScalar: equalScalar 37 }); 38 var algorithm14 = (0, _algorithm2.createAlgorithm14)({ 39 typed: typed 40 }); 41 /** 42 * Round a value towards minus infinity. 43 * For matrices, the function is evaluated element wise. 44 * 45 * Syntax: 46 * 47 * math.floor(x) 48 * math.floor(x, n) 49 * 50 * Examples: 51 * 52 * math.floor(3.2) // returns number 3 53 * math.floor(3.8) // returns number 3 54 * math.floor(-4.2) // returns number -5 55 * math.floor(-4.7) // returns number -5 56 * 57 * math.floor(3.212, 2) // returns number 3.21 58 * math.floor(3.288, 2) // returns number 3.28 59 * math.floor(-4.212, 2) // returns number -4.22 60 * math.floor(-4.782, 2) // returns number -4.79 61 * 62 * const c = math.complex(3.24, -2.71) 63 * math.floor(c) // returns Complex 3 - 3i 64 * math.floor(c, 1) // returns Complex 3.2 - 2.8i 65 * 66 * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] 67 * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] 68 * 69 * See also: 70 * 71 * ceil, fix, round 72 * 73 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded 74 * @param {number | BigNumber | Array} [n=0] Number of decimals 75 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value 76 */ 77 78 return typed('floor', { 79 number: function number(x) { 80 if ((0, _number.nearlyEqual)(x, round(x), config.epsilon)) { 81 return round(x); 82 } else { 83 return Math.floor(x); 84 } 85 }, 86 'number, number': function numberNumber(x, n) { 87 if ((0, _number.nearlyEqual)(x, round(x, n), config.epsilon)) { 88 return round(x, n); 89 } else { 90 var _$split = "".concat(x, "e").split('e'), 91 _$split2 = (0, _slicedToArray2.default)(_$split, 2), 92 number = _$split2[0], 93 exponent = _$split2[1]; 94 95 var result = Math.floor(Number("".concat(number, "e").concat(Number(exponent) + n))); 96 97 var _$split3 = "".concat(result, "e").split('e'); 98 99 var _$split4 = (0, _slicedToArray2.default)(_$split3, 2); 100 101 number = _$split4[0]; 102 exponent = _$split4[1]; 103 return Number("".concat(number, "e").concat(Number(exponent) - n)); 104 } 105 }, 106 Complex: function Complex(x) { 107 return x.floor(); 108 }, 109 'Complex, number': function ComplexNumber(x, n) { 110 return x.floor(n); 111 }, 112 BigNumber: function BigNumber(x) { 113 if ((0, _nearlyEqual.nearlyEqual)(x, round(x), config.epsilon)) { 114 return round(x); 115 } else { 116 return x.floor(); 117 } 118 }, 119 'BigNumber, BigNumber': function BigNumberBigNumber(x, n) { 120 if ((0, _nearlyEqual.nearlyEqual)(x, round(x, n), config.epsilon)) { 121 return round(x, n); 122 } else { 123 return x.toDecimalPlaces(n.toNumber(), _decimal.default.ROUND_FLOOR); 124 } 125 }, 126 Fraction: function Fraction(x) { 127 return x.floor(); 128 }, 129 'Fraction, number': function FractionNumber(x, n) { 130 return x.floor(n); 131 }, 132 'Array | Matrix': function ArrayMatrix(x) { 133 // deep map collection, skip zeros since floor(0) = 0 134 return (0, _collection.deepMap)(x, this, true); 135 }, 136 'Array | Matrix, number': function ArrayMatrixNumber(x, n) { 137 var _this = this; 138 139 // deep map collection, skip zeros since ceil(0) = 0 140 return (0, _collection.deepMap)(x, function (i) { 141 return _this(i, n); 142 }, true); 143 }, 144 'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) { 145 return algorithm11(x, y, this, false); 146 }, 147 'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) { 148 return algorithm14(x, y, this, false); 149 }, 150 'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) { 151 // use matrix implementation 152 return algorithm14(matrix(y), x, this, true).valueOf(); 153 } 154 }); 155 }); 156 exports.createFloor = createFloor;