-
Notifications
You must be signed in to change notification settings - Fork 558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PATCH] Math::BigInt->new($n)->bnok($k) incorrect for $k==0 and $k===$n-1 #10601
Comments
From pfusik@op.plThis is a bug report for perl from pfusik@op.pl, There is a bug in Math::BigInt's implementation of n over k: Attached is a patch. Flags: Site configuration information for perl 5.12.1: Configured by 1 at Thu Jul 29 16:39:48 2010. Summary of my perl5 (revision 5 version 12 subversion 1) configuration: Locally applied patches: @INC for perl 5.12.1: Environment for perl 5.12.1: |
From pfusik@op.plMath-BigInt-bnok.patchdiff --git a/cpan/Math-BigInt/lib/Math/BigInt.pm b/cpan/Math-BigInt/lib/Math/BigInt.pm
index f97e438..303f4a0 100644
--- a/cpan/Math-BigInt/lib/Math/BigInt.pm
+++ b/cpan/Math-BigInt/lib/Math/BigInt.pm
@@ -1320,18 +1320,17 @@ sub bnok
}
else
{
- # ( 7 ) 7! 7*6*5 * 4*3*2*1 7 * 6 * 5
- # ( - ) = --------- = --------------- = ---------
- # ( 3 ) 3! (7-3)! 3*2*1 * 4*3*2*1 3 * 2 * 1
+ # ( 7 ) 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7
+ # ( - ) = --------- = --------------- = --------- = 5 * - * -
+ # ( 3 ) (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3
- # compute n - k + 2 (so we start with 5 in the example above)
- my $z = $x - $y;
- if (!$z->is_one())
+ if (!$y->is_zero())
{
+ my $z = $x - $y;
$z->binc();
my $r = $z->copy(); $z->binc();
my $d = $self->new(2);
- while ($z->bacmp($x) <= 0) # f < x ?
+ while ($z->bacmp($x) <= 0) # f <= x ?
{
$r->bmul($z); $r->bdiv($d);
$z->binc(); $d->binc();
diff --git a/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm b/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm
index 52e33d2..6527b38 100644
--- a/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm
+++ b/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm
@@ -1264,7 +1264,7 @@ sub _is_even
sub _is_odd
{
- # return true if arg is even
+ # return true if arg is odd
(($_[1]->[0] & 1)) <=> 0;
}
@@ -1536,22 +1536,20 @@ sub _nok
# ref to array, return ref to array
my ($c,$n,$k) = @_;
- # ( 7 ) 7! 7*6*5 * 4*3*2*1 7 * 6 * 5
- # ( - ) = --------- = --------------- = ---------
- # ( 3 ) 3! (7-3)! 3*2*1 * 4*3*2*1 3 * 2 * 1
-
- # compute n - k + 2 (so we start with 5 in the example above)
- my $x = _copy($c,$n);
+ # ( 7 ) 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7
+ # ( - ) = --------- = --------------- = --------- = 5 * - * -
+ # ( 3 ) (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3
- _sub($c,$n,$k);
- if (!_is_one($c,$n))
+ if (!_is_zero($c,$k))
{
+ my $x = _copy($c,$n);
+ _sub($c,$n,$k);
_inc($c,$n);
my $f = _copy($c,$n); _inc($c,$f); # n = 5, f = 6, d = 2
my $d = _two($c);
- while (_acmp($c,$f,$x) <= 0) # f < n ?
+ while (_acmp($c,$f,$x) <= 0) # f <= n ?
{
- # n = (n * f / d) == 5 * 6 / 2 => n == 3
+ # n = (n * f / d) == 5 * 6 / 2
$n = _mul($c,$n,$f); $n = _div($c,$n,$d);
# f = 7, d = 3
_inc($c,$f); _inc($c,$d);
diff --git a/cpan/Math-BigInt/t/bigintpm.inc b/cpan/Math-BigInt/t/bigintpm.inc
index 87140ba..317e5ed 100644
--- a/cpan/Math-BigInt/t/bigintpm.inc
+++ b/cpan/Math-BigInt/t/bigintpm.inc
@@ -2346,9 +2346,12 @@ NaN:1:NaN
1:-2:0
# 7 over 3 = 35
7:3:35
-7:6:1
+7:6:7
100:90:17310309456440
100:95:75287520
+2:0:1
+7:0:1
+2:1:2
&bround
$round_mode('trunc')
0:12:0
|
From [Unknown Contact. See original ticket]Applied, after some slight fixups, as |
@rafl - Status changed from 'new' to 'resolved' |
Migrated from rt.perl.org#77640 (status was 'resolved')
Searchable as RT77640$
The text was updated successfully, but these errors were encountered: