Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
jos 2017-04-24 21:38:12 +02:00
commit b90305992f
5 changed files with 59 additions and 7 deletions

View File

@ -1,5 +1,11 @@
# History
## not yet released, version 3.12.1
- Fixed #804
- Improved handling of powers of `Infinity`. Thanks @HarrySarson.
- Fixed wrong formatting of complex NaN.
## 2017-04-17, version 3.12.1

View File

@ -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);
}

View File

@ -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;

View File

@ -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));

View File

@ -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() {