0

Regarding multiplication,

puts 8.52*10; #multi

the answer is 85.199999 it is not 85.2. why does it happen.

Hansa Tharuka
  • 137
  • 1
  • 5
  • 1
    In Ruby the fix for this is to use the BigDecimal class for storing numerics. https://ruby-doc.org/stdlib-2.1.1/libdoc/bigdecimal/rdoc/BigDecimal.html – David Aldridge Aug 21 '18 at 13:42

1 Answers1

3

The simple answer is that there is no floating point number equal to exactly 85.2.

One way to understand this is because floating point numbers are basically represented using scientific notation in base 2.

This video does a really good job of explaining.

Floating Point Numbers - Computerphile

https://www.youtube.com/watch?v=PZRI1IfStY0

AdaRaider
  • 1,126
  • 9
  • 20