conversion between string and float/double

Hi all can any one tell me why when I convert std::string to float or double type the decimal part of value is not exactly as in the string ?
for example float value 1.56 loaded from file and converted to float is 1.55999994
can you explain why this is happening and how I can fix this ?
An infinity of values cannot be perfectly represented with floating point numbers. The best it can do is come close.

The path to fixing this begins with understanding what a floating point number is, and what it is, and is not, capable of.

http://floating-point-gui.de/

Here is a page that will show you what various decimal numbers will be represented as using the standard IEEE floating point representation. Note that 1.56 comes out as 1.559999942779541 - which is what you found when you tried it.

http://www.h-schmidt.net/FloatConverter/IEEE754.html
Last edited on
Topic archived. No new replies allowed.