This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: RFC: bigint et. al exporting PI method? [PATCH]
authorTels <nospam-abuse@bloodgate.com>
Tue, 26 Jun 2007 20:56:45 +0000 (22:56 +0200)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Wed, 27 Jun 2007 12:57:52 +0000 (12:57 +0000)
Message-Id: <200706262056.47311@bloodgate.com>

p4raw-id: //depot/perl@31479

MANIFEST
lib/bigint.pm
lib/bignum.pm
lib/bignum/t/big_e_pi.t [new file with mode: 0644]
lib/bignum/t/bii_e_pi.t [new file with mode: 0644]
lib/bignum/t/bir_e_pi.t [new file with mode: 0644]
lib/bigrat.pm

index a5e940e..cdc9a2f 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1467,6 +1467,9 @@ lib/bignum/t/bigexp.t             See if bignum works
 lib/bignum/t/bigint.t          See if bigint works
 lib/bignum/t/bignum.t          See if bignum works
 lib/bignum/t/bigrat.t          See if bigrat works
+lib/bignum/t/bii_e_pi.t                See if bigint exports e() and PI()
+lib/bignum/t/bir_e_pi.t                See if bigrat exports e() and PI()
+lib/bignum/t/big_e_pi.t                See if bignum exports e() and PI()
 lib/bignum/t/biinfnan.t                See if bignum works
 lib/bignum/t/bninfnan.t                See if bignum works
 lib/bignum/t/bn_lite.t         See if bignum with Math::BigInt::Lite works
index 941ee5c..b8a25a3 100644 (file)
@@ -4,7 +4,7 @@ use 5.006002;
 $VERSION = '0.22';
 use Exporter;
 @ISA           = qw( Exporter );
-@EXPORT_OK     = qw( ); 
+@EXPORT_OK     = qw( PI e ); 
 @EXPORT                = qw( inf NaN ); 
 
 use strict;
@@ -215,7 +215,10 @@ sub import
       splice @a, $j, 1; $j --;
       $oct = \&_oct_global;
       }
-    else { die "unknown option $_[$i]"; }
+    elsif ($_[$i] !~ /^(PI|e)\z/)
+      {
+      die ("unknown option $_[$i]");
+      }
     }
   my $class;
   $_lite = 0;                                  # using M::BI::L ?
@@ -266,7 +269,7 @@ 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';
@@ -275,8 +278,10 @@ sub import
   }
   }
 
-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); }
 
 1;
 
@@ -489,6 +494,14 @@ 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()
+
+Returns Euler's number C<e>, aka exp(1), to the given number of digits.
+
+=item PI()
+
+Returns PI to the given number of digits.
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning
index 5ebb904..f8814f9 100644 (file)
@@ -4,7 +4,7 @@ use 5.006002;
 $VERSION = '0.22';
 use Exporter;
 @ISA           = qw( bigint );
-@EXPORT_OK     = qw( ); 
+@EXPORT_OK     = qw( PI e ); 
 @EXPORT        = qw( inf NaN ); 
 
 use strict;
@@ -166,7 +166,10 @@ sub import
       splice @a, $j, 1; $j --;
       $oct = \&bigint::_oct_global;
       }
-    else { die "unknown option $_[$i]"; }
+    elsif ($_[$i] !~ /^(PI|e)\z/)
+      {
+      die ("unknown option $_[$i]");
+      }
     }
   my $class;
   $_lite = 0;                                  # using M::BI::L ?
@@ -237,6 +240,9 @@ sub import
   }
   }
 
+sub PI { Math::BigFloat::bpi(@_); }
+sub e  { Math::BigFloat->bone->bexp(@_); }
+
 1;
 
 __END__
@@ -488,6 +494,14 @@ 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()
+
+Returns Euler's number C<e>, aka exp(1), to the given number of digits.
+
+=item PI()
+
+Returns PI to the given number of digits.
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning
diff --git a/lib/bignum/t/big_e_pi.t b/lib/bignum/t/big_e_pi.t
new file mode 100644 (file)
index 0000000..b0de593
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+###############################################################################
+# test for e() and PI() exports
+
+use Test::More;
+use strict;
+
+BEGIN
+  {
+  $| = 1;
+  chdir 't' if -d 't';
+  unshift @INC, '../lib';
+  plan tests => 4;
+  }
+
+use bignum qw/e PI/;
+
+is (e, "2.718281828459045235360287471352662497757", 'e');
+is (PI, "3.141592653589793238462643383279502884197", 'PI');
+
+is (e(10), "2.718281828", 'e');
+is (PI(10), "3.141592654", 'PI');
diff --git a/lib/bignum/t/bii_e_pi.t b/lib/bignum/t/bii_e_pi.t
new file mode 100644 (file)
index 0000000..1694640
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+###############################################################################
+# test for e() and PI() exports
+
+use Test::More;
+use strict;
+
+BEGIN
+  {
+  $| = 1;
+  chdir 't' if -d 't';
+  unshift @INC, '../lib';
+  plan tests => 4;
+  }
+
+use bigint qw/e PI/;
+
+is (e, "2", 'e');
+is (PI, "3", 'PI');
+
+is (e(10), "2", 'e');
+is (PI(10), "3", 'PI');
diff --git a/lib/bignum/t/bir_e_pi.t b/lib/bignum/t/bir_e_pi.t
new file mode 100644 (file)
index 0000000..0041e2c
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/perl -w
+
+###############################################################################
+# test for e() and PI() exports
+
+use Test::More;
+use strict;
+
+BEGIN
+  {
+  $| = 1;
+  chdir 't' if -d 't';
+  unshift @INC, '../lib';
+  plan tests => 4;
+  }
+
+use bigrat qw/e PI/;
+
+is (e, "2.718281828459045235360287471352662497757", 'e');
+is (PI, "3.141592653589793238462643383279502884197", 'PI');
+
+is (e(10), "2.718281828", 'e');
+is (PI(10), "3.141592654", 'PI');
index a4de1d6..e185d4f 100644 (file)
@@ -4,7 +4,7 @@ use 5.006002;
 $VERSION = '0.22';
 require Exporter;
 @ISA           = qw( bigint );
-@EXPORT_OK     = qw( ); 
+@EXPORT_OK     = qw( PI e ); 
 @EXPORT                = qw( inf NaN ); 
 
 use strict;
@@ -158,7 +158,7 @@ sub import
       splice @a, $j, 1; $j --;
       $oct = \&bigint::_oct_global;
       }
-    else
+    elsif ($_[$i] !~ /^(PI|e)\z/)
       {
       die ("unknown option $_[$i]");
       }
@@ -226,6 +226,9 @@ sub import
   }
   }
 
+sub PI { local $Math::BigFloat::upgrade = undef; Math::BigFloat::bpi(@_); }
+sub e  { local $Math::BigFloat::upgrade = undef; Math::BigFloat->bone()->bexp(@_); }
+
 1;
 
 __END__
@@ -329,6 +332,14 @@ 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()
+
+Returns Euler's number C<e>, aka exp(1), to the given number of digits.
+
+=item PI()
+
+Returns PI to the given number of digits.
+
 =item upgrade()
 
 Return the class that numbers are upgraded to, is in fact returning