Why does 10.0/3
return 3.3333333333333335
? It does not seem correct, does it? Is it a bug?
10.0/3
# => 3.3333333333333335
Why does 10.0/3
return 3.3333333333333335
? It does not seem correct, does it? Is it a bug?
10.0/3
# => 3.3333333333333335
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
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)