Fixes for ext/compress
[p5sagit/p5-mst-13.2.git] / lib / bigint.pm
index c64116a..e143af6 100644 (file)
@@ -1,11 +1,11 @@
 package bigint;
-use 5.006002;
+use 5.006;
 
-$VERSION = '0.22';
+$VERSION = '0.23';
 use Exporter;
 @ISA           = qw( Exporter );
-@EXPORT_OK     = qw( ); 
-@EXPORT                = qw( inf NaN ); 
+@EXPORT_OK     = qw( PI e bpi bexp );
+@EXPORT                = qw( inf NaN );
 
 use strict;
 use overload;
@@ -159,12 +159,12 @@ sub import
 
   $^H{bigint} = 1;                                     # we are in effect
 
+  my ($hex,$oct);
   # for newer Perls always override hex() and oct() with a lexical version:
   if ($] > 5.009004)
     {
-    no warnings 'redefine';
-    *CORE::GLOBAL::oct = \&_oct;
-    *CORE::GLOBAL::hex = \&_hex;
+    $oct = \&_oct;
+    $hex = \&_hex;
     }
   # some defaults
   my $lib = ''; my $lib_kind = 'try';
@@ -208,16 +208,17 @@ sub import
     elsif ($_[$i] eq 'hex')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      *CORE::GLOBAL::hex = \&_hex_global;
+      $hex = \&_hex_global;
       }
     elsif ($_[$i] eq 'oct')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      *CORE::GLOBAL::oct = \&_oct_global;
+      $oct = \&_oct_global;
+      }
+    elsif ($_[$i] !~ /^(PI|e|bpi|bexp)\z/)
+      {
+      die ("unknown option $_[$i]");
       }
-    else { die "unknown option $_[$i]"; }
     }
   my $class;
   $_lite = 0;                                  # using M::BI::L ?
@@ -268,12 +269,22 @@ sub import
   no strict 'refs';
   if (!defined *{"${package}::inf"})
     {
-    $self->export_to_level(1,$self,@a);           # export inf and NaN
+    $self->export_to_level(1,$self,@a);           # export inf and NaN, e and PI
     }
+  {
+    no warnings 'redefine';
+    *CORE::GLOBAL::oct = $oct if $oct;
+    *CORE::GLOBAL::hex = $hex if $hex;
+  }
   }
 
-sub inf () { Math::BigInt->binf(); }
-sub NaN () { Math::BigInt->bnan(); }
+sub inf () { Math::BigInt::binf(); }
+sub NaN () { Math::BigInt::bnan(); }
+
+sub PI () { Math::BigInt->new(3); }
+sub e () { Math::BigInt->new(2); }
+sub bpi ($) { Math::BigInt->new(3); }
+sub bexp ($$) { my $x = Math::BigInt->new($_[0]); $x->bexp($_[1]); }
 
 1;
 
@@ -385,13 +396,13 @@ Math::BigInt.
 
 =item hex
 
-Override the build-in hex() method with a version that can handle big
+Override the built-in hex() method with a version that can handle big
 integers. Note that under Perl v5.9.4 or ealier, this will be global
 and cannot be disabled with "no bigint;".
 
 =item oct
 
-Override the build-in oct() method with a version that can handle big
+Override the built-in oct() method with a version that can handle big
 integers. Note that under Perl v5.9.4 or ealier, this will be global
 and cannot be disabled with "no bigint;".
 
@@ -486,6 +497,44 @@ handle bareword C<inf> properly.
 A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always
 handle bareword C<NaN> properly.
 
+=item e
+
+       # perl -Mbigint=e -wle 'print e'
+
+Returns Euler's number C<e>, aka exp(1). Note that under bigint, this is
+truncated to an integer, and hence simple '2'.
+
+=item PI
+
+       # perl -Mbigint=PI -wle 'print PI'
+
+Returns PI. Note that under bigint, this is truncated to an integer, and hence
+simple '3'.
+
+=item bexp()
+
+       bexp($power,$accuracy);
+
+Returns Euler's number C<e> raised to the appropriate power, to
+the wanted accuracy.
+
+Note that under bigint, the result is truncated to an integer.
+
+Example:
+
+       # perl -Mbigint=bexp -wle 'print bexp(1,80)'
+
+=item bpi()
+
+       bpi($accuracy);
+
+Returns PI to the wanted accuracy. Note that under bigint, this is truncated
+to an integer, and hence simple '3'.
+
+Example:
+
+       # perl -Mbigint=bpi -wle 'print bpi(80)'
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning