shao.lo has asked for the wisdom of the Perl Monks concerning the following question:

use Math::BigInt; my $n = 2; my $k = 0; my $x = Math::BigInt->new($n); my $z = $x->bnok($k); printf("$n choose $k = %d\n", $z); printf("$n choose $k = %d\n", $z->as_int()); printf("$n choose $k = %s\n", $z->bstr()); printf("$n choose $k = %d\n", $z->numify());
I'm guessing I'm doing something wrong because this just prints 2 choose 0 = 3. I'm pretty sure it should be 1.
my $n = 5; my $k = 2; my $x = Math::BigInt->new($n); my $z = $x->bnok($k); printf("$n choose $k = %d\n", $z);
The above correctly prints 5 choose 2 = 10. So, what am I doing wrong? Or is bnok() broken?? BTW..I tried BigFloat too...with same result.
***
THANKS to everyone that responded. I was indeed using an older verion (1.89_01). I use Ubuntu and I guess Math::BigInt comes as part of some other package. When I installed it directly via 'apt-get install libmath-bigint-perl' I now get version (1.993) which does indeed get the 2 choose 0 correct.
***
Thanks again for the responses!

Replies are listed 'Best First'.
Re: BigInt bug?
by davido (Cardinal) on Mar 07, 2012 at 07:05 UTC

    If you're using a version of Math::BigInt older than version 1.90, you may want to upgrade to the latest. I found this entry in the CHANGES log for the Math::BigInt distribution:

    2010-09-03 v1.90 rafl * fix bnok() for k==0 and k==n-1

    It looks like the bug-fix in version 1.90 targeted the bug you're experiencing.


    Dave

Re: BigInt bug?
by tobyink (Canon) on Mar 07, 2012 at 06:46 UTC

    The output of your first script is correct on this machine:

    2 choose 0 = 1
    2 choose 0 = 1
    2 choose 0 = 1
    2 choose 0 = 1
    

    Perl 5.10.1, Math::BigInt 1.95.

      using 5.10.1, Math::BigInt 1.89 I get
      2 choose 0 = 3 2 choose 0 = 3 2 choose 0 = 3 2 choose 0 = 3

      I then updated to Math::BigInt 1.997 and I get

      2 choose 0 = 1 2 choose 0 = 1 2 choose 0 = 1 2 choose 0 = 1

      Looks like you found a bug that's been fixed. (and helped me update...)

        Using 5.14.2, Math::BigInt 1.994 I get

        2 choose 0 = 1 2 choose 0 = 1 2 choose 0 = 1 2 choose 0 = 1