Skip to content

Infinity fixes #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ Rounds the complex number parts

boolean equals(n)
---
Checks if both numbers are exactly the same
Checks if both numbers are exactly the same, if both numbers are infinite they
are considered **not** equal.

boolean isNaN()
---
Expand Down
60 changes: 34 additions & 26 deletions complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,10 @@
} else if ('abs' in a && 'arg' in a) {

P['re'] = a['abs'] * Math.cos(a['arg']);
P['im'] = a['arg'] === 0
? 0
: a['abs'] * Math.sin(a['arg']);
P['im'] = NaNtoZero(a['abs'] * Math.sin(a['arg']));
} else if ('r' in a && 'phi' in a) {
P['re'] = a['r'] * Math.cos(a['phi']);
P['im'] = a['phi'] === 0
? 0
: a['r'] * Math.sin(a['phi']);
P['im'] = NaNtoZero(a['r'] * Math.sin(a['phi']));
} else {
parser_exit();
}
Expand Down Expand Up @@ -237,6 +233,14 @@

/**
* Convert NaN to zero.
*
* **Note:** wherever this is used there is a risk of losing an actual NaN.
* Usually `NaNtoZero()` is used for one of the two arguments to `new Complex()`
* and therefore if the complex number should be NaN the other argument to `new
* Complex()` will be NaN and so it is ok. Care does need to be taken though.
*
* @private
*
*/
function NaNtoZero(x) {
if (Number.isNaN(x)) {
Expand All @@ -257,7 +261,7 @@
/**
* Test if a complex object `{re, im}` is Finite
*/
function complexIsFinte(z) {
function complexIsFinite(z) {
return isFinite(z['re']) && isFinite(z['im']);
}

Expand Down Expand Up @@ -349,15 +353,14 @@

if (complexIsNaN(this)
|| complexIsNaN(P)
|| (!complexIsFinte(this) && complexIsZero(P))
|| (!complexIsFinte(P) && complexIsZero(this))
|| (!complexIsFinite(this) && complexIsZero(P))
|| (!complexIsFinite(P) && complexIsZero(this))
){
return Complex.COMPLEX_NAN;
}

// Some values will be coersed to ComplexNaN by constructor
// make sure not to short cut this parsing.

// make sure not to shortcut this parsing.
return new Complex(
NaNtoZero(this['re'] * P['re']) - NaNtoZero(this['im'] * P['im']),
NaNtoZero(this['re'] * P['im']) + NaNtoZero(this['im'] * P['re'])
Expand All @@ -383,9 +386,11 @@
if (0 === d) {
if (0 === c) {
// Divisor is zero
return new Complex(
(a !== 0) ? (a / 0) : Infinity,
(b !== 0) ? (b / 0) : Infinity);
if (complexIsZero(this)) {
return Complex['NaN'];
} else {
return Complex['ComplexInfinity'];
}
} else {
// Divisor is real
return new Complex(a / c, b / c);
Expand Down Expand Up @@ -481,7 +486,7 @@

if (!isFinite(b)) {
// Return complex infinity
return Complex['Infinity'];
return Complex['ComplexInfinity'];
}

return new Complex(
Expand Down Expand Up @@ -531,12 +536,9 @@

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

if (this['im'] === 0) {
//return new Complex(tmp, 0);
}
return new Complex(
tmp * Math.cos(this['im']),
tmp * Math.sin(this['im']));
NaNtoZero(tmp * Math.sin(this['im'])));
},

/**
Expand Down Expand Up @@ -591,7 +593,7 @@
var b = this['im'];

return new Complex(
Math.sin(a) * Math.cosh(b),
NaNtoZero(Math.sin(a) * Math.cosh(b)),
Math.cos(a) * Math.sinh(b));
},

Expand All @@ -609,7 +611,7 @@

return new Complex(
Math.cos(a) * Math.cosh(b),
-Math.sin(a) * Math.sinh(b));
NaNtoZero(-Math.sin(a) * Math.sinh(b)));
},

/**
Expand Down Expand Up @@ -854,7 +856,7 @@

return new Complex(
Math.sinh(a) * Math.cos(b),
Math.cosh(a) * Math.sin(b));
NaNtoZero(Math.cosh(a) * Math.sin(b)));
},

/**
Expand All @@ -871,7 +873,7 @@

return new Complex(
Math.cosh(a) * Math.cos(b),
Math.sinh(a) * Math.sin(b));
NaNtoZero(Math.sinh(a) * Math.sin(b)));
},

/**
Expand Down Expand Up @@ -1122,7 +1124,11 @@
var d = a * a + b * b;

if (d === 0) {
return Complex['Infinity'];
return Complex['ComplexInfinity'];
}

if (!complexIsFinite(this)) {
return Complex['ZERO'];
}

return new Complex(
Expand Down Expand Up @@ -1195,6 +1201,8 @@
/**
* Compares two complex numbers
*
* **Note:** new Complex(Infinity).equals(Infinity) === false
*
* @returns {boolean}
*/
'equals': function(a, b) {
Expand Down Expand Up @@ -1301,7 +1309,7 @@
* @returns {boolean}
*/
'isFinite': function() {
return complexIsNaN(this);
return complexIsFinite(this);
},
};

Expand All @@ -1310,7 +1318,7 @@
Complex['I'] = new Complex(0, 1);
Complex['PI'] = new Complex(Math.PI, 0);
Complex['E'] = new Complex(Math.E, 0);
Complex['Infinity'] = new Complex(Infinity, Infinity);
Complex['ComplexInfinity'] = new Complex(Infinity, Infinity);
Complex['NaN'] = new Complex(NaN, NaN);
Complex['EPSILON'] = 1e-16;

Expand Down
2 changes: 1 addition & 1 deletion tests/complex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var tests = [{
set: "0",
fn: "div",
param: "0",
expect: "∞ + ∞i"
expect: "NaN"
}, {
set: "4 + 2i",
fn: "div",
Expand Down