Sign in to follow this  
mind in a box

Some wired problem with floats

Recommended Posts

mind in a box    887
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
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
taz0010    277
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this