2

Possible Duplicate:
Precision of Floating Point
Can you compare floating point values exactly to zero?
floating point issue

In my c++ code I have the string: "0.55".

And I want to convert it to float value "0.55", but all function I used give me the float value "0.55000001".

Why? How can I take the clear value?

Last version of code is:

wstring s = L"0.55";
float f = stof(s);
Community
  • 1
  • 1
Eugene
  • 389
  • 1
  • 7
  • 18
  • You should read more about floating point arithmetic: floating point numbers have a limited precision. You can't express all existing values. – ereOn Oct 24 '11 at 12:27

4 Answers4

4

Floats are not exact. This is because there are infinite number of possible values, but only finite number of bits to represent them!

So because 0.55 cannot be represented, it gives you the value 0.55000001 instead.

More info can be found in: what every programmer should know about floating points arithmetics

amit
  • 175,853
  • 27
  • 231
  • 333
  • 1
    The title of the paper says "computer scientist", not "programmer" - and that's exactly how it is written. See my reply for an attempt to provide a resource more useful for programmers. – Michael Borgwardt Oct 24 '11 at 12:30
  • Floats are exact. They just aren't real numbers, but something else (with notably different rules of arithmetic---addition isn't associative, etc.). – James Kanze Oct 24 '11 at 12:52
  • @MichaelBorgwardt Someone who isn't capable of understanding the paper cited shouldn't be writing programs using `float` or `double`. – James Kanze Oct 24 '11 at 12:53
  • @James: That's going way too far (not even counting languages that use floats implicitly like JavaScript). Elitism is not a useful approach to real-world problems. – Michael Borgwardt Oct 24 '11 at 12:58
  • @MichaelBorgwardt Solving real-world problems requires writing code that works, and has the desired semantics. The probability of this being the case if you use floating point without understanding the issues surrounding it is very small. And to date, the article cited is the **simplest** explication I've seen of the problems. (Not that I'm saying that you have to be able to work through all of the proofs in the article. But you should be able to understand the issues it raises, and why they are issues.) – James Kanze Oct 24 '11 at 13:17
  • @James: The simplest? Seriously? Well, I submit that I managed to write an explanation that is approximately 10 times simpler and still sufficient for most purposes (see my answer for link). – Michael Borgwardt Oct 24 '11 at 13:30
  • @MichaelBorgwardt The Floating-Point Guide cited in your response is sufficient to indicate that you shouldn't be using floating point without some additional knowledge, but that's about it. It basically says that there is a problem, but doesn't really explain what you need to do about it. – James Kanze Oct 24 '11 at 13:52
  • @James: It does explain WHY there is a problem. It doesn't go into designing numerically stable algorithms, but very few programmers are doing that. All they really need to know is to avoid floats for accounting, and simply accept the insignaificant rounding erros this question is based on. – Michael Borgwardt Oct 24 '11 at 14:36
3

0.55 does not have an exact binary representation. There will always be some rounding errors.

Dennis
  • 14,264
  • 2
  • 48
  • 57
2

There is no float value 0.55 - the format cannot represent that value.

Read the Floating-Point Guide for a detailed explanation.

If you need an exact representation for decimal fractions, use a decimal format, such as provided by the GMP library.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

you can use a little algebra and you will get 0.55 wstring s = L"0.55"; float f = stof(s);

use floor function
f = floor(f);

sarosh
  • 1
  • 1