Sign in to follow this  
derek7

atof and data loss

Recommended Posts

float key = atof(uiIter->second.c_str()); said double to float may lost data so which is better way? // lessbread edit - added a title

Share this post


Link to post
Share on other sites
atof returns a double. The compiler is just warning you that you may lose precision, since floats don't store as much data as doubles, although if that is not a problem you can use a float.

Share this post


Link to post
Share on other sites
in my terrain a lot of data is int type and then I convert it float do more precious calculation.but vc said int -> float may lost data. so which occasion will lost data? how I do better ?

Share this post


Link to post
Share on other sites
If you convert an int with the value 123456789 to a float, the result will be 123456784.0 because there are not enough bits in a float to represent the number exactly.

Share this post


Link to post
Share on other sites
It does not lose data when you call the function. It loses data when you store the result in a float. The function returns a double, which is larger than a float, so therefore some of the decimal places maybe lost. Because your data was originally an int this a non-issue for you.

adit: unless you have a very large integer, like JohnBolton said.

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