Adding big numbers

This is a discussion on Adding big numbers within the C++ Programming forums, part of the General Programming Boards category; I have a running total that should show up to one billion, maybe ten. I've tried adding with int's but ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    Adding big numbers

    I have a running total that should show up to one billion, maybe ten.

    I've tried adding with int's but ended up with a negative number.

    I tried float and double but got a results of 2.85067e+009

    How can I see it as a whole number like 285067763463?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    #include <iomanip>
    
    ...
    
    int main() {
    
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);
    
    ...
    Try adding that to your program.

    Anything bigger than your compiler's long double would require you to find a custom datatype that someone made. Try this:

    http://www.swox.com/gmp/
    Last edited by SlyMaelstrom; 11-18-2005 at 02:20 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    If the number is only going to be positive, you can use unsigned int instead of int. I think an int will hold +/- 2 billion. An unsigned int is roughly 4 billion. For larger numbers, you might consider "long int" if your processor is 64-bit, or a platform-specific 64 bit integer otherwise. For VC, the 64 bit int is type __int64.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    The _int64 worked. Problem now is if I try std::cout with this variable then I get "'operator <<' is ambiguous" at compile time.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Make sure you've included <iostream>, <iostream.h> gives that error.

    Also, use two underscores: __int64.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding large numbers
    By derek23 in forum C Programming
    Replies: 6
    Last Post: 07-22-2005, 09:02 PM
  2. Adding to a big char* for search engine
    By Landroid in forum C Programming
    Replies: 5
    Last Post: 03-03-2005, 07:16 PM
  3. Big numbers...
    By kjmagic in forum Windows Programming
    Replies: 3
    Last Post: 02-16-2005, 05:56 AM
  4. Adding double numbers in a function
    By ybala in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:36 AM
  5. 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

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