From: Tels Date: Wed, 27 Jun 2007 19:15:45 +0000 (+0200) Subject: Re: Broken bignum tests [PATCH] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d98d5fa08bf12cec80dc7627c70732eeb3c24baa;p=p5sagit%2Fp5-mst-13.2.git Re: Broken bignum tests [PATCH] Message-Id: <200706271915.46068@bloodgate.com> p4raw-id: //depot/perl@31485 --- diff --git a/lib/bigint.pm b/lib/bigint.pm index 6eaa77e..9ff01de 100644 --- a/lib/bigint.pm +++ b/lib/bigint.pm @@ -4,8 +4,8 @@ use 5.006002; $VERSION = '0.22'; use Exporter; @ISA = qw( Exporter ); -@EXPORT_OK = qw( PI e ); -@EXPORT = qw( inf NaN ); +@EXPORT_OK = qw( PI e bpi bexp ); +@EXPORT = qw( inf NaN ); use strict; use overload; @@ -215,7 +215,7 @@ sub import splice @a, $j, 1; $j --; $oct = \&_oct_global; } - elsif ($_[$i] !~ /^(PI|e)\z/) + elsif ($_[$i] !~ /^(PI|e|bpi|bexp)\z/) { die ("unknown option $_[$i]"); } @@ -280,8 +280,11 @@ sub import sub inf () { Math::BigInt::binf(); } sub NaN () { Math::BigInt::bnan(); } -sub PI { Math::BigInt->new(3); } -sub e { Math::BigInt->new(2); } + +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; @@ -494,13 +497,43 @@ handle bareword C properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. -=item e() +=item e + + # perl -Mbigint=e -wle 'print e' + +Returns Euler's number C, 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 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 Euler's number C, aka exp(1), to the given number of digits. +Returns PI to the wanted accuracy. Note that under bigint, this is truncated +to an integer, and hence simple '3'. -=item PI() +Example: -Returns PI to the given number of digits. + # perl -Mbigint=bpi -wle 'print bpi(80)' =item upgrade() diff --git a/lib/bignum.pm b/lib/bignum.pm index f8814f9..e731b26 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( PI e ); +@EXPORT_OK = qw( PI e bexp bpi ); @EXPORT = qw( inf NaN ); use strict; @@ -166,7 +166,7 @@ sub import splice @a, $j, 1; $j --; $oct = \&bigint::_oct_global; } - elsif ($_[$i] !~ /^(PI|e)\z/) + elsif ($_[$i] !~ /^(PI|e|bexp|bpi)\z/) { die ("unknown option $_[$i]"); } @@ -240,8 +240,10 @@ sub import } } -sub PI { Math::BigFloat::bpi(@_); } -sub e { Math::BigFloat->bone->bexp(@_); } +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; @@ -494,13 +496,38 @@ handle bareword C properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. -=item e() +=item e -Returns Euler's number C, aka exp(1), to the given number of digits. + # perl -Mbignum=e -wle 'print e' + +Returns Euler's number C, aka exp(1). =item PI() -Returns PI to the given number of digits. + # perl -Mbignum=PI -wle 'print PI' + +Returns PI. + +=item bexp() + + bexp($power,$accuracy); + +Returns Euler's number C 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() diff --git a/lib/bignum/t/big_e_pi.t b/lib/bignum/t/big_e_pi.t index b0de593..819e225 100644 --- a/lib/bignum/t/big_e_pi.t +++ b/lib/bignum/t/big_e_pi.t @@ -14,10 +14,10 @@ BEGIN plan tests => 4; } -use bignum qw/e PI/; +use bignum qw/e PI bexp bpi/; is (e, "2.718281828459045235360287471352662497757", 'e'); is (PI, "3.141592653589793238462643383279502884197", 'PI'); -is (e(10), "2.718281828", 'e'); -is (PI(10), "3.141592654", 'PI'); +is (bexp(1,10), "2.718281828", 'e'); +is (bpi(10), "3.141592654", 'PI'); diff --git a/lib/bignum/t/bii_e_pi.t b/lib/bignum/t/bii_e_pi.t index 1694640..76ee07a 100644 --- a/lib/bignum/t/bii_e_pi.t +++ b/lib/bignum/t/bii_e_pi.t @@ -11,13 +11,14 @@ BEGIN $| = 1; chdir 't' if -d 't'; unshift @INC, '../lib'; - plan tests => 4; + plan tests => 5; } -use bigint qw/e PI/; +use bigint qw/e PI bpi bexp/; is (e, "2", 'e'); is (PI, "3", 'PI'); -is (e(10), "2", 'e'); -is (PI(10), "3", 'PI'); +is (bexp(1,10), "2", 'e'); +is (bexp(3,10), "20", 'e'); +is (bpi(10), "3", 'PI'); diff --git a/lib/bignum/t/bir_e_pi.t b/lib/bignum/t/bir_e_pi.t index 0041e2c..88342b0 100644 --- a/lib/bignum/t/bir_e_pi.t +++ b/lib/bignum/t/bir_e_pi.t @@ -14,10 +14,12 @@ BEGIN plan tests => 4; } -use bigrat qw/e PI/; +use bigrat qw/e PI bexp bpi/; is (e, "2.718281828459045235360287471352662497757", 'e'); is (PI, "3.141592653589793238462643383279502884197", 'PI'); -is (e(10), "2.718281828", 'e'); -is (PI(10), "3.141592654", 'PI'); +# these tests should actually produce big rationals, but this is not yet +# implemented: +is (bexp(1,10), "2.718281828", 'e'); +is (bpi(10), "3.141592654", 'PI'); diff --git a/lib/bigrat.pm b/lib/bigrat.pm index e185d4f..70dddd9 100644 --- a/lib/bigrat.pm +++ b/lib/bigrat.pm @@ -4,8 +4,8 @@ use 5.006002; $VERSION = '0.22'; require Exporter; @ISA = qw( bigint ); -@EXPORT_OK = qw( PI e ); -@EXPORT = qw( inf NaN ); +@EXPORT_OK = qw( PI e bpi bexp ); +@EXPORT = qw( inf NaN ); use strict; use overload; @@ -158,7 +158,7 @@ sub import splice @a, $j, 1; $j --; $oct = \&bigint::_oct_global; } - elsif ($_[$i] !~ /^(PI|e)\z/) + elsif ($_[$i] !~ /^(PI|e|bpi|bexp)\z/) { die ("unknown option $_[$i]"); } @@ -226,8 +226,16 @@ sub import } } -sub PI { local $Math::BigFloat::upgrade = undef; Math::BigFloat::bpi(@_); } -sub e { local $Math::BigFloat::upgrade = undef; Math::BigFloat->bone()->bexp(@_); } +sub PI () { Math::BigFloat->new('3.141592653589793238462643383279502884197'); } +sub e () { Math::BigFloat->new('2.718281828459045235360287471352662497757'); } + +sub bpi ($) { local $Math::BigFloat::upgrade; Math::BigFloat::bpi(@_); } + +sub bexp ($$) + { + local $Math::BigFloat::upgrade; + my $x = Math::BigFloat->new($_[0]); $x->bexp($_[1]); + } 1; @@ -332,13 +340,39 @@ handle bareword C properly. A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always handle bareword C properly. -=item e() +=item e + + # perl -Mbigrat=e -wle 'print e' + +Returns Euler's number C, aka exp(1). + +=item PI + + # perl -Mbigrat=PI -wle 'print PI' + +Returns PI. + +=item bexp() + + bexp($power,$accuracy); + + +Returns Euler's number C raised to the appropriate power, to +the wanted accuracy. + +Example: + + # perl -Mbigrat=bexp -wle 'print bexp(1,80)' + +=item bpi() + + bpi($accuracy); -Returns Euler's number C, aka exp(1), to the given number of digits. +Returns PI to the wanted accuracy. -=item PI() +Example: -Returns PI to the given number of digits. + # perl -Mbigrat=bpi -wle 'print bpi(80)' =item upgrade()