Stan Bright
Stan Bright

Reputation: 1375

Why 6.84 - 3.6 == 3.2399999999999998

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.

Upvotes: 1

Views: 331

Answers (7)

andyras
andyras

Reputation: 15920

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....?

Upvotes: 3

tadman
tadman

Reputation: 211610

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.

Upvotes: 0

Pier-Alexandre Bouchard
Pier-Alexandre Bouchard

Reputation: 5235

It's a binary floating-point problem.

read this

Upvotes: 1

Schifty
Schifty

Reputation: 635

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;
}  

Upvotes: 1

Yusuf
Yusuf

Reputation: 621

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

Upvotes: 0

Yusuf
Yusuf

Reputation: 621

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

Upvotes: 0

t3hn00b
t3hn00b

Reputation: 912

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

Upvotes: 3

Related Questions