1 |
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 | const nearlyEqual = require('../../utils/number').nearlyEqual
|
5 | const bigNearlyEqual = require('../../utils/bignumber/nearlyEqual')
|
6 |
|
7 | function factory (type, config, load, typed) {
|
8 | const round = load(require('../../function/arithmetic/round'))
|
9 |
|
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 | floor.toTex = { 1: `\\left\\lfloor\${args[0]}\\right\\rfloor` }
|
69 |
|
70 | return floor
|
71 | }
|
72 |
|
73 | exports.name = 'floor'
|
74 | exports.factory = factory
|