### Variable Choice

I'm writing a Statistical Calculator for a videogame. Some of the statistics use numbers that are longer than 16 digits, and from what I can tell, a `long double` can only hold up to 15 digits (or double).

Should I use a variable to store this data, or would using an algorithm to split it into smaller numbers and re-construct it from those numbers be more suitable?
I'd just grab a library to handle this, unless you wanna handle it yourself.
This one is pretty lighweight and will definitely cover any number you could want.
http://www.ttmath.org/faq

Or,

http://gmplib.org/

I think is the more "official" library, though I do believe it is a bit larger.
Last edited on
I would prefer to handle these numbers myself.
Floating point numbers are designed to be able to handle very small values and very large values. They can contain much more than 15 digits but only the 15 right-most digits will be without rounding errors, loosely speaking. You will get rounding errors but for many applications they are good enough.

The precision of the different floating point numbers really depends on your compiler. You can run this program to find out.
 ``123456789`` ``````#include #include int main() { std::cout << "float: " << std::numeric_limits::digits10 << std::endl; std::cout << "double: " << std::numeric_limits::digits10 << std::endl; std::cout << "long double: " << std::numeric_limits::digits10 << std::endl; } ``````
Last edited on
Interesting, ty peter. I will investigate that.
If you are using an int. You may use long long as a type which can represent upto 8 bytes integers.

 ``123`` ``````//for example long long a; cout << sizeof(a) << endl; //8 will be printed ``````

You could use a char array to store nos. See the foll code as an example:

 ``1234567891011121314`` ``````//code for adding 2 20 digit nos. char n1[20],n2[20], sum [22]; int s; cin >> n1 >> n2; sum[0]=0; for(int i = 0;i<20;i++) { s = (n1[i]-48) + (n2[i]-48); sum[i+1]=s%10; sum[i]+=(s/10)*10+48; } sum[22] = '\0'; cout<

There are similar libraries available on the web like a good one is ttmath(http://www.ttmath.org/)
Topic archived. No new replies allowed.