mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
var nearlyEqual = require('../../utils/number').nearlyEqual;
|
|
|
|
function factory (type, config, load, typed) {
|
|
|
|
/**
|
|
* Test whether two values are equal.
|
|
*
|
|
* @param {number | BigNumber | Fraction | boolean | Complex | Unit} x First value to compare
|
|
* @param {number | BigNumber | Fraction | boolean | Complex} y Second value to compare
|
|
* @return {boolean} Returns true when the compared values are equal, else returns false
|
|
* @private
|
|
*/
|
|
var equalScalar = typed('equalScalar', {
|
|
|
|
'boolean, boolean': function (x, y) {
|
|
return x === y;
|
|
},
|
|
|
|
'number, number': function (x, y) {
|
|
return x === y || nearlyEqual(x, y, config.epsilon);
|
|
},
|
|
|
|
'BigNumber, BigNumber': function (x, y) {
|
|
return x.eq(y);
|
|
},
|
|
|
|
'Fraction, Fraction': function (x, y) {
|
|
return x.equals(y);
|
|
},
|
|
|
|
'Complex, Complex': function (x, y) {
|
|
return (x.re === y.re || nearlyEqual(x.re, y.re, config.epsilon)) &&
|
|
(x.im === y.im || nearlyEqual(x.im, y.im, config.epsilon));
|
|
},
|
|
|
|
'Unit, Unit': function (x, y) {
|
|
if (!x.equalBase(y)) {
|
|
throw new Error('Cannot compare units with different base');
|
|
}
|
|
return equalScalar(x.value, y.value);
|
|
},
|
|
|
|
'string, string': function (x, y) {
|
|
return x === y;
|
|
}
|
|
});
|
|
|
|
return equalScalar;
|
|
}
|
|
|
|
exports.factory = factory;
|