mirror of
https://github.com/josdejong/mathjs.git
synced 2026-02-01 16:07:46 +00:00
Added support for Fraction in functions compare, equal, larger, largerEq, smaller, smallerEq, unequal.
This commit is contained in:
parent
fca1b8c98c
commit
a0ad13b9e0
@ -41,9 +41,9 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal, smaller, smallerEq, larger, largerEq
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Number | BigNumber | Array | Matrix} Returns the result of the comparison: 1, 0 or -1.
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison: 1, 0 or -1.
|
||||
*/
|
||||
var compare = typed('compare', {
|
||||
|
||||
@ -59,6 +59,10 @@ function factory (type, config, load, typed) {
|
||||
return new type.BigNumber(x.cmp(y));
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return new type.Fraction(x.compare(y));
|
||||
},
|
||||
|
||||
'Complex, Complex': function () {
|
||||
throw new TypeError('No ordering relation is defined for complex numbers');
|
||||
},
|
||||
|
||||
@ -26,9 +26,9 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} x First matrix to compare
|
||||
* @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} y Second matrix to compare
|
||||
* @return {Number | BigNumber | Complex | Unit | Array | Matrix}
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | Unit | Array | Matrix | null} x First matrix to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | Unit | Array | Matrix | null} y Second matrix to compare
|
||||
* @return {Number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
|
||||
* Returns true when the input matrices have the same size and each of their elements is equal.
|
||||
*/
|
||||
return typed('deepEqual', {
|
||||
|
||||
@ -7,8 +7,8 @@ function factory (type, config, load, typed) {
|
||||
/**
|
||||
* Test whether two values are equal.
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Complex | Unit | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Complex | null} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | Unit | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | null} y Second value to compare
|
||||
* @return {Boolean} Returns true when the compared values are equal, else returns false
|
||||
* @private
|
||||
*/
|
||||
@ -26,6 +26,10 @@ function factory (type, config, load, typed) {
|
||||
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));
|
||||
|
||||
@ -38,8 +38,8 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal, smaller, smallerEq, largerEq, compare
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Boolean | Array | Matrix} Returns true when the x is larger than y, else returns false
|
||||
*/
|
||||
var larger = typed('larger', {
|
||||
@ -56,6 +56,10 @@ function factory (type, config, load, typed) {
|
||||
return x.gt(y);
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return x.compare(y) === 1;
|
||||
},
|
||||
|
||||
'Complex, Complex': function () {
|
||||
throw new TypeError('No ordering relation is defined for complex numbers');
|
||||
},
|
||||
|
||||
@ -34,8 +34,8 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal, smaller, smallerEq, larger, compare
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Boolean | Array | Matrix} Returns true when the x is larger or equal to y, else returns false
|
||||
*/
|
||||
var largerEq = typed('largerEq', {
|
||||
@ -52,6 +52,10 @@ function factory (type, config, load, typed) {
|
||||
return x.gte(y);
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return x.compare(y) !== -1;
|
||||
},
|
||||
|
||||
'Complex, Complex': function () {
|
||||
throw new TypeError('No ordering relation is defined for complex numbers');
|
||||
},
|
||||
|
||||
@ -38,8 +38,8 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal, smallerEq, smaller, smallerEq, compare
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
|
||||
*/
|
||||
var smaller = typed('smaller', {
|
||||
@ -56,6 +56,10 @@ function factory (type, config, load, typed) {
|
||||
return x.lt(y);
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return x.compare(y) === -1;
|
||||
},
|
||||
|
||||
'Complex, Complex': function (x, y) {
|
||||
throw new TypeError('No ordering relation is defined for complex numbers');
|
||||
},
|
||||
|
||||
@ -33,8 +33,8 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, unequal, smaller, larger, largerEq, compare
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Unit | String | Array | Matrix | null} y Second value to compare
|
||||
* @return {Boolean | Array | Matrix} Returns true when the x is smaller than y, else returns false
|
||||
*/
|
||||
var smallerEq = typed('smallerEq', {
|
||||
@ -51,6 +51,10 @@ function factory (type, config, load, typed) {
|
||||
return x.lte(y);
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return x.compare(y) !== 1;
|
||||
},
|
||||
|
||||
'Complex, Complex': function () {
|
||||
throw new TypeError('No ordering relation is defined for complex numbers');
|
||||
},
|
||||
|
||||
@ -50,8 +50,8 @@ function factory (type, config, load, typed) {
|
||||
*
|
||||
* equal, deepEqual, smaller, smallerEq, larger, largerEq, compare
|
||||
*
|
||||
* @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to compare
|
||||
* @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} y Second value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to compare
|
||||
* @param {Number | BigNumber | Fraction | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} y Second value to compare
|
||||
* @return {Boolean | Array | Matrix} Returns true when the compared values are unequal, else returns false
|
||||
*/
|
||||
var unequal = typed('unequal', {
|
||||
@ -170,6 +170,10 @@ function factory (type, config, load, typed) {
|
||||
return !x.eq(y);
|
||||
},
|
||||
|
||||
'Fraction, Fraction': function (x, y) {
|
||||
return x.compare(y) !== 0;
|
||||
},
|
||||
|
||||
'Complex, Complex': function (x, y) {
|
||||
return !nearlyEqual(x.re, y.re, config.epsilon) ||
|
||||
!nearlyEqual(x.im, y.im, config.epsilon);
|
||||
|
||||
@ -75,6 +75,21 @@ describe('compare', function() {
|
||||
assert.deepEqual(compare(true, bignumber(0)), bignumber(1));
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert(compare(math.fraction(1,3), math.fraction(1,6)) instanceof math.type.Fraction);
|
||||
|
||||
assert.equal(compare(math.fraction(3), math.fraction(2)).valueOf(), 1);
|
||||
assert.equal(compare(math.fraction(2), math.fraction(3)).valueOf(), -1);
|
||||
assert.equal(compare(math.fraction(3), math.fraction(3)).valueOf(), 0);
|
||||
|
||||
assert.strictEqual(compare(math.add(math.fraction(0.1), math.fraction(0.2)), math.fraction(0.3)).valueOf(), 0); // this would fail with numbers
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(compare(1, math.fraction(1,3)), 1);
|
||||
assert.strictEqual(compare(math.fraction(1,3), 1), -1);
|
||||
});
|
||||
|
||||
it('should add two measures of the same unit', function() {
|
||||
assert.equal(compare(unit('100cm'), unit('10inch')), 1);
|
||||
assert.equal(compare(unit('99cm'), unit('1m')), -1);
|
||||
|
||||
@ -97,6 +97,19 @@ describe('equal', function() {
|
||||
assert.deepEqual(equal(bignumber(6), math.complex(6, 4)), false);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(equal(math.fraction(3), math.fraction(2)).valueOf(), false);
|
||||
assert.strictEqual(equal(math.fraction(2), math.fraction(3)).valueOf(), false);
|
||||
assert.strictEqual(equal(math.fraction(3), math.fraction(3)).valueOf(), true);
|
||||
|
||||
assert.strictEqual(equal(math.add(math.fraction(0.1), math.fraction(0.2)), math.fraction(0.3)).valueOf(), true); // this would fail with numbers
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(equal(1, math.fraction(1,3)), false);
|
||||
assert.strictEqual(equal(math.fraction(2), 2), true);
|
||||
});
|
||||
|
||||
it('should compare two units correctly', function() {
|
||||
assert.equal(equal(unit('100cm'), unit('10inch')), false);
|
||||
assert.equal(equal(unit('100cm'), unit('1m')), true);
|
||||
|
||||
@ -79,6 +79,17 @@ describe('larger', function() {
|
||||
assert.equal(larger(true, bignumber(0)), true);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(larger(math.fraction(3), math.fraction(2)).valueOf(), true);
|
||||
assert.strictEqual(larger(math.fraction(2), math.fraction(3)).valueOf(), false);
|
||||
assert.strictEqual(larger(math.fraction(3), math.fraction(3)).valueOf(), false);
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(larger(1, math.fraction(1,3)), true);
|
||||
assert.strictEqual(larger(math.fraction(2), 2), false);
|
||||
});
|
||||
|
||||
it('should add two measures of the same unit', function() {
|
||||
assert.equal(larger(unit('100cm'), unit('10inch')), true);
|
||||
assert.equal(larger(unit('99cm'), unit('1m')), false);
|
||||
|
||||
@ -81,6 +81,17 @@ describe('largerEq', function() {
|
||||
assert.equal(largerEq(true, bignumber(1)), true);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(largerEq(math.fraction(3), math.fraction(2)).valueOf(), true);
|
||||
assert.strictEqual(largerEq(math.fraction(2), math.fraction(3)).valueOf(), false);
|
||||
assert.strictEqual(largerEq(math.fraction(3), math.fraction(3)).valueOf(), true);
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(largerEq(1, math.fraction(1,3)), true);
|
||||
assert.strictEqual(largerEq(math.fraction(2), 2), true);
|
||||
});
|
||||
|
||||
it('should compare two units correctly', function() {
|
||||
assert.equal(largerEq(unit('100cm'), unit('10inch')), true);
|
||||
assert.equal(largerEq(unit('99cm'), unit('1m')), false);
|
||||
|
||||
@ -86,6 +86,17 @@ describe('smaller', function() {
|
||||
assert.deepEqual(smaller(true, bignumber(1)), false);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(smaller(math.fraction(3), math.fraction(2)).valueOf(), false);
|
||||
assert.strictEqual(smaller(math.fraction(2), math.fraction(3)).valueOf(), true);
|
||||
assert.strictEqual(smaller(math.fraction(3), math.fraction(3)).valueOf(), false);
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(smaller(1, math.fraction(1,3)), false);
|
||||
assert.strictEqual(smaller(math.fraction(2), 2), false);
|
||||
});
|
||||
|
||||
it('should compare two measures of the same unit correctly', function() {
|
||||
assert.equal(smaller(unit('100cm'), unit('10inch')), false);
|
||||
assert.equal(smaller(unit('99cm'), unit('1m')), true);
|
||||
|
||||
@ -84,6 +84,17 @@ describe('smallerEq', function() {
|
||||
assert.deepEqual(smallerEq(true, bignumber(1)), true);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(smallerEq(math.fraction(3), math.fraction(2)).valueOf(), false);
|
||||
assert.strictEqual(smallerEq(math.fraction(2), math.fraction(3)).valueOf(), true);
|
||||
assert.strictEqual(smallerEq(math.fraction(3), math.fraction(3)).valueOf(), true);
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(smallerEq(1, math.fraction(1,3)), false);
|
||||
assert.strictEqual(smallerEq(math.fraction(2), 2), true);
|
||||
});
|
||||
|
||||
it('should compare two measures of the same unit correctly', function() {
|
||||
assert.equal(smallerEq(unit('100cm'), unit('10inch')), false);
|
||||
assert.equal(smallerEq(unit('99cm'), unit('1m')), true);
|
||||
|
||||
@ -100,6 +100,17 @@ describe('unequal', function() {
|
||||
assert.deepEqual(unequal(bignumber(6), math.complex(6, 4)), true);
|
||||
});
|
||||
|
||||
it('should compare two fractions', function() {
|
||||
assert.strictEqual(unequal(math.fraction(3), math.fraction(2)).valueOf(), true);
|
||||
assert.strictEqual(unequal(math.fraction(2), math.fraction(3)).valueOf(), true);
|
||||
assert.strictEqual(unequal(math.fraction(3), math.fraction(3)).valueOf(), false);
|
||||
});
|
||||
|
||||
it('should compare mixed fractions and numbers', function() {
|
||||
assert.strictEqual(unequal(1, math.fraction(1,3)), true);
|
||||
assert.strictEqual(unequal(math.fraction(2), 2), false);
|
||||
});
|
||||
|
||||
it('should compare two quantitites of the same unit correctly', function() {
|
||||
assert.equal(unequal(unit('100cm'), unit('10inch')), true);
|
||||
assert.equal(unequal(unit('100cm'), unit('1m')), false);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user