From: Tels Date: Tue, 26 Jun 2007 20:56:45 +0000 (+0200) Subject: Re: RFC: bigint et. al exporting PI method? [PATCH] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fade31f0d958af49245e9d5d2d39ab5492d90d94;p=p5sagit%2Fp5-mst-13.2.git Re: RFC: bigint et. al exporting PI method? [PATCH] Message-Id: <200706262056.47311@bloodgate.com> p4raw-id: //depot/perl@31479 --- diff --git a/MANIFEST b/MANIFEST index a5e940e..cdc9a2f 100644 --- 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 diff --git a/lib/bigint.pm b/lib/bigint.pm index 941ee5c..b8a25a3 100644 --- a/lib/bigint.pm +++ b/lib/bigint.pm @@ -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 properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. +=item e() + +Returns Euler's number C, 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.pm b/lib/bignum.pm index 5ebb904..f8814f9 100644 --- a/lib/bignum.pm +++ b/lib/bignum.pm @@ -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 properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. +=item e() + +Returns Euler's number C, 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 index 0000000..b0de593 --- /dev/null +++ b/lib/bignum/t/big_e_pi.t @@ -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 index 0000000..1694640 --- /dev/null +++ b/lib/bignum/t/bii_e_pi.t @@ -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 index 0000000..0041e2c --- /dev/null +++ b/lib/bignum/t/bir_e_pi.t @@ -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'); diff --git a/lib/bigrat.pm b/lib/bigrat.pm index a4de1d6..e185d4f 100644 --- a/lib/bigrat.pm +++ b/lib/bigrat.pm @@ -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 properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. +=item e() + +Returns Euler's number C, 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