mathjs/lib/function/arithmetic/addScalar.js
2015-04-27 12:03:59 -04:00

56 lines
1.5 KiB
JavaScript

'use strict';
function factory(type, config, load, 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), and does
* not validate the number of of inputs.
*
* @param {Number | BigNumber | Boolean | Complex | Unit | null} x First value to add
* @param {Number | BigNumber | Boolean | Complex | null} y Second value to add
* @return {Number | BigNumber | Complex | Unit} Sum of `x` and `y`
* @private
*/
var addScalar = typed('addScalar', {
'number, number': function (x, y) {
return x + y;
},
'Complex, Complex': function (x, y) {
return new type.Complex(
x.re + y.re,
x.im + y.im
);
},
'BigNumber, BigNumber': function (x, y) {
return x.plus(y);
},
'Unit, Unit': function (x, y) {
if (x.value == null) throw new Error('Parameter x contains a unit with undefined value');
if (y.value == null) throw new Error('Parameter y contains a unit with undefined value');
if (!x.equalBase(y)) throw new Error('Units do not match');
var res = x.clone();
res.value += y.value;
res.fixPrefix = false;
return res;
},
'string, string': function (x, y) {
return x + y;
}
});
return addScalar;
}
exports.name = 'addScalar';
exports.factory = factory;