Jump to content
  • Advertisement
Sign in to follow this  

Some wired problem with floats

This topic is 2941 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

Hello! I have a problem with floats. My engine writes down the engine version at the beginning of the map, just like 0.09, but when it reads it again, the 0.09 transformed to 0.89999999 or something like that. What can I do against that?

Share this post


Link to post
Share on other sites
Advertisement
Version numbers are not floating-point numbers.

The probem you're having is that the decimal value 0.09 does not have a finite binary representation. No matter whether your data file format is text or binary (though I assume it's binary) you should use integers for version numbers, for the same reason you'd use an integer for index numbers.

There are many ways to store version numbers. It's largely up to you, but you may find, for example, that two integers, for the major and minor version numbers, will be sufficient. In a binary format, you can store these as one byte each, or as the upper and lower four bits of one byte.

Share this post


Link to post
Share on other sites
Yes, I know this. Just to lazy to edit my posting ;) So, 0.09 will become 9 and 0.10 will become 10 and 1.23 will become 123. Thats how I'm going to do it.

Share this post


Link to post
Share on other sites
Use strings. Or if you want to keep the ability to sort version numbers correctly, store it as a set of 2 or 3 integers. But you may want to pad the integers with zeroes, as something like 1.02 will come out as just 1.2. You can use the setw and setfill functions which are included with the <iomanip> header.


int v0 = 1;
int v1 = 2;
int v2 = 45;
cout << "Version: " << v0 << "." << setw(2) << setfill('0') << v1 << "." << setw(4) << v2 << endl;

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!