2

Whats wrong here? (ruby version: 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]

x = 523.8
w = 46.9
xm = x + w
assert_equal w, (xm - x) # FAILS with: <46.9> expected but was <46.89999999999998>
mskfisher
  • 3,291
  • 4
  • 35
  • 48
florianguenther
  • 235
  • 2
  • 4
  • 10
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) – dee-see Aug 17 '11 at 11:45
  • 1
    @Vache: unfortunately this site is for programmers, not computer scientists, and Goldberg's paper rather more complex and detailed than people can be expected to accept as an answer to a simple question. – Michael Borgwardt Aug 17 '11 at 11:50
  • 2
    Which is why I posted this as a comment and not an answer. :) – dee-see Aug 17 '11 at 11:51
  • 3
    "programmers, not computer scientists" - they ought to be synonymous, enough to be able to understand that article and floating point numbers. – duffymo Aug 17 '11 at 13:43
  • This has to be a duplicate question. – Andrew Grimm Aug 17 '11 at 22:21
  • @Michael Borgward: I am neither a programmer nor a computer scientist, I am just a dentist with a hobby, and yet I had no problem understanding the Goldberg article. I think a "programmer" (as opposed to a "computer scientist") should be able to read it, with some effort. – Rudy Velthuis Aug 18 '11 at 20:02
  • @duffymo: in practice they are not synonymous at all. Programmers generally don't need to read scientific papers to do their work, and computer scientists generally don't need to write user-friendly and maintainable software to do theirs. – Michael Borgwardt Aug 18 '11 at 20:15
  • @Rudy: with some effort perhaps, but it would take several hours, and thus not a reasonable answer to this kind of question. – Michael Borgwardt Aug 18 '11 at 20:20
  • 1
    I think it is, since it explains what people should know about floating point. That can prevent a lot of additional questions. Some things can't be explained in a few sentences. – Rudy Velthuis Aug 18 '11 at 21:33

4 Answers4

10

From The Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

Read the linked-to site for details and ways to get around this.

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

This is perfectly normal; it is a fact about the lower-level concept of floating point arithmetic rather than Ruby and therefore can occur in any language.

Floating point arithmetic is not exact. Equality should be replaced with closeness along the lines of assert((xm-x).abs < epsilon), where epsilon is some small number like 0.01.

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
1

Read this. It describes the way binary representation of floating point numbers work in every language, not just Ruby.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    Is the (sigh) necessary? – shevy Jun 04 '14 at 11:44
  • It is you who brings in "reputation" - reputation alone does not mean anything at all. You are being presumptuous in your reply. Others manage without that attitude and provide better answers - see the one above your reply. Unlike you I find the question perfectly well suited and well deserving for an answer. If you have a problem answering without an attitude, perhaps don't answer at all? I am not the one to attack newcomers with a "sigh". Why are you such a drama queen? If there is anyone trolling then it is you - from a technical point of view, there is no need for "sigh" emoting. – shevy Jul 05 '14 at 03:19
0

The answer to your question is: No.

(Other answers tell you why, but you didn't ask that. :p)

Phrogz
  • 296,393
  • 112
  • 651
  • 745