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