• Advertisement
Sign in to follow this  

too big number

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a number that's 30 digits big. Which C++ variable type can properly store it? I've tried long unsigned, long and double without luck.

Share this post


Link to post
Share on other sites
Advertisement
None of the standard C++ types will be able to store that number. You'll need to create (or find online) A Big Int class to handle numbers that large.

Here's the first one Google came up with

Share this post


Link to post
Share on other sites
Depends on what you need it for. If it's just for display and some very simple math you can always use byte arrays. Store a digit between 0 and 9 within each element and you can display numbers of arbitrary size. What do you need such a big number for anyway?

Greetings, Roga

Share this post


Link to post
Share on other sites
Quote:
Original post by Aerts
I have a number that's 30 digits big. Which C++ variable type can properly store it? I've tried long unsigned, long and double without luck.

Are all of the digits significant? 2^100 for example has roughly 30 digits and can be stored perfectly in a float. 2^1000 has roughly 300 digits and fits in a double.

Share this post


Link to post
Share on other sites
To Rogalon: I just created a program that reads a player's health from a file and just thought to try it for fun.
To Fred304: This is roughly my code:

int health;
ifstream ifs;
ifs.open("player_health.txt");
ifs>>health;

Share this post


Link to post
Share on other sites
Why not just make it a smaller number? Or read it in as a string?

Share this post


Link to post
Share on other sites
Yes if your player's health uses 30 digits you might want to think about changing the current system ;)

We created a Big Int class in my highschool comp sci. class. It was pretty fun actually, you create a vector of single digits and then read it off as a string. Although you have to program the overloaded operators yourself (not very hard).

Share this post


Link to post
Share on other sites
You can store a 30 digit number in a 128-bit integer. Some compilers support 128-bit integers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
30 digits for a player's health??? why do you want that much?


I too would like to know the answer to this. And assuming you did need 30 digits, I second GMP.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement