1

I just encountered this and can't figure out why exactly Ruby behaves this way.

Could someone explain why in Ruby:

6.84 - 3.6 == 3.2399999999999998

and not just 3.24? I know that it's related to the binary representation of those number and that

(Decimal('6.84') - Decimal('3.6'))

would return the expected results. I'm just curious about the detailed explanation of the Float behavior.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Stan Bright
  • 1,355
  • 1
  • 15
  • 22

7 Answers7

3

See the answers in "Why is the 100*0.07 equal to 6.9999....?" (and many other places) about floating-point math.

Why is the 100*0.07 equal to 6.9999....?

Community
  • 1
  • 1
andyras
  • 15,542
  • 6
  • 55
  • 77
3

Because double/float use base-2 notation and decimal base-10 notation.

Here's a useful link: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

t3hn00b
  • 904
  • 5
  • 13
1

Float/Double is not that precise in most cases (because floats interpolate numbers with base2). The result of floating point operations may vary on different CPU's. The area close to .01 is more precise than the area close to .99

If you need to compare values calculated with float operations use an epsilon value (really small value) in order to erase rounding errors.

Pseudocode:

boolean equals(float x, float y) {
float result = x - y;
if(result*result < epsilon)
  return true;
else 
  false;
}  
Schifty
  • 625
  • 1
  • 7
  • 17
1

It's a binary floating-point problem.

read this

Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72
0

Base notation-2 and decimal base is used by double and float

Yusuf
  • 611
  • 4
  • 9
  • 21
0

0.01 or 0.07 cannot be precisely represented in binary floating-point.

Yusuf
  • 611
  • 4
  • 9
  • 21
0

Floating point uses an internal representation that's inherently imprecise. You should always round down your answers for display purposes:

'%.4f' % (6.84 - 3.6)
# => "3.2400"

Left to its own devices, Ruby, like many other languages, will express floating point numbers to a ridiculous degree of precision.

tadman
  • 208,517
  • 23
  • 234
  • 262