Thread: Huge Numbers in C++

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Huge Numbers in C++

    Hi guys...
    I'm pretty new to C++. I'm working with Visual C++ 6.0 on Windows XP, doing mainly console apps at the moment.

    My question deals with how C++ handles large numbers--like > 5,000,000,000. As I understand it, the biggest number that an int variable can handle is 2,147,483,647, and the biggest than an unsigned int can handle is 4,294,967,295. That ok for most programs... but I can't imagine that no C++ application is able to handle bigger numbers!

    Any solutions are welcome--either accepted methods or rough hacks. Is there maybe a header file that I can include that will give me additional data types? Like I said I'm pretty new to this (although I'm familiar with many web-based languages, like JavaScript, PHP, and ASP, so I know the principles of OOP programming). Any help you can provide is extremely welcome... but please treat me like the dumbest programmer you've ever met! All the details are important to me. Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    By the way, here's the program I'm working on. The goal is to figure out the probabity of 2 out of 100 people in a room sharing the same birthday. Right now, I'm trying to find the probability of the two people not sharing the same day, and the program is coded to generate the numerator:

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    	int numDays = 365;
    	int numPeople = 100;
    	unsigned int product = 1;
    	for (int i = 0; i < numPeople; i++)
    	{
    		product = product * (numDays - i);
    		cout << product << "\n";
    	}
    
    	return 0;
    }
    Thanks again!

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here are some threads you can read:
    http://cboard.cprogramming.com/search.php?searchid=5250

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <iostream>
    
    int main(void)
    {
      unsigned long long int x= (unsigned long long)-1;
      cout<<"Size:  "<< sizeof x<<endl;
      cout<<"Value: "<< x <<endl;
      return 0;
    }
    Output:
    Size: 8
    Value: 18446744073709551615

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Codeplug
    nice link...

    look around for a program called 'BigInt' or something to that effect... I've seen it around as a case study-type thing for school courses... I'm not sure if it'll do exactly what you want though...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There's the GNU MP library
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Damn, I just realized my link is no longer valid....

    It was a search on "GMP".

    gg

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Thantos your code gave me an error about long followed by long is illegal. I found that this though would give the same ouput:
    Code:
    #include <iostream>
    
    int main()
    {
    	unsigned __int64 x = (unsigned __int64)-1;
    	std::cout << x << std::endl;
    }

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    __int64 is MSVC specific....

    change "unsigned long long int" to "unsigned long long"

    gg

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try the following:

    cout<<sizeof (long);

    If it is 8 then use unsigned long instead of long long.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by Codeplug
    change "unsigned long long int" to "unsigned long long"
    My bad....

    "long long int" is C99 and is suppored in C89 mode by GCC as a compiler extension.

    Here's the scoop:
    http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html

    gg

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Well normally you would use a 'long' variable for these types of things, as those types of variables reserve more memory, they can take a greater value. And then theres math where you can seperate variables and treat them like normal numbers in base 10. For instance if you had a huge number..lets say 500000000000000000..just some huge number. If you seperated that into multiple variables and _dealt_ with them as one in your programs math/calculations, then you have a way of actually accomplishing that feet.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    i also need to use big numbers in an encryption program i am working on.
    but i need numbers about 1 * 10^1000
    how do i get numbers so big?
    i downloaded that gmp, but i get lots of errors

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    how do i get numbers so big?
    I would write a class to replace all the C++ primitives. By overloading all the required operators, it can function like a standard primitive. It would store your large numbers by splitting it up into sections that can be described by scientific notation, and then added to each other. Or something like that.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting numbers in arrays
    By rachael033 in forum C++ Programming
    Replies: 10
    Last Post: 05-29-2007, 02:56 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM