This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In File::Copy, convert two regexps to explicit ranges, instead of using /i
[perl5.git] / lib / bigfloat.pl
index feaaa6e..82d0f5c 100644 (file)
@@ -1,16 +1,30 @@
+warn "Legacy library @{[(caller(0))[6]]} will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs. It is being used at @{[(caller)[1]]}, line @{[(caller)[2]]}.\n";
+
 package bigfloat;
 require "bigint.pl";
+#
+# This library is no longer being maintained, and is included for backward
+# compatibility with Perl 4 programs which may require it.
+# This legacy library is deprecated and will be removed in a future
+# release of perl.
+#
+# In particular, this should not be used as an example of modern Perl
+# programming techniques.
+#
+# Suggested alternative: Math::BigFloat
 
 # Arbitrary length float math package
 #
+# by Mark Biggar
+#
 # number format
 #   canonical strings have the form /[+-]\d+E[+-]\d+/
-#   Input values can have inbedded whitespace
+#   Input values can have embedded whitespace
 # Error returns
 #   'NaN'           An input parameter was "Not a Number" or 
 #                       divide by zero or sqrt of negative number
 # Division is computed to 
-#   max($div_scale,length(dividend).length(divisor)) 
+#   max($div_scale,length(dividend)+length(divisor)) 
 #   digits by default.
 # Also used for default sqrt scale
 
@@ -40,8 +54,10 @@ $rnd_mode = 'even';
 sub main'fnorm { #(string) return fnum_str
     local($_) = @_;
     s/\s+//g;                               # strip white space
-    if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/ && "$2$4" ne '') {
-       &norm(($1 ? "$1$2$4" : "+$2$4"),(($4 ne '') ? $6-length($4) : $6));
+    if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
+         && ($2 ne '' || defined($4))) {
+       my $x = defined($4) ? $4 : '';
+       &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
     } else {
        'NaN';
     }
@@ -66,14 +82,20 @@ sub norm { #(mantissa, exponent) return fnum_str
 # negation
 sub main'fneg { #(fnum_str) return fnum_str
     local($_) = &'fnorm($_[0]);
-    substr($_,0,1) =~ tr/+-/-+/ if ($_ ne '+0E+0'); # flip sign
+    vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
+    if ( ord("\t") == 9 ) { # ascii
+        s/^H/N/;
+    }
+    else { # ebcdic character set
+        s/\373/N/;
+    }
     $_;
 }
 
 # absolute value
 sub main'fabs { #(fnum_str) return fnum_str
     local($_) = &'fnorm($_[0]);
-    substr($_,0,1) = '+';                       # mash sign
+    s/^-/+/;                                  # mash sign
     $_;
 }
 
@@ -122,7 +144,7 @@ sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
        $scale = length($xm)-1 if (length($xm)-1 > $scale);
        $scale = length($ym)-1 if (length($ym)-1 > $scale);
        $scale = $scale + length($ym) - length($xm);
-       &norm(&round(&'bdiv($xm.('0' x $scale),$ym),$ym),
+       &norm(&round(&'bdiv($xm.('0' x $scale),$ym),&'babs($ym)),
            $xe-$ye-$scale);
     }
 }
@@ -182,10 +204,15 @@ sub main'ffround { #(fnum_str, scale) return fnum_str
            if ($xe < 1) {
                '+0E+0';
            } elsif ($xe == 1) {
-               &norm(&round('+0',"+0".substr($xm,1,1),"+10"), $scale);
+               # The first substr preserves the sign, which means that
+               # we'll pass a non-normalized "-0" to &round when rounding
+               # -0.006 (for example), purely so that &round won't lose
+               # the sign.
+               &norm(&round(substr($xm,0,1).'0',
+                     "+0".substr($xm,1,1),"+10"), $scale);
            } else {
-               &norm(&round(substr($xm,0,$trunc),
-                     "+0".substr($xm,$trunc,1),"+10"), $scale);
+               &norm(&round(substr($xm,0,$xe),
+                     "+0".substr($xm,$xe,1),"+10"), $scale);
            }
        }
     }
@@ -198,18 +225,13 @@ sub main'fcmp #(fnum_str, fnum_str) return cond_code
     local($x, $y) = (&'fnorm($_[0]),&'fnorm($_[1]));
     if ($x eq "NaN" || $y eq "NaN") {
        undef;
-    } elsif ($x eq $y) {
-       0;
-    } elsif (ord($x) != ord($y)) {
-       (ord($y) - ord($x));                # based on signs
     } else {
-       local($xm,$xe) = split('E',$x);
-       local($ym,$ye) = split('E',$y);
-       if ($xe ne $ye) {
-           ($xe - $ye) * (substr($x,0,1).'1');
-       } else {
-           &bigint'cmp($xm,$ym);           # based on value
-       }
+       ord($y) <=> ord($x)
+       ||
+       (  local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
+            (($xe <=> $ye) * (substr($x,0,1).'1')
+             || &bigint'cmp($xm,$ym))
+       );
     }
 }
 \f