perl 5.003_05: lib/Math/Complex.pm
authorPerl 5 Porters <perl5-porters@africa.nicoh.com>
Sun, 8 Sep 1996 00:40:34 +0000 (00:40 +0000)
committerAndy Dougherty <doughera@lafcol.lafayette.edu>
Sun, 8 Sep 1996 00:40:34 +0000 (00:40 +0000)
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.

lib/Math/Complex.pm

index 969f3c2..2a571aa 100644 (file)
@@ -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/\+-/-/;