Skip to content

Commit 027d097

Browse files
committed
Modifies exp() and pow() and specs behaviour of z^inf and inf^z.
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
1 parent bcbf479 commit 027d097

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

complex.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,18 @@
456456
return Complex['ONE'];
457457
}
458458

459+
if (!z.isFinite()) {
460+
return Complex['NAN'];
461+
}
462+
463+
if (!this.isFinite()) {
464+
return z['re'] < 0
465+
? Complex['ZERO']
466+
: z['im'] === 0 && z['re'] > 0 // should we epsilon ball here
467+
? Complex['INFINITY']
468+
: Complex['NAN'];
469+
}
470+
459471
// If the exponent is real
460472
if (z['im'] === 0) {
461473

@@ -549,9 +561,10 @@
549561

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

552-
if (this['im'] === 0) {
553-
//return new Complex(tmp, 0);
564+
if (!this.isFinite()) {
565+
return Complex['NAN'];
554566
}
567+
555568
return new Complex(
556569
tmp * Math.cos(this['im']),
557570
tmp * Math.sin(this['im']));
@@ -573,6 +586,10 @@
573586
= expm1(a)*cos(b) + cosm1(b) + j*exp(a)*sin(b)
574587
*/
575588

589+
if (!this.isFinite()) {
590+
return Complex['NAN'];
591+
}
592+
576593
const a = this['re'];
577594
const b = this['im'];
578595

tests/complex.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ var functionTests = [{
193193
set: "3 + 2i",
194194
fn: "exp",
195195
expect: "-8.358532650935372 + 18.263727040666765i"
196+
}, {
197+
set: Infinity,
198+
fn: "exp",
199+
expect: "NaN"
200+
}, {
201+
set: Infinity,
202+
fn: "expm1",
203+
expect: "NaN"
196204
}, {
197205
set: "3 - 2i",
198206
fn: "exp",
@@ -403,8 +411,58 @@ var functionTests = [{
403411
}, {
404412
set: "0-0i",
405413
fn: "pow",
414+
param: Infinity,
415+
expect: "NaN"
416+
}, {
417+
set: "-1",
418+
fn: "pow",
419+
param: Infinity,
420+
expect: "NaN"
421+
}, {
422+
set: "1",
423+
fn: "pow",
424+
param: Infinity,
425+
expect: "NaN"
426+
}, {
427+
set: "4 - 7i",
428+
fn: "pow",
429+
param: Infinity,
430+
expect: "NaN"
431+
}, {
432+
set: "-5.9 + 8i",
433+
fn: "pow",
434+
param: Infinity,
435+
expect: "NaN"
436+
}, {
437+
set: Infinity,
438+
fn: "pow",
406439
param: 0,
407440
expect: "1"
441+
}, {
442+
set: Infinity,
443+
fn: "pow",
444+
param: -1,
445+
expect: "0"
446+
}, {
447+
set: Infinity,
448+
fn: "pow",
449+
param: 1,
450+
expect: "Infinity"
451+
}, {
452+
set: Infinity,
453+
fn: "pow",
454+
param: "-1.9 + 7i",
455+
expect: "0"
456+
}, {
457+
set: Infinity,
458+
fn: "pow",
459+
param: "0.3",
460+
expect: "Infinity"
461+
}, {
462+
set: Infinity,
463+
fn: "pow",
464+
param: "88",
465+
expect: "Infinity"
408466
}, {
409467
set: "1 + 4i",
410468
fn: "sqrt",

0 commit comments

Comments
 (0)