Thread: large numbers

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

    large numbers

    how can i use numbers that are more then the limit in c++ like numbers that are over 100 digits long?

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Create your own set of classes based on strings, to act as somewhere to store the numbers. That allows you to have unlimited digit numbers (so long as they fit in memory/virtual memory etc). Then create your own bunch of addition, subtraction, multiplication and division operators for the class. Something like this:

    Code:
    class MyNumber
    {
        string TheNumber;
        ....
        ..operator =(..);
        ..operator +(..);
        ..operator -(..);
        ..operator *(..);
        ..operator /(..);
    }
    
    ....
    TheNumber newnumber = "50";
    TheNumber anotherNumber = newnumber * "200";
    Oh, you could also make it able to work with normal numbers as well as strings

    PS: I'm joking, this would be a really slow way of doing it (although I guess it could work... and kinda cuts out the need for multiple types too doesn't it, just need a string). Theres probably something out there you can use... maybe a float?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a library like this
    http://www.swox.com/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.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    instead of string, you can use vectors, they are like arrays.

    google this and you will find a lot of help since i had seen this as an assignment somewhere.

  5. #5
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    if you know linked lists, you can make a linked list... like a digit for each node (makes for ez math that way anyway).

    conceptually it's something like this...

    headpointer -> digit -> digit.... -> tailpointer
    1 -> 3 -> 2 -> 5

    to store the number 1325

    if you need more info and want to do it this way respond and ill give you more information on math with linked lists.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  6. #6
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Isn't there a BigInteger class somewhere in the standard library? Oh, and what about iterators? Can't they be used as big integers?

    trainee

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't there a BigInteger class somewhere in the standard library?
    No.

    >Oh, and what about iterators? Can't they be used as big integers?
    I'll wonder all night how you managed to make that connection. Iterators cannot be used as big integers to the best of my knowledge. Of course, I could be wrong.
    My best code is written with the delete key.

  8. #8
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Why not? Iterators as far as I know can store as many numbers as memory permits.

    trainee

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Iterators as far as I know can store as many numbers as memory permits.
    You obviously don't know what an iterator is then. Assuming we are thinking of the same thing (the iterator concept for standard containers and algorithms), an iterator is purely an abstraction of a pointer used to access a sequence. The correct sequence can store any amount of numbers, but the iterator itself only provides a convenient way to traverse that sequence and can only "point to" a single item in the sequence at one time.
    My best code is written with the delete key.

  10. #10
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    i was working on a class like this when i was bored one day. had everything worked out except for the fact of division. any links to sites on how to do division like thia? thanks

  11. #11
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    Wink

    repeated subtraction
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  13. #13
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32

    BigInt class

    Here's one a friend of mine created.

    BigInt.

    trainee

  14. #14
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Salem, thanks a lot for the link. Cause of this i figured out a neat ltitle project i can do.
    never woulda know to call it Binary division :P:
    Last edited by Iamien; 01-09-2004 at 09:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to deal wth large numbers
    By bvgsrs in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 06:16 AM
  2. Handling Large Numbers
    By Xzyx987X in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 02:33 PM
  3. Using really big numbers!
    By Machewy in forum C++ Programming
    Replies: 11
    Last Post: 02-26-2004, 10:49 AM
  4. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 12:01 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM