forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.js
More file actions
104 lines (97 loc) · 3.21 KB
/
mod.js
File metadata and controls
104 lines (97 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { factory } from '../../utils/factory.js'
import { createFloor } from './floor.js'
import { createMatAlgo02xDS0 } from '../../type/matrix/utils/matAlgo02xDS0.js'
import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js'
import { createMatAlgo05xSfSf } from '../../type/matrix/utils/matAlgo05xSfSf.js'
import { createMatAlgo11xS0s } from '../../type/matrix/utils/matAlgo11xS0s.js'
import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js'
import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js'
const name = 'mod'
const dependencies = [
'typed',
'config',
'round',
'matrix',
'equalScalar',
'zeros',
'DenseMatrix',
'concat'
]
export const createMod = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar, zeros, DenseMatrix, concat }) => {
const floor = createFloor({ typed, config, round, matrix, equalScalar, zeros, DenseMatrix })
const matAlgo02xDS0 = createMatAlgo02xDS0({ typed, equalScalar })
const matAlgo03xDSf = createMatAlgo03xDSf({ typed })
const matAlgo05xSfSf = createMatAlgo05xSfSf({ typed, equalScalar })
const matAlgo11xS0s = createMatAlgo11xS0s({ typed, equalScalar })
const matAlgo12xSfs = createMatAlgo12xSfs({ typed, DenseMatrix })
const matrixAlgorithmSuite = createMatrixAlgorithmSuite({ typed, matrix, concat })
/**
* Calculates the modulus, the remainder of an integer division.
*
* For matrices, the function is evaluated element wise.
*
* The modulus is defined as:
*
* x - y * floor(x / y)
*
* See https://en.wikipedia.org/wiki/Modulo_operation.
*
* Syntax:
*
* math.mod(x, y)
*
* Examples:
*
* math.mod(8, 3) // returns 2
* math.mod(11, 2) // returns 1
*
* function isOdd(x) {
* return math.mod(x, 2) != 0
* }
*
* isOdd(2) // returns false
* isOdd(3) // returns true
*
* See also:
*
* divide
*
* @param {number | BigNumber | Fraction | Array | Matrix} x Dividend
* @param {number | BigNumber | Fraction | Array | Matrix} y Divisor
* @return {number | BigNumber | Fraction | Array | Matrix} Returns the remainder of `x` divided by `y`.
*/
return typed(
name,
{
'number, number': _modNumber,
'BigNumber, BigNumber': function (x, y) {
return y.isZero() ? x : x.sub(y.mul(floor(x.div(y))))
},
'Fraction, Fraction': function (x, y) {
return y.equals(0) ? x : x.sub(y.mul(floor(x.div(y))))
}
},
matrixAlgorithmSuite({
SS: matAlgo05xSfSf,
DS: matAlgo03xDSf,
SD: matAlgo02xDS0,
Ss: matAlgo11xS0s,
sS: matAlgo12xSfs
})
)
/**
* Calculate the modulus of two numbers
* @param {number} x
* @param {number} y
* @returns {number} res
* @private
*/
function _modNumber (x, y) {
// We don't use JavaScript's % operator here as this doesn't work
// correctly for x < 0 and x === 0
// see https://en.wikipedia.org/wiki/Modulo_operation
// We use mathjs floor to handle errors associated with
// precision float approximation
return (y === 0) ? x : x - y * floor(x / y)
}
})