Very large intergers in C

This is a discussion on Very large intergers in C within the C Programming forums, part of the General Programming Boards category; I have written a basic C program to return factorials. I am having trouble with intergers greater than 2 billion. ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    Very large intergers in C

    I have written a basic C program to return factorials. I am having trouble with intergers greater than 2 billion.

    fact(18) and above return nonsense
    values.

    How do you use very large intergers in C?
    I have tried unsigned long int, but it
    did not help.

    Thanks,
    Rookie

  2. #2
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Code:
    unsigned long long
    I think...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The built-in types can only hold so much. If you want integers that exceed those types then you need to either use a nonstandard compiler extended type, such as int64, or an arbitrary length integer library that allows you to use numbers of any size (unless you run out of some critical resource first).

    There was a contest concerning this topic a while ago, you can search back. I believe some of the entries were posted. The code may be helpful in you writing your own library, or you can ask the author for permission to use their code in your program.
    My best code is written with the delete key.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    GMP
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Note that the largest unsigned integer that can be represented by a 32-bit number is about 4.29 E9 (4 times 10 to the 9th, in English.) For a 64-bit number, it's about 1.8 E19. Note, further, that 21 factorial is about 5.1 E19.

    It helps to consider mathematical possibilities before trying to program something unattainable. If you want to calculate something larger than 20 factorial, you can't do it with built-in arithmetic limited to 32-bit or 64-bit numbers.

    Dave

  6. #6
    Registered User scrapedbr's Avatar
    Join Date
    May 2003
    Posts
    19
    GNU mutli-precision library (libgmp)
    cscience.org
    gobolinux.org
    Gobolinux user: 00101100

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 03:18 AM
  2. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  3. Representing a Large Integer with an Array
    By random_accident in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 07:56 PM
  4. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 11:23 AM
  5. is there an infinitely large integer type?
    By MKashlev in forum C++ Programming
    Replies: 7
    Last Post: 08-10-2002, 02:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21