UNPKG

1.99 kBJavaScriptView Raw
1import { factory } from '../../utils/factory';
2import { deepMap } from '../../utils/collection';
3import { nearlyEqual } from '../../utils/number';
4import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual';
5var name = 'floor';
6var dependencies = ['typed', 'config', 'round'];
7export var createFloor =
8/* #__PURE__ */
9factory(name, dependencies, function (_ref) {
10 var typed = _ref.typed,
11 config = _ref.config,
12 round = _ref.round;
13
14 /**
15 * Round a value towards minus infinity.
16 * For matrices, the function is evaluated element wise.
17 *
18 * Syntax:
19 *
20 * math.floor(x)
21 *
22 * Examples:
23 *
24 * math.floor(3.2) // returns number 3
25 * math.floor(3.8) // returns number 3
26 * math.floor(-4.2) // returns number -5
27 * math.floor(-4.7) // returns number -5
28 *
29 * const c = math.complex(3.2, -2.7)
30 * math.floor(c) // returns Complex 3 - 3i
31 *
32 * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5]
33 *
34 * See also:
35 *
36 * ceil, fix, round
37 *
38 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
39 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
40 */
41 var floor = typed('floor', {
42 number: function number(x) {
43 if (nearlyEqual(x, round(x), config.epsilon)) {
44 return round(x);
45 } else {
46 return Math.floor(x);
47 }
48 },
49 Complex: function Complex(x) {
50 return x.floor();
51 },
52 BigNumber: function BigNumber(x) {
53 if (bigNearlyEqual(x, round(x), config.epsilon)) {
54 return round(x);
55 } else {
56 return x.floor();
57 }
58 },
59 Fraction: function Fraction(x) {
60 return x.floor();
61 },
62 'Array | Matrix': function ArrayMatrix(x) {
63 // deep map collection, skip zeros since floor(0) = 0
64 return deepMap(x, floor, true);
65 }
66 });
67 return floor;
68});
\No newline at end of file