From cdd7bb8a83cf9398bca77efe30450d33840a7bbd Mon Sep 17 00:00:00 2001 From: jos Date: Thu, 26 Feb 2015 21:51:25 +0100 Subject: [PATCH] Updated to typed-function 0.6.2, fixing a couple of bugs. Conversion from BigNumber to Complex added. --- lib/function/arithmetic/log.js | 5 ----- lib/function/arithmetic/multiply.js | 5 ----- lib/function/arithmetic/subtract.js | 6 ------ lib/math.js | 13 ++++++------- package.json | 2 +- test/expression/parse.test.js | 3 +-- test/function/arithmetic/add.test.js | 4 ++-- test/function/arithmetic/divide.test.js | 6 ++---- test/function/arithmetic/multiply.test.js | 3 +-- test/function/arithmetic/pow.test.js | 3 +-- test/function/arithmetic/subtract.test.js | 4 ++-- test/function/arithmetic/unaryMinus.test.js | 2 +- 12 files changed, 17 insertions(+), 39 deletions(-) diff --git a/lib/function/arithmetic/log.js b/lib/function/arithmetic/log.js index 3e659c4f2..804fe5bf4 100644 --- a/lib/function/arithmetic/log.js +++ b/lib/function/arithmetic/log.js @@ -44,11 +44,6 @@ module.exports = function (config) { var log = typed('log', { 'number': _logNumber, - // FIXME: boolean and null should be converted automatically to number (is this not working because of any? - 'boolean | null': function(x) { - return _logNumber(+x); - }, - 'Complex': function (x) { return new Complex ( Math.log(Math.sqrt(x.re * x.re + x.im * x.im)), diff --git a/lib/function/arithmetic/multiply.js b/lib/function/arithmetic/multiply.js index 73ffcd003..5776dc3fe 100644 --- a/lib/function/arithmetic/multiply.js +++ b/lib/function/arithmetic/multiply.js @@ -94,11 +94,6 @@ module.exports = function (config) { 'any, Array | Matrix': function (x, y) { return collection.deepMap2(x, y, multiply); - }, - - // FIXME: this should be redundant, should be handled by typed-function - 'boolean, boolean': function (x, y) { - return x * y; } }); diff --git a/lib/function/arithmetic/subtract.js b/lib/function/arithmetic/subtract.js index 51fe5ed03..e58d3db64 100644 --- a/lib/function/arithmetic/subtract.js +++ b/lib/function/arithmetic/subtract.js @@ -39,12 +39,6 @@ module.exports = function (config) { * Subtraction of `x` and `y` */ var subtract = typed('subtract', { - // FIXME: should not be needed to define subtract for booleans - // TODO: return either number or BigNumber depending on config? - 'boolean, boolean': function (x, y) { - return x - y; - }, - 'number, number': function (x, y) { return x - y; }, diff --git a/lib/math.js b/lib/math.js index f8f41f30a..a2b5b1944 100644 --- a/lib/math.js +++ b/lib/math.js @@ -179,13 +179,12 @@ function create (config) { return new Complex(x, 0); } }, { - // TODO: convert from BigNumber to complex? - // from: 'BigNumber', - // to: 'Complex', - // convert: function (x) { - // return new Complex(x.toNumber(), 0); - // } - //}, { + from: 'BigNumber', + to: 'Complex', + convert: function (x) { + return new Complex(x.toNumber(), 0); + } + }, { from: 'number', to: 'string', convert: function (x) { diff --git a/package.json b/package.json index 6fa31b05e..c7fc2ba13 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ ], "dependencies": { "decimal.js": "^4.0.1", - "typed-function": "^0.6.1" + "typed-function": "^0.6.2" }, "devDependencies": { "webpack": "latest", diff --git a/test/expression/parse.test.js b/test/expression/parse.test.js index e358737c4..0128c3171 100644 --- a/test/expression/parse.test.js +++ b/test/expression/parse.test.js @@ -1396,8 +1396,7 @@ describe('parse', function() { bigmath.matrix([new BigNumber(0.7), new BigNumber(0.8)])); }); - // TODO: cleanup once decided to not downgrade BigNumber to number - it.skip('should work with complex numbers (downgrades bignumbers to number)', function() { + it('should work with complex numbers (downgrades bignumbers to number)', function() { assert.deepEqual(bigmath.eval('3i'), new Complex(0, 3)); assert.deepEqual(bigmath.eval('2 + 3i'), new Complex(2, 3)); assert.deepEqual(bigmath.eval('2 * i'), new Complex(0, 2)); diff --git a/test/function/arithmetic/add.test.js b/test/function/arithmetic/add.test.js index 6617511b3..f245069b7 100644 --- a/test/function/arithmetic/add.test.js +++ b/test/function/arithmetic/add.test.js @@ -58,8 +58,8 @@ describe('add', function() { }); it('should add mixed complex numbers and BigNumbers', function() { - assert.throws(function () {add(math.complex(3, -4), new BigNumber(2))}, /TypeError: Unexpected type of argument \(expected: Array or Matrix, actual: BigNumber, index: 1\)/); - assert.throws(function () {add(new BigNumber(2), math.complex(3, -4))}, /TypeError: Unexpected type of argument \(expected: Array or Matrix, actual: Complex, index: 1\)/); + assert.deepEqual(add(math.complex(3, -4), new BigNumber(2)), math.complex(5, -4)); + assert.deepEqual(add(new BigNumber(2), math.complex(3, -4)), math.complex(5, -4)); }); it('should add two complex numbers', function() { diff --git a/test/function/arithmetic/divide.test.js b/test/function/arithmetic/divide.test.js index 5d703104a..2f5beab1c 100644 --- a/test/function/arithmetic/divide.test.js +++ b/test/function/arithmetic/divide.test.js @@ -100,10 +100,8 @@ describe('divide', function() { }); it('should divide mixed complex numbers and bignumbers', function() { - //assert.deepEqual(divide(math.complex(6, -4), bignumber(2)), math.complex(3, -2)); - //assert.deepEqual(divide(bignumber(1), math.complex(2, 4)), math.complex(0.1, -0.2)); - assert.throws(function () {divide(math.complex(6, -4), bignumber(2))}, /TypeError: Unexpected type of argument \(expected: Complex or number, actual: BigNumber, index: 1\)/); - assert.throws(function () {divide(bignumber(1), math.complex(2, 4))}, /TypeError: Unexpected type of argument \(expected: BigNumber or number or boolean or null, actual: Complex, index: 1\)/); + assert.deepEqual(divide(math.complex(6, -4), bignumber(2)), math.complex(3, -2)); + assert.deepEqual(divide(bignumber(1), math.complex(2, 4)), math.complex(0.1, -0.2)); }); it('should divide units by a number', function() { diff --git a/test/function/arithmetic/multiply.test.js b/test/function/arithmetic/multiply.test.js index 26ee89166..39b113ff0 100644 --- a/test/function/arithmetic/multiply.test.js +++ b/test/function/arithmetic/multiply.test.js @@ -117,8 +117,7 @@ describe('multiply', function() { assert.deepEqual(multiply(2, math.complex(2, 4)), math.complex(4, 8)); }); - // TODO: remove this test - it.skip('should multiply mixed complex numbers and big numbers', function() { + it('should multiply mixed complex numbers and big numbers', function() { assert.deepEqual(multiply(math.complex(6, -4), math.bignumber(2)), math.complex(12, -8)); assert.deepEqual(multiply(math.bignumber(2), math.complex(2, 4)), math.complex(4, 8)); }); diff --git a/test/function/arithmetic/pow.test.js b/test/function/arithmetic/pow.test.js index fe7d035b9..270f7305a 100644 --- a/test/function/arithmetic/pow.test.js +++ b/test/function/arithmetic/pow.test.js @@ -121,8 +121,7 @@ describe('pow', function() { approx.deepEqual(pow(complex(1,1),complex(1,1)), complex('0.2739572538301211 + 0.5837007587586147i')); }); - // TODO: support mixed complex and bignumbers? - it.skip('should exponentiate a complex number to the given bignumber power', function() { + it('should exponentiate a complex number to the given bignumber power', function() { approx.deepEqual(pow(complex(3, 0), math.bignumber(2)), complex(9, 0)); approx.deepEqual(pow(complex(0, 2), math.bignumber(2)), complex(-4, 0)); }); diff --git a/test/function/arithmetic/subtract.test.js b/test/function/arithmetic/subtract.test.js index 7dde3a8d6..5ca3aebf3 100644 --- a/test/function/arithmetic/subtract.test.js +++ b/test/function/arithmetic/subtract.test.js @@ -73,8 +73,8 @@ describe('subtract', function() { }); it('should throw an error for mixed complex numbers and big numbers', function() { - assert.throws(function () {subtract(math.complex(3, 4), math.bignumber(10))}, /TypeError: Unexpected type of argument \(expected: Matrix or Array, actual: BigNumber, index: 1\)/); - assert.throws(function () {subtract(math.bignumber(10), math.complex(3, 4))}, /TypeError: Unexpected type of argument \(expected: Matrix or Array, actual: Complex, index: 1\)/); + assert.deepEqual(subtract(math.complex(3, 4), math.bignumber(10)), math.complex(-7, 4)); + assert.deepEqual(subtract(math.bignumber(10), math.complex(3, 4)), math.complex(7, -4)); }); it('should subtract two quantities of the same unit', function() { diff --git a/test/function/arithmetic/unaryMinus.test.js b/test/function/arithmetic/unaryMinus.test.js index e4fd3ab4a..3ada5bc5d 100644 --- a/test/function/arithmetic/unaryMinus.test.js +++ b/test/function/arithmetic/unaryMinus.test.js @@ -10,7 +10,7 @@ describe('unaryMinus', function() { assert.equal(math.unaryMinus(false), 0); }); - // FIXME: convert boolean to BigNumber in unaryMinus + // TODO: unary minus should return bignumber on boolean input when configured for bignumber it.skip('should return bignumber unary minus of a boolean', function () { var bigmath = math.create({number: 'bignumber'}); assert.deepEqual(bigmath.unaryMinus(true), bigmath.bignumber(-1));