UNPKG

4.89 kBJavaScriptView Raw
1'use strict'
2
3const isInteger = require('../../utils/number').isInteger
4const toFixed = require('../../utils/number').toFixed
5const deepMap = require('../../utils/collection/deepMap')
6
7const NO_INT = 'Number of decimals in function round must be an integer'
8
9function 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 * Round a value towards the nearest integer.
20 * For matrices, the function is evaluated element wise.
21 *
22 * Syntax:
23 *
24 * math.round(x)
25 * math.round(x, n)
26 *
27 * Examples:
28 *
29 * math.round(3.2) // returns number 3
30 * math.round(3.8) // returns number 4
31 * math.round(-4.2) // returns number -4
32 * math.round(-4.7) // returns number -5
33 * math.round(math.pi, 3) // returns number 3.142
34 * math.round(123.45678, 2) // returns number 123.46
35 *
36 * const c = math.complex(3.2, -2.7)
37 * math.round(c) // returns Complex 3 - 3i
38 *
39 * math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5]
40 *
41 * See also:
42 *
43 * ceil, fix, floor
44 *
45 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
46 * @param {number | BigNumber | Array} [n=0] Number of decimals
47 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
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 // deep map collection, skip zeros since round(0) = 0
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 // check scalar is zero
119 if (equalScalar(x, 0)) {
120 // do not execute algorithm, result will be a zero matrix
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 // check scalar is zero
128 if (equalScalar(x, 0)) {
129 // do not execute algorithm, result will be a zero matrix
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 // use matrix implementation
137 return algorithm14(matrix(x), y, round, false).valueOf()
138 },
139
140 'number | Complex | BigNumber, Array': function (x, y) {
141 // use matrix implementation
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 // use default template
149 }
150
151 return round
152}
153
154/**
155 * round a number to the given number of decimals, or to zero if decimals is
156 * not provided
157 * @param {number} value
158 * @param {number} decimals number of decimals, between 0 and 15 (0 by default)
159 * @return {number} roundedValue
160 * @private
161 */
162function _round (value, decimals) {
163 return parseFloat(toFixed(value, decimals))
164}
165
166exports.name = 'round'
167exports.factory = factory