Skip to content

Commit

Permalink
Added isFinite method
Browse files Browse the repository at this point in the history
  • Loading branch information
infusion committed May 28, 2017
1 parent a07dece commit c3074c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Complex.js is a well tested JavaScript library to work with complex number arith

Example
===

```js
var Complex = require('complex.js');

Expand Down Expand Up @@ -134,6 +134,10 @@ boolean isNaN()
---
Checks if the given number is not a number

boolean isFinite()
---
Checks if the given number is finite

Complex clone()
---
Returns a new Complex instance with the same real and imaginary properties
Expand Down
17 changes: 13 additions & 4 deletions complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@

parse(a, b); // mutates P

// Besides the addition/subtraction, this helps having a solution for rational Infinity
// Besides the addition/subtraction, this helps having a solution for real Infinity
if (P['im'] === 0 && this['im'] === 0) {
return new Complex(this['re'] * P['re'], 0);
}
Expand Down Expand Up @@ -330,7 +330,7 @@
(a !== 0) ? (a / 0) : 0,
(b !== 0) ? (b / 0) : 0);
} else {
// Divisor is rational
// Divisor is real
return new Complex(a / c, b / c);
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@

return new Complex(Math.pow(a, P['re']), 0);

} else if (a === 0) {
} else if (a === 0) { // If base is fully imaginary

switch (P['re'] % 4) {
case 0:
Expand Down Expand Up @@ -1212,8 +1212,17 @@
*
* @returns {boolean}
*/
isNaN: function() {
'isNaN': function() {
return isNaN(this['re']) || isNaN(this['im']);
},

/**
* Checks if the given complex number is finite
*
* @returns {boolean}
*/
'isFinite': function() {
return isFinite(this['re']) && isFinite(this['im']);
}
};

Expand Down

0 comments on commit c3074c3

Please sign in to comment.