summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTels <nospam-abuse@bloodgate.com>2007-06-26 22:56:45 +0200
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2007-06-27 12:57:52 +0000
commitfade31f0d958af49245e9d5d2d39ab5492d90d94 (patch)
tree95738216b73e942e33eb7e0dd719212b6bdb78c2
parent30afc38d44f16307c535215ac91b9b6259f70b14 (diff)
downloadperl-fade31f0d958af49245e9d5d2d39ab5492d90d94.tar.gz
Re: RFC: bigint et. al exporting PI method? [PATCH]
Message-Id: <200706262056.47311@bloodgate.com> p4raw-id: //depot/perl@31479
-rw-r--r--MANIFEST3
-rw-r--r--lib/bigint.pm23
-rw-r--r--lib/bignum.pm18
-rw-r--r--lib/bignum/t/big_e_pi.t23
-rw-r--r--lib/bignum/t/bii_e_pi.t23
-rw-r--r--lib/bignum/t/bir_e_pi.t23
-rw-r--r--lib/bigrat.pm15
7 files changed, 119 insertions, 9 deletions
diff --git a/MANIFEST b/MANIFEST
index a5e940e8a0..cdc9a2fd81 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 941ee5cf3b..b8a25a3475 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<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.pm b/lib/bignum.pm
index 5ebb904480..f8814f90db 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<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
index 0000000000..b0de593c54
--- /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 0000000000..169464011e
--- /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 0000000000..0041e2cbd8
--- /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 a4de1d68f5..e185d4f0bc 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<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