how can i use numbers that are more then the limit in c++ like numbers that are over 100 digits long?
how can i use numbers that are more then the limit in c++ like numbers that are over 100 digits long?
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:
Oh, you could also make it able to work with normal numbers as well as stringsCode:class MyNumber { string TheNumber; .... ..operator =(..); ..operator +(..); ..operator -(..); ..operator *(..); ..operator /(..); } .... TheNumber newnumber = "50"; TheNumber anotherNumber = newnumber * "200";
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?
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.
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.
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
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
>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.
Why not? Iterators as far as I know can store as many numbers as memory permits.
trainee
>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.
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
repeated subtraction
"uh uh uh, you didn't say the magic word"
-Jurassic Park
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.
Here's one a friend of mine created.
BigInt.
trainee
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.