Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 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
Copy link
Contributor Author

@harrysarson harrysarson Mar 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the imaginary part of z be compared exactly to 0 here or should Complex['EPSILON'] be used?

? Complex['INFINITY']
: Complex['NAN'];
}

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

Expand Down Expand Up @@ -553,9 +565,10 @@

var 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 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 @@ -408,8 +416,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