Upgrade to CPAN-1.88_79
[p5sagit/p5-mst-13.2.git] / lib / bignum.pm
index db03d98..7796d47 100644 (file)
@@ -1,13 +1,14 @@
 package bignum;
-require 5.005;
+use 5.006002;
 
-$VERSION = '0.16';
+$VERSION = '0.19';
 use Exporter;
 @EXPORT_OK     = qw( ); 
 @EXPORT        = qw( inf NaN ); 
 @ISA           = qw( Exporter );
 
 use strict;
+use overload;
 
 ############################################################################## 
 
@@ -55,7 +56,20 @@ sub upgrade
 #    $Math::BigInt::upgrade = $_[0];
 #    $Math::BigFloat::upgrade = $_[0];
 #    }
-  return $Math::BigInt::upgrade;
+  $Math::BigInt::upgrade;
+  }
+
+sub _binary_constant
+  {
+  # this takes a binary/hexadecimal/octal constant string and returns it
+  # as string suitable for new. Basically it converts octal to decimal, and
+  # passes every thing else unmodified back.
+  my $string = shift;
+
+  return Math::BigInt->new($string) if $string =~ /^0[bx]/;
+
+  # so it must be an octal constant
+  Math::BigInt->from_oct($string);
   }
 
 sub import 
@@ -63,7 +77,7 @@ sub import
   my $self = shift;
 
   # some defaults
-  my $lib = 'Calc';
+  my $lib = '';
   my $upgrade = 'Math::BigFloat';
   my $downgrade = 'Math::BigInt';
 
@@ -140,9 +154,10 @@ sub import
       }
     require Math::BigInt if $_lite == 0;       # not already loaded?
     $class = 'Math::BigInt';                   # regardless of MBIL or not
-    } 
+    }
+  push @import, 'try' => $lib if $lib ne ''; 
   # Math::BigInt::Trace or plain Math::BigInt
-  $class->import(@import, upgrade => $upgrade, lib => $lib);
+  $class->import(@import, upgrade => $upgrade);
 
   if ($trace)
     {
@@ -167,6 +182,10 @@ sub import
     print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
     exit;
     }
+
+  # Take care of octal/hexadecimal constants
+  overload::constant 'binary' => sub { _binary_constant(shift) };
+
   $self->export_to_level(1,$self,@a);          # export inf and NaN
   }
 
@@ -217,13 +236,13 @@ You can see this with the following:
 
 Don't worry if it says Math::BigInt::Lite, bignum and friends will use Lite
 if it is installed since it is faster for some operations. It will be
-automatically upgraded to BigInt whenever neccessary:
+automatically upgraded to BigInt whenever necessary:
 
         perl -Mbignum -le 'print ref(2**255)'
 
 This also means it is a bad idea to check for some specific package, since
 the actual contents of $x might be something unexpected. Due to the
-transparent way of bignum C<ref()> should not be neccessary, anyway.
+transparent way of bignum C<ref()> should not be necessary, anyway.
 
 Since Math::BigInt and BigFloat also overload the normal math operations,
 the following line will still work:
@@ -306,6 +325,8 @@ than or equal to zero. See Math::BigInt's bround() function for details.
 
        perl -Mbignum=a,50 -le 'print sqrt(20)'
 
+Note that setting precision and accurary at the same time is not possible.
+
 =item p or precision
 
 This sets the precision for all math operations. The argument can be any
@@ -315,6 +336,8 @@ integer. See Math::BigInt's bfround() function for details.
 
        perl -Mbignum=p,-50 -le 'print sqrt(20)'
 
+Note that setting precision and accurary at the same time is not possible.
+
 =item t or trace
 
 This enables a trace mode and is primarily for debugging bignum or
@@ -327,7 +350,11 @@ Load a different math lib, see L<MATH LIBRARY>.
        perl -Mbignum=l,GMP -e 'print 2 ** 512'
 
 Currently there is no way to specify more than one library on the command
-line. This will be hopefully fixed soon ;)
+line. This means the following does not work:
+
+       perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'
+
+This will be hopefully fixed soon ;)
 
 =item v or version
 
@@ -335,6 +362,8 @@ This prints out the name and version of all modules used and then exits.
 
        perl -Mbignum=v
 
+=back
+
 =head2 Methods
 
 Beside import() and AUTOLOAD() there are only a few other methods.
@@ -363,7 +392,7 @@ following work:
         print $x + 1, " ", $y,"\n";     # prints 10 9
 
 but calling any method that modifies the number directly will result in
-B<both> the original and the copy beeing destroyed:
+B<both> the original and the copy being destroyed:
 
         $x = 9; $y = $x;
         print $x->badd(1), " ", $y,"\n";        # prints 10 10
@@ -386,12 +415,12 @@ well as the documentation in BigInt for further details.
 
 =item inf()
 
-A shortcut to return Math::BigInt->binf(). Usefull because Perl does not always
+A shortcut to return Math::BigInt->binf(). Useful because Perl does not always
 handle bareword C<inf> properly.
 
 =item NaN()
 
-A shortcut to return Math::BigInt->bnan(). Usefull because Perl does not always
+A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always
 handle bareword C<NaN> properly.
 
 =item upgrade()
@@ -465,6 +494,7 @@ Some cool command line examples to impress the Python crowd ;)
        perl -Mbignum -le 'print log(2)'
        perl -Mbignum -le 'print 2 ** 0.5'
        perl -Mbignum=a,65 -le 'print 2 ** 0.2'
+       perl -Mbignum=a,65,l,GMP -le 'print 7 ** 7777'
 
 =head1 LICENSE
 
@@ -480,6 +510,6 @@ as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and  L<Math::BigInt::GMP>.
 
 =head1 AUTHORS
 
-(C) by Tels L<http://bloodgate.com/> in early 2002, 2003.
+(C) by Tels L<http://bloodgate.com/> in early 2002 - 2007.
 
 =cut