CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2005
    Posts
    6

    Are there bigger integers than "long long"?

    Hi folks,

    I scribbled a little c++ program whilst in the bar ;-) last night. It was simply to calculate Fibonacci numbers (you know, the sum of the previous 2)

    Basically I calculated up to about the 20th (using a simple int declaration) before the output started "acting up" basically the numbers got too big for a simple int.

    I changed the declaration to "long long int" and this got me to the 92nd Fibonacci number before becoming again too large:

    89. 2880067194370816120
    90. 4660046610375530309
    91. 7540113804746346429
    92. -6246583658587674878 <----this one added a "-" sign.

    Here's the code:

    //CODE

    #include <iostream>

    int main()
    {
    long long int i1 = 0;
    long long int i2 = 1;
    long long int fib;

    {
    for (int x = 1; x <= 92; ++x){
    fib = i1 + i2;
    std::cout << x;
    std::cout << ".";
    std::cout << " ";
    std::cout << fib << endl;
    i1 = i2;
    i2 = fib;
    }
    }
    return 0;
    }

    //CODE

    Can I go any higher in calculating these numbers?

    Thanks.

    Sundodger.

  2. #2
    Join Date
    Feb 2005
    Posts
    6

    Re: Are there bigger integers than "long long"?

    Yup!!

    got the syntax wrong for posting code!!

    Should have been
    Code:
      ....
    . (thanks to dude1967)

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Are there bigger integers than "long long"?

    There also is a button below your post, that says "Edit". You can correct your own posts.

    As for the integer size, the C++ Standard doesn't say how large the intgral types have to be. Many compilers support __int64 (they may name it something else). For larger ints you'll need to implement them on your own.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Are there bigger integers than "long long"?

    A good free library for big numbers is GMP.
    See http://www.swox.com/gmp/

  5. #5
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: Are there bigger integers than "long long"?

    Aren't __int64 and long long identical in all but naming conventions?
    I would use GMP if I could work out how to compile it on MSVC.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  6. #6
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Are there bigger integers than "long long"?

    Quote Originally Posted by Improving
    Aren't __int64 and long long identical in all but naming conventions?
    No, they are not. A long long can have 64 bits, but it isn't required to. The _int64 is a type that always has 64 bits. I'm unsure whether there are standard types with fixed bit sizes (I think there aren't) -- I'm not in the mood right now to look it up
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Are there bigger integers than "long long"?

    Quote Originally Posted by Gabriel
    The _int64 is a type that always has 64 bits. I'm unsure whether there are standard types with fixed bit sizes (I think there aren't)
    C++ standard only says that:

    1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

    And I think it also says that bool must be represented on 1 byte.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Are there bigger integers than "long long"?

    long long and __int64 aren't standard types, only some compilers support them. On the compilers that do support them, __int64 is always 64 bits in size, and long long is typically double the size of long (64-bits if long is 32-bits). I don't know of any compiler supporting built-in integer types larger than 64 bits. If you need to work with larger integers, take a look at some third-party libraries like the GMP library mentioned by olivthill, the apfloat library, or the CBigInt library
    Old Unix programmers never die, they just mv to /dev/null

  9. #9
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: Are there bigger integers than "long long"?

    Talking of GMP. Anyone know how to compile this on MSVC or even compile it into a library usable by MSVC with another compiler?
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  10. #10
    Join Date
    May 2004
    Posts
    340

    Re: Are there bigger integers than "long long"?

    my suggestion is that should we consider putting it in a singly link list.with the structure containing data field which store parts of the BIgInt,for example,a big integer,123 345 567 890 678 555 456.we contain store 123 in node1,345 in node2,567 in node3,890 in node4,678 in node5.ETC.,and pointer field which maintains the connection of the list.
    struct BigIntNode
    {
    int data;
    BigInt*link;
    }

    regards,
    jolley

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured