-2

I came across this the other day and sure its not causing me any trouble but i'm just curious as to why it happens?

1.9.2p320 :001 > 0.39-0.09
 => 0.30000000000000004
saGii
  • 447
  • 5
  • 14

2 Answers2

1

This is because Ruby by default uses Double-precision floating-point format. You can read about issues related to it here. However here's a short and crisp answer:

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.

Source: http://floating-point-gui.de/

shivam
  • 16,048
  • 3
  • 56
  • 71
0

Floating-point numbers cannot precisely represent all real numbers, and floating-point operations cannot precisely represent true arithmetic operations, this leads to many surprising situations.

I advise reading: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

You may want to use BigDecimal to avoid such problems.

spickermann
  • 100,941
  • 9
  • 101
  • 131