This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Math::BigInt to version 1.98
[perl5.git] / dist / Math-BigInt / lib / Math / BigInt / Calc.pm
index 4e54f88..a24400b 100644 (file)
@@ -4,7 +4,7 @@ use 5.006002;
 use strict;
 # use warnings;        # dont use warnings for older Perls
 
-our $VERSION = '0.53';
+our $VERSION = '0.57';
 
 # Package to store unsigned big integers in decimal and do math with them
 
@@ -60,7 +60,7 @@ sub _base_len
       $BASE = int("1e".$BASE_LEN);
       $MAX_VAL = $BASE-1;
       return $BASE_LEN unless wantarray;
-      return ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL, $BASE);
+      return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL,);
       }
 
     # find whether we can use mul or div in mul()/div()
@@ -95,7 +95,7 @@ sub _base_len
       }
     }
   return $BASE_LEN unless wantarray;
-  return ($BASE_LEN, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL, $BASE);
+  return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL);
   }
 
 sub _new
@@ -189,7 +189,7 @@ BEGIN
   $XOR_MASK = __PACKAGE__->_new( ( 2 ** $XOR_BITS ));
   $OR_MASK = __PACKAGE__->_new( ( 2 ** $OR_BITS ));
 
-  # We can compute the approximate lenght no faster than the real length:
+  # We can compute the approximate length no faster than the real length:
   *_alen = \&_len;
   }
 
@@ -595,7 +595,7 @@ sub _div_use_mul
 
   my ($c,$x,$yorg) = @_;
   
-  # the general div algorithmn here is about O(N*N) and thus quite slow, so
+  # the general div algorithm here is about O(N*N) and thus quite slow, so
   # we first check for some special cases and use shortcuts to handle them.
 
   # This works, because we store the numbers in a chunked format where each
@@ -785,7 +785,7 @@ sub _div_use_div_64
   my ($c,$x,$yorg) = @_;
 
   use integer;
-  # the general div algorithmn here is about O(N*N) and thus quite slow, so
+  # the general div algorithm here is about O(N*N) and thus quite slow, so
   # we first check for some special cases and use shortcuts to handle them.
 
   # This works, because we store the numbers in a chunked format where each
@@ -976,7 +976,7 @@ sub _div_use_div
   # in list context
   my ($c,$x,$yorg) = @_;
 
-  # the general div algorithmn here is about O(N*N) and thus quite slow, so
+  # the general div algorithm here is about O(N*N) and thus quite slow, so
   # we first check for some special cases and use shortcuts to handle them.
 
   # This works, because we store the numbers in a chunked format where each
@@ -1206,20 +1206,18 @@ sub _len
 
 sub _digit
   {
-  # return the nth digit, negative values count backward
-  # zero is rightmost, so _digit(123,0) will give 3
+  # Return the nth digit. Zero is rightmost, so _digit(123,0) gives 3.
+  # Negative values count from the left, so _digit(123, -1) gives 1.
   my ($c,$x,$n) = @_;
 
   my $len = _len('',$x);
 
-  $n = $len+$n if $n < 0;              # -1 last, -2 second-to-last
-  $n = abs($n);                                # if negative was too big
-  $len--; $n = $len if $n > $len;      # n to big?
-  
-  my $elem = int($n / $BASE_LEN);      # which array element
-  my $digit = $n % $BASE_LEN;          # which digit in this element
-  $elem = '0' x $BASE_LEN . @$x[$elem];        # get element padded with 0's
-  substr($elem,-$digit-1,1);
+  $n += $len if $n < 0;                 # -1 last, -2 second-to-last
+  return "0" if $n < 0 || $n >= $len;   # return 0 for digits out of range
+
+  my $elem = int($n / $BASE_LEN);       # which array element
+  my $digit = $n % $BASE_LEN;           # which digit in this element
+  substr("$x->[$elem]", -$digit-1, 1);
   }
 
 sub _zeros
@@ -2028,7 +2026,7 @@ sub _root
     # reset step to 2
     $step = _two();
     # add two, because $trial cannot be exactly the result (otherwise we would
-    # alrady have found it)
+    # already have found it)
     _add($c, $trial, $step);
  
     # and now add more and more (2,4,6,8,10 etc)
@@ -2598,7 +2596,7 @@ the same terms as Perl itself.
 
 Original math code by Mark Biggar, rewritten by Tels L<http://bloodgate.com/>
 in late 2000.
-Seperated from BigInt and shaped API with the help of John Peacock.
+Separated from BigInt and shaped API with the help of John Peacock.
 
 Fixed, speed-up, streamlined and enhanced by Tels 2001 - 2007.