1 | 'use strict'
|
2 |
|
3 | const isInteger = require('../../utils/number').isInteger
|
4 | const toFixed = require('../../utils/number').toFixed
|
5 | const deepMap = require('../../utils/collection/deepMap')
|
6 |
|
7 | const NO_INT = 'Number of decimals in function round must be an integer'
|
8 |
|
9 | function factory (type, config, load, typed) {
|
10 | const matrix = load(require('../../type/matrix/function/matrix'))
|
11 | const equalScalar = load(require('../relational/equalScalar'))
|
12 | const zeros = load(require('../matrix/zeros'))
|
13 |
|
14 | const algorithm11 = load(require('../../type/matrix/utils/algorithm11'))
|
15 | const algorithm12 = load(require('../../type/matrix/utils/algorithm12'))
|
16 | const algorithm14 = load(require('../../type/matrix/utils/algorithm14'))
|
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 | const round = typed('round', {
|
50 |
|
51 | 'number': function (x) {
|
52 | return _round(x, 0)
|
53 | },
|
54 |
|
55 | 'number, number': function (x, n) {
|
56 | if (!isInteger(n)) { throw new TypeError(NO_INT) }
|
57 | if (n < 0 || n > 15) { throw new Error('Number of decimals in function round must be in te range of 0-15') }
|
58 |
|
59 | return _round(x, n)
|
60 | },
|
61 |
|
62 | 'Complex': function (x) {
|
63 | return x.round()
|
64 | },
|
65 |
|
66 | 'Complex, number': function (x, n) {
|
67 | if (n % 1) { throw new TypeError(NO_INT) }
|
68 |
|
69 | return x.round(n)
|
70 | },
|
71 |
|
72 | 'Complex, BigNumber': function (x, n) {
|
73 | if (!n.isInteger()) { throw new TypeError(NO_INT) }
|
74 |
|
75 | const _n = n.toNumber()
|
76 | return x.round(_n)
|
77 | },
|
78 |
|
79 | 'number, BigNumber': function (x, n) {
|
80 | if (!n.isInteger()) { throw new TypeError(NO_INT) }
|
81 |
|
82 | return new type.BigNumber(x).toDecimalPlaces(n.toNumber())
|
83 | },
|
84 |
|
85 | 'BigNumber': function (x) {
|
86 | return x.toDecimalPlaces(0)
|
87 | },
|
88 |
|
89 | 'BigNumber, BigNumber': function (x, n) {
|
90 | if (!n.isInteger()) { throw new TypeError(NO_INT) }
|
91 |
|
92 | return x.toDecimalPlaces(n.toNumber())
|
93 | },
|
94 |
|
95 | 'Fraction': function (x) {
|
96 | return x.round()
|
97 | },
|
98 |
|
99 | 'Fraction, number': function (x, n) {
|
100 | if (n % 1) { throw new TypeError(NO_INT) }
|
101 | return x.round(n)
|
102 | },
|
103 |
|
104 | 'Array | Matrix': function (x) {
|
105 |
|
106 | return deepMap(x, round, true)
|
107 | },
|
108 |
|
109 | 'SparseMatrix, number | BigNumber': function (x, y) {
|
110 | return algorithm11(x, y, round, false)
|
111 | },
|
112 |
|
113 | 'DenseMatrix, number | BigNumber': function (x, y) {
|
114 | return algorithm14(x, y, round, false)
|
115 | },
|
116 |
|
117 | 'number | Complex | BigNumber, SparseMatrix': function (x, y) {
|
118 |
|
119 | if (equalScalar(x, 0)) {
|
120 |
|
121 | return zeros(y.size(), y.storage())
|
122 | }
|
123 | return algorithm12(y, x, round, true)
|
124 | },
|
125 |
|
126 | 'number | Complex | BigNumber, DenseMatrix': function (x, y) {
|
127 |
|
128 | if (equalScalar(x, 0)) {
|
129 |
|
130 | return zeros(y.size(), y.storage())
|
131 | }
|
132 | return algorithm14(y, x, round, true)
|
133 | },
|
134 |
|
135 | 'Array, number | BigNumber': function (x, y) {
|
136 |
|
137 | return algorithm14(matrix(x), y, round, false).valueOf()
|
138 | },
|
139 |
|
140 | 'number | Complex | BigNumber, Array': function (x, y) {
|
141 |
|
142 | return algorithm14(matrix(y), x, round, true).valueOf()
|
143 | }
|
144 | })
|
145 |
|
146 | round.toTex = {
|
147 | 1: `\\left\\lfloor\${args[0]}\\right\\rceil`,
|
148 | 2: undefined
|
149 | }
|
150 |
|
151 | return round
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function _round (value, decimals) {
|
163 | return parseFloat(toFixed(value, decimals))
|
164 | }
|
165 |
|
166 | exports.name = 'round'
|
167 | exports.factory = factory
|