Jump to content
  • Advertisement
Sign in to follow this  
Aerts

too big number

This topic is 4525 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!