-6

Why does 10.0/3 return 3.3333333333333335? It does not seem correct, does it? Is it a bug?

10.0/3
# => 3.3333333333333335
sawa
  • 165,429
  • 45
  • 277
  • 381
getupsix
  • 1
  • 1

2 Answers2

1

This is not a bug, this is how floating point operations work. One should not care about mantissa too much, while

10.0 / 3.0 * 3.0 == 10.0
#⇒ true
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

Float objects represent inexact real numbers using the native architecture's double-precision floating point representation.

If you look at the Ruby docs, you'll see that float is double precision and is considered an approximate number.http://ruby-doc.org/core-1.9.3/Float.html

Floating point has a different arithmetic and is an inexact number. You should know its esoteric system. See the following:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

You could perform a round on the division to get a more accurate answer.

(10.0/3).round(5)
Charles Owen
  • 2,403
  • 1
  • 14
  • 25