From a5692e1d0a176dc2320b2b030accad86091e286e Mon Sep 17 00:00:00 2001 From: Harry Sarson Date: Sun, 23 Apr 2017 13:13:23 +0100 Subject: [PATCH 1/3] Extension of pow function to support Infinite exponents. --- lib/function/arithmetic/pow.js | 17 +++++++++++++++++ test/function/arithmetic/pow.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/function/arithmetic/pow.js b/lib/function/arithmetic/pow.js index 9ed92827d..47553248c 100644 --- a/lib/function/arithmetic/pow.js +++ b/lib/function/arithmetic/pow.js @@ -120,6 +120,23 @@ function factory (type, config, load, typed) { // Unable to express y as a fraction, so continue on } + + // x^Infinity === 0 if -1 < x < 1 + // A real number 0 is returned instead of complex(0) + if ((x*x < 1 && y === Infinity) || + (x*x > 1 && y === -Infinity)) { + return 0; + } + + // **for predictable mode** x^Infinity === NaN if x < -1 + // N.B. this behavour is different from `Math.pow` which gives + // (-2)^Infinity === Infinity + if (config.predictable && + ((x < -1 && y === Infinity) || + (x > -1 && x < 0 && y === -Infinity))) { + return NaN; + } + if (isInteger(y) || x >= 0 || config.predictable) { return Math.pow(x, y); } diff --git a/test/function/arithmetic/pow.test.js b/test/function/arithmetic/pow.test.js index 10897da97..267f1842c 100644 --- a/test/function/arithmetic/pow.test.js +++ b/test/function/arithmetic/pow.test.js @@ -131,6 +131,31 @@ describe('pow', function() { assert.throws(function () {pow(1, 2, 3)}, /TypeError: Too many arguments in function pow \(expected: 2, actual: 3\)/); }); + it('should handle infitie exponents', function() { + var Ptbl = mathPredictable; + + // TODO replace isNaN with complexInfinity when complex.js updates + + + assert.equal(math.pow( 3, Infinity), Infinity); + assert.equal(math.pow( 3, -Infinity), 0); + assert(isNaN(Ptbl.pow(-3, Infinity))); + assert( math.pow(-3, Infinity).isNaN()); + assert.equal(math.pow(-3, -Infinity), 0); + + assert.equal(math.pow( 0.3, Infinity), 0); + assert.equal(math.pow( 0.3, -Infinity), Infinity); + assert.equal(math.pow(-0.3, Infinity), 0); + assert(isNaN(Ptbl.pow(-0.3, -Infinity))); + assert( math.pow(-0.3, -Infinity).isNaN()); + + assert.equal(math.pow( Infinity, Infinity), Infinity); + assert.equal(math.pow( Infinity, -Infinity), 0); // https://www.wolframalpha.com/input/?i=infinity%5E(-infinity) + assert(isNaN(Ptbl.pow(-Infinity, Infinity))); + assert( math.pow(-Infinity, Infinity).isNaN()); + assert.equal(math.pow(-Infinity, -Infinity), 0); + }); + it('should exponentiate a complex number to the given power', function() { approx.deepEqual(pow(complex(3, 0), 2), complex(9, 0)); approx.deepEqual(pow(complex(0, 2), 2), complex(-4, 0)); From 9075a6b81ea4cfed2b388686d9a44dc81e5aefca Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Mon, 24 Apr 2017 08:28:05 +0200 Subject: [PATCH 2/3] Updated history --- HISTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 8da6eba36..9d76cffca 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ # History +## not yet released, version 3.12.1 + +- Improved handling of powers of `Infinity`. Thanks @HarrySarson. + ## 2017-04-17, version 3.12.0 From 692519e330332ffc72385294b08654ecefa18ffb Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Mon, 24 Apr 2017 08:39:36 +0200 Subject: [PATCH 3/3] Fixed wrong formatting of complex NaN (see 804) --- HISTORY.md | 4 +++- lib/type/complex/Complex.js | 14 +++++++------- test/function/string/format.test.js | 4 ++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9d76cffca..3bd4675d2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,7 +2,9 @@ ## not yet released, version 3.12.1 -- Improved handling of powers of `Infinity`. Thanks @HarrySarson. +- Fixed #804 + - Improved handling of powers of `Infinity`. Thanks @HarrySarson. + - Fixed wrong formatting of complex NaN. ## 2017-04-17, version 3.12.0 diff --git a/lib/type/complex/Complex.js b/lib/type/complex/Complex.js index 966fe3c5b..ba8cac2bb 100644 --- a/lib/type/complex/Complex.js +++ b/lib/type/complex/Complex.js @@ -78,18 +78,18 @@ function factory (type, config, load, typed, math) { } } else { // complex value - if (im > 0) { + if (im < 0) { + if (im == -1) { + str = strRe + ' - i'; + } else { + str = strRe + ' - ' + (/[\d-.]/.test(strIm.charAt(0)) ? strIm.substring(1) : strIm) + 'i'; + } + } else { if (im == 1) { str = strRe + ' + i'; } else { str = strRe + ' + ' + strIm + 'i'; } - } else { - if (im == -1) { - str = strRe + ' - i'; - } else { - str = strRe + ' - ' + strIm.substring(1) + 'i'; - } } } return str; diff --git a/test/function/string/format.test.js b/test/function/string/format.test.js index 4aef38865..becadc3b2 100644 --- a/test/function/string/format.test.js +++ b/test/function/string/format.test.js @@ -27,6 +27,10 @@ describe('format', function() { assert.equal(math.format(math.divide(math.complex(2,5),3), 5), '0.66667 + 1.6667i'); assert.equal(math.format(math.divide(math.complex(2,5),3), {notation: 'fixed'}), '1 + 2i'); assert.equal(math.format(math.divide(math.complex(2,5),3), {notation: 'fixed', precision: 1}), '0.7 + 1.7i'); + + assert.equal(math.format(math.complex(NaN , NaN)), 'NaN + NaNi'); + assert.equal(math.format(math.complex(Infinity, Infinity)), 'Infinity + Infinityi'); + assert.equal(math.format(math.complex(Infinity, -Infinity)), 'Infinity - Infinityi'); }); describe('precision', function() {