This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
AW: IO::Dir destructor
[perl5.git] / lib / bignum.pm
index 4323356..2e075c8 100644 (file)
@@ -1,10 +1,10 @@
 package bignum;
-use 5.006002;
+use 5.006;
 
-$VERSION = '0.22';
+$VERSION = '0.23';
 use Exporter;
 @ISA           = qw( bigint );
-@EXPORT_OK     = qw( ); 
+@EXPORT_OK     = qw( PI e bexp bpi ); 
 @EXPORT        = qw( inf NaN ); 
 
 use strict;
@@ -92,12 +92,13 @@ sub import
 
   $^H{bignum} = 1;                                     # we are in effect
 
+  my ($hex,$oct);
+
   # for newer Perls override hex() and oct() with a lexical version:
   if ($] > 5.009003)
     {
-    no warnings 'redefine';
-    *CORE::GLOBAL::oct = \&_oct;
-    *CORE::GLOBAL::hex = \&_hex;
+    $hex = \&_hex;
+    $oct = \&_oct;
     }
 
   # some defaults
@@ -158,18 +159,17 @@ sub import
     elsif ($_[$i] eq 'hex')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      # override with a global version
-      *CORE::GLOBAL::hex = \&bigint::_hex_global;
+      $hex = \&bigint::_hex_global;
       }
     elsif ($_[$i] eq 'oct')
       {
       splice @a, $j, 1; $j --;
-      no warnings 'redefine';
-      # override with a global version
-      *CORE::GLOBAL::oct = \&bigint::_oct_global;
+      $oct = \&bigint::_oct_global;
+      }
+    elsif ($_[$i] !~ /^(PI|e|bexp|bpi)\z/)
+      {
+      die ("unknown option $_[$i]");
       }
-    else { die "unknown option $_[$i]"; }
     }
   my $class;
   $_lite = 0;                                  # using M::BI::L ?
@@ -233,7 +233,17 @@ sub import
     {
     $self->export_to_level(1,$self,@a);           # export inf and NaN
     }
+  {
+    no warnings 'redefine';
+    *CORE::GLOBAL::oct = $oct if $oct;
+    *CORE::GLOBAL::hex = $hex if $hex;
   }
+  }
+
+sub PI () { Math::BigFloat->new('3.141592653589793238462643383279502884197'); }
+sub e () { Math::BigFloat->new('2.718281828459045235360287471352662497757'); }
+sub bpi ($) { Math::BigFloat::bpi(@_); }
+sub bexp ($$) { my $x = Math::BigFloat->new($_[0]); $x->bexp($_[1]); }
 
 1;
 
@@ -409,13 +419,13 @@ This will be hopefully fixed soon ;)
 
 =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 older than v5.9.4, 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 older than v5.9.4, this will be global
 and cannot be disabled with "no bigint;".
 
@@ -486,6 +496,39 @@ 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 -Mbignum=e -wle 'print e'
+
+Returns Euler's number C<e>, aka exp(1).
+
+=item PI()
+
+       # perl -Mbignum=PI -wle 'print PI'
+
+Returns PI.
+
+=item bexp()
+
+       bexp($power,$accuracy);
+
+Returns Euler's number C<e> raised to the appropriate power, to
+the wanted accuracy.
+
+Example:
+
+       # perl -Mbignum=bexp -wle 'print bexp(1,80)'
+
+=item bpi()
+
+       bpi($accuracy);
+
+Returns PI to the wanted accuracy.
+
+Example:
+
+       # perl -Mbignum=bpi -wle 'print bpi(80)'
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning