Skip to content

Commit

Permalink
Modifies exp() and pow() and specs behaviour of z^inf and inf^z.
Browse files Browse the repository at this point in the history
Previous behaviour was inconsistant and was not tested for. This
commit ensures that z^inf and inf^z return reasonable and consistant
values.

See rawify#24 for the dicussion on the behaviour which can be summaried as:

    z ^ Infinity === NaN
    Infinity ^ z === Infinity if Im(z) === 0 and Re(z) > 0
    Infinity ^ z === 0 if Re(z) < 0
    Infinity ^ 0 === 1
    Infinity ^ z === NaN otherwise
  • Loading branch information
harrysarson committed Mar 15, 2018
1 parent bcbf479 commit 027d097
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
21 changes: 19 additions & 2 deletions complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,18 @@
return Complex['ONE'];
}

if (!z.isFinite()) {
return Complex['NAN'];
}

if (!this.isFinite()) {
return z['re'] < 0
? Complex['ZERO']
: z['im'] === 0 && z['re'] > 0 // should we epsilon ball here
? Complex['INFINITY']
: Complex['NAN'];
}

// If the exponent is real
if (z['im'] === 0) {

Expand Down Expand Up @@ -549,9 +561,10 @@

const tmp = Math.exp(this['re']);

if (this['im'] === 0) {
//return new Complex(tmp, 0);
if (!this.isFinite()) {
return Complex['NAN'];
}

return new Complex(
tmp * Math.cos(this['im']),
tmp * Math.sin(this['im']));
Expand All @@ -573,6 +586,10 @@
= expm1(a)*cos(b) + cosm1(b) + j*exp(a)*sin(b)
*/

if (!this.isFinite()) {
return Complex['NAN'];
}

const a = this['re'];
const b = this['im'];

Expand Down
58 changes: 58 additions & 0 deletions tests/complex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ var functionTests = [{
set: "3 + 2i",
fn: "exp",
expect: "-8.358532650935372 + 18.263727040666765i"
}, {
set: Infinity,
fn: "exp",
expect: "NaN"
}, {
set: Infinity,
fn: "expm1",
expect: "NaN"
}, {
set: "3 - 2i",
fn: "exp",
Expand Down Expand Up @@ -403,8 +411,58 @@ var functionTests = [{
}, {
set: "0-0i",
fn: "pow",
param: Infinity,
expect: "NaN"
}, {
set: "-1",
fn: "pow",
param: Infinity,
expect: "NaN"
}, {
set: "1",
fn: "pow",
param: Infinity,
expect: "NaN"
}, {
set: "4 - 7i",
fn: "pow",
param: Infinity,
expect: "NaN"
}, {
set: "-5.9 + 8i",
fn: "pow",
param: Infinity,
expect: "NaN"
}, {
set: Infinity,
fn: "pow",
param: 0,
expect: "1"
}, {
set: Infinity,
fn: "pow",
param: -1,
expect: "0"
}, {
set: Infinity,
fn: "pow",
param: 1,
expect: "Infinity"
}, {
set: Infinity,
fn: "pow",
param: "-1.9 + 7i",
expect: "0"
}, {
set: Infinity,
fn: "pow",
param: "0.3",
expect: "Infinity"
}, {
set: Infinity,
fn: "pow",
param: "88",
expect: "Infinity"
}, {
set: "1 + 4i",
fn: "sqrt",
Expand Down

0 comments on commit 027d097

Please sign in to comment.