From: Perl 5 Porters Date: Sun, 8 Sep 1996 00:40:34 +0000 (+0000) Subject: perl 5.003_05: lib/Math/Complex.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3f193590c5874b4c5b016955e0e787f60536c0dc;p=p5sagit%2Fp5-mst-13.2.git perl 5.003_05: lib/Math/Complex.pm There was a mistake in the sqrt routine in lib/Math/Complex.pm that gave wrong answers when the magnitude of the imaginary part of the argument exceeded the magnitude of the real part. Line 69 had too many sqrt($y)'s. Further, expressions were re-arranged so that calls to the expensive real sqrt() routine were reduced from 4 to 2 in this case. --- diff --git a/lib/Math/Complex.pm b/lib/Math/Complex.pm index 969f3c2..2a571aa 100644 --- a/lib/Math/Complex.pm +++ b/lib/Math/Complex.pm @@ -62,11 +62,11 @@ use overload $y = abs($zi); if ($x >= $y) { $r = $y/$x; - $w = sqrt($x) * sqrt(0.5*(1.0+sqrt(1.0+$r*$r))); + $w = sqrt(0.5 * $x * (1.0+sqrt(1.0+$r*$r))); } else { $r = $x/$y; - $w = sqrt($y) * sqrt($y) * sqrt(0.5*($r+sqrt(1.0+$r*$r))); + $w = sqrt(0.5 * ($x + $y*sqrt(1.0+$r*$r))); } if ( $zr >= 0) { @$c = ($w, $zi/(2 * $w) ); @@ -110,7 +110,7 @@ sub stringify { $re = $x if ($x); if ($y == 1) {$im = 'i';} elsif ($y == -1){$im = '-i';} - elsif ($y) {$im = "${y}i"; } + elsif ($y) {$im = $y . 'i'; } local $_ = $re.'+'.$im; s/\+-/-/;