bigint vs. bignum

Discussion in 'Perl Misc' started by Alex, Feb 26, 2009.

  1. Alex

    Alex Guest

    Hello, I'm new to Perl

    I have a small program, which uses only (big) integers, but works
    differently with "use bigint" and "use bignum". Can you please explain
    why?

    sub Next($)
    {
    my $var = shift;
    my $tst = $var % 2;
    return ($tst == 0) ? $var / 2 : $var * 3 + 1;
    }

    my $n = 2 ** 1000 - 1;
    my $counter = 0;

    while (1)
    {
    $n = Next($n);
    if ($n == 1)
    {
    print("counter = ", $counter, "\n");
    last;
    }
    $counter++;
    }

    Thank you in advance
    Alex
    Alex, Feb 26, 2009
    #1
    1. Advertising

  2. Alex

    Guest

    Alex <> wrote:
    > Hello, I'm new to Perl
    >
    > I have a small program, which uses only (big) integers, but works
    > differently with "use bigint" and "use bignum". Can you please explain
    > why?
    >
    > sub Next($)
    > {
    > my $var = shift;
    > my $tst = $var % 2;
    > return ($tst == 0) ? $var / 2 : $var * 3 + 1;


    use bignum turns on upgrading.

    When the division by 2 happens, the BigInt upgrades to BigFloat. It
    doesn't look to see if the division results in an integer before
    upgrading--it just does it.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Feb 26, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Bazell

    BigInt leading plus sign

    Dave Bazell, Dec 13, 2005, in forum: Perl
    Replies:
    2
    Views:
    2,589
    Dave Bazell
    Dec 14, 2005
  2. KK

    bigInt operator* help

    KK, Oct 10, 2003, in forum: C++
    Replies:
    3
    Views:
    834
    rossum
    Oct 12, 2003
  3. MK
    Replies:
    10
    Views:
    2,847
    DHOLLINGSWORTH2
    Feb 27, 2005
  4. MK
    Replies:
    1
    Views:
    1,605
  5. =?ISO-8859-2?Q?Rafa=B3?= Maj Raf256

    Something like bigint / GMP?

    =?ISO-8859-2?Q?Rafa=B3?= Maj Raf256, Mar 17, 2005, in forum: C++
    Replies:
    5
    Views:
    801
    Alex Vinokur
    Mar 19, 2005
Loading...

Share This Page