1

I always wondered why the floats aren't really acurate when computers should give the precise answer. I read in a book somewhere that it is better to compare a variable to a number around the value we want, since the calculate value may not always be a whole number as we expect. How do machines caluclate these divisions? Any links to websites are welcome :)

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
  • Perhaps you can find something of value in this post: http://stackoverflow.com/questions/872544/precision-of-floating-point – DeCaf Oct 07 '11 at 17:10

2 Answers2

3

Jon Skeet mentions it here (scroll down till you see "double d=0.3;" drawn on a slide): http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx

A more detailed answer here: http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

Tom
  • 964
  • 9
  • 25
  • The link http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx is no longer available. However, I accessed it through https://web.archive.org (the snapshot Aug 14, 2010) – Maksim Dmitriev Jan 14 '17 at 12:30
0

a simple answer would be that a computer uses a limited amount of digits to represent a number.

If you try to represent i.e. the number 1/7 in decimal it would be 0.14285714... and so on infinitely. The same happens for computer i.e. trying to represent the number 1/10 (0.1 in decimal) in binary which becomes an infinite series as-well.

Therefor sometimes you don't get the most accurate number.

Omi
  • 16
  • 1