-5

I need simple math operations with some floats like "3.3 - 2.6". The result is 0.700001. I have no idea why there are numbers like 0.700001.

Some examples

Can you help me please?

mleko
  • 11,650
  • 6
  • 50
  • 71
Eric3235963
  • 1
  • 1
  • 5
  • 2
    "floats and a wrong result?" - yes, you get wrong results using floating-point numbers. that's how they work. but you couldn't *possibly* google "C++ floating-point arithmetic wrong result", right? –  Jan 25 '14 at 19:39

4 Answers4

3

You need to read What Every Computer Scientist Should Know About Floating-Point Arithmetic

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – hyde Apr 23 '14 at 22:37
  • @hyde Perhaps it should be re-titled [What Every Computer Scientist Should READ About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). And when oracle is so down and out it that it has to throw the *off* switch on its `doc` site we'll be well past caring anyway. – Paul Evans Apr 24 '14 at 00:11
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – senshin Apr 24 '14 at 00:38
  • @senshin So, because you can't be bothered reading a *very-well-written* (relatively-short) seminal essay on this *very* topic you dis me for not spoon feeding it you? One can lead a horse to water *etc*... – Paul Evans Apr 24 '14 at 00:46
  • @PaulEvans It is generally accepted policy on Stack Overflow to prefer that answers be self-contained, or at least not be wholly dependent on external resources. Naturally, I agree with you that "What Every Computer Scientist..." is a brilliant piece of writing, having read it myself, but bare links to things typically function better as comments. In any case, this question is abominable, and I have voted to close it. – senshin Apr 24 '14 at 00:51
  • @senshin what part of "What Every Computer Scientist Should Know About Floating-Point Arithmetic" don't you get? This *is* the completely distilled info. I believe that stack overflow is about achieving excellence, perhaps I'm wrong. You seem to be telling me it's more about short-cuts and forget about the consequences. – Paul Evans Apr 24 '14 at 01:04
  • @PaulEvans Be as it may, fact remains that link-only answers, which are useless if link breaks, are frowned upon at SO, and best given as comments. Additionally, a big corp like Oracle might break a link to a doc like that without warning, for any PHB reason or just by accident, so I don't see why you'd believe that the link will stay valid longer than SO will stay in business. – hyde Apr 24 '14 at 05:16
2

The computer handles numbers in binary format with finite precision. Various simple decimal numbers cannot be precisely represented, so it gives the closest answer possible.

The usual way of dealing with the problem is simply to limit the precision you actually display. If you're using C++ streams for output then the std::fixed and std::setprecision manipulators should help.

Peter Bloomfield
  • 5,578
  • 26
  • 37
1
3.3 is represented in memory as 3.29999

and

2.6 is represented in memory as 2.59998

so

 3.3 -2.6 = 0.70001

this post might give you a little more insight on the matter

Community
  • 1
  • 1
Pandrei
  • 4,843
  • 3
  • 27
  • 44
0

Maybe that happens because of float precision. Try using double.

eli
  • 156
  • 5
  • 15
  • it's actually because of the day decimal numbers are represented in memory – Pandrei Jan 25 '14 at 19:35
  • 2
    Using `double` instead of `float` will just make the problem smaller and push it further from view. You can still get oddities. – Teepeemm Jan 26 '14 at 11:51