First of all I am using a c++ library ttmath, and I have a problem. I have a really big number declared like this:
ttmath::UInt<20> big1;
And I am calculating the number "100!".
It comes out as cca. 160 digit number, and now I have to add up all the single numbers, to get the total sum. So what I want to do is convert this to a string and then simply add up all the chars, simple right? The only thing not simple (since I am new to the library) is converting the number to a string (if at all possible). Now since it's a pretty long shoot to find somebody here who is such an expert at this library, I will also ask for any suggestions using some other techniques, to access the data, or for any other libraries where this task is possible, hopefully trivial with a ToString() function :)
Hehehe, I'm so stupid :P, I just copy-pasted the text, and then made a string by hand :P
Ok, so now the problem is solved, but I would still like to know if this can be done in a normal fashion.
In this case, it lets you use the String() function to convert any type of number/class to a string (if, of course, the class supports stringstreams and << and >> for going to/from stringstreams)
You could remove the template part of it, but then you would have to make a different version of the function for each type you would want to convert to a string.
This is the algorithm for calculating the numbers, but my question is, where to store the numbers of such huge proportions. ttmath::UInt<?>, this puppy would probably crumble, and I don't even know how big of a number would have to go in the <>, 1000, 3000, 10000, a gazillion, :P.
Just do a few range tests and see what has a larger range than that. As a starting base, I used a 2kb number (16384 bits) to calculate that. (probably overkill) You can go down from there. ;)
Remember the ttmath macro TTMATH_BITS(/*number of bits to use*/) which can go in the <>
But the ttmath::UInt will be able to handle it? (since in the FAQ it says that it is not meant to store more then <100> cuz it puts the data on the stack) (I don't know why I'm asking why I don't just try it :P). But I will ask you what you mean with "Just do a few range tests and see what has a larger range than that."?
It's just a typedef I made: typedef ttmath::Int<TTMATH_BITS(1024)> SInt1024;
And by doing a few range tests, I mean use an unsigned type and set it to 0, then subtract one from it. If you then print the number, that will be the highest number it can represent.
Here is the concept using a char as an example:
1 2 3
unsignedchar Num = 0;
--Num;
cout << Num; //Prints 255
Just do a few range tests and see what has a larger range than that. As a starting base, I used a 2kb number (16384 bits) to calculate that. (probably overkill) You can go down from there. ;)
There's a simple formula.
Given the number x^y in base 10, the number of bits needed to store this number is