atof and data loss

Started by
3 comments, last by Kelly G 18 years, 4 months ago
float key = atof(uiIter->second.c_str()); said double to float may lost data so which is better way? // lessbread edit - added a title
Advertisement
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.
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 ?
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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.

This topic is closed to new replies.

Advertisement