Thread: Big number implementation.

  1. #16
    Registered User
    Join Date
    May 2005
    Posts
    76
    I don't understand. If I have a number for instance 10000000100 and the base 100000 I will have [100000][100] but I should have something like [100000][00100], right ? But I have those first two zeros lost. I can introduce for example one more extra variable which would keep the number of leading zeros - in this case 2, but maybe there is some other way of solving it without any other variable ?
    --
    Regards,
    apacz.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If I have a number for instance 10000000100 and the base 100000 I will have [100000][100] but I should have something like [100000][00100], right ? But I have those first two zeros lost. I can introduce for example one more extra variable which would keep the number of leading zeros - in this case 2, but maybe there is some other way of solving it without any other variable ?
    Set the required width(), and set the fill() character to '0', and ensure that your output is right justified.
    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

  3. #18
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Fill the digits into a temporary buffer (eg. std::stringstream), then do iomanip-ulation (post above), then return as std::string. Most flexible.

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by n3v
    there's another clever little system you can use. it's called a 'letter array'. put the number into a char array, but with a really high uh.. whats the word.. ah, a really high number base. for example, our normal counting has base 10, and hexidecimal has 15, and binary has 1. that means, when the amount of the number gets to that point, it adds on another place holder. for example, here's some numbers written with different bases:
    You Twit, everyone knows that hexadecimal is base 16 and binary is base 2!

    To the OP: If you don't want to perform any mathematical operations on them then you don't need a big number library, at least not in the usual sense. Just store them as strings and be done with it. In other words, base 256 will be just fine.

  5. #20
    Registered User
    Join Date
    May 2005
    Posts
    76
    Hi,
    Ok, now I know how to deal with it. I just haven't realized that if the number is >15 digits long then in the 'b' there will be always 15 digits so it's easy to calculate the number of leading zeros even without the visible represenation of them. Thanks for the help,
    --
    Regards,
    apacz.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. Big number question
    By C of Green in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2007, 12:06 PM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. Big help for big program
    By Mahesh in forum C Programming
    Replies: 1
    Last Post: 05-04-2005, 10:02 AM