forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addScalar.js
47 lines (39 loc) · 1.45 KB
/
addScalar.js
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
import { factory } from '../../utils/factory'
import { addNumber } from '../../plain/number'
const name = 'addScalar'
const dependencies = ['typed']
export const createAddScalar = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
/**
* Add two scalar values, `x + y`.
* This function is meant for internal use: it is used by the public function
* `add`
*
* This function does not support collections (Array or Matrix).
*
* @param {number | BigNumber | Fraction | Complex | Unit} x First value to add
* @param {number | BigNumber | Fraction | Complex} y Second value to add
* @return {number | BigNumber | Fraction | Complex | Unit} Sum of `x` and `y`
* @private
*/
return typed(name, {
'number, number': addNumber,
'Complex, Complex': function (x, y) {
return x.add(y)
},
'BigNumber, BigNumber': function (x, y) {
return x.plus(y)
},
'Fraction, Fraction': function (x, y) {
return x.add(y)
},
'Unit, Unit': function (x, y) {
if (x.value === null || x.value === undefined) throw new Error('Parameter x contains a unit with undefined value')
if (y.value === null || y.value === undefined) throw new Error('Parameter y contains a unit with undefined value')
if (!x.equalBase(y)) throw new Error('Units do not match')
const res = x.clone()
res.value = this(res.value, y.value)
res.fixPrefix = false
return res
}
})
})