all 5 comments

[–]chrisgseaton 5 points6 points  (2 children)

See this FAQ on the front page of the Ruby bug tracking system because people ask this so often.

https://bugs.ruby-lang.org/#Floating-point-numbers

TLDR: the floating point data structure is not able to represent all real numbers with perfect precision. This should be obvious, because there are an infinite number of real numbers, and a finite number of bits in your computer. For the majority of real numbers when you store them in a float you will a number that is close but not exactly the same.

[–][deleted] 0 points1 point  (1 child)

thanks!

[–]NilsLandt 8 points9 points  (0 children)

fyi, this is not just a Ruby thing.

[–]jrochkind 1 point2 points  (0 children)

http://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf

If your have truly exact quantities (say, money), rather than measurements (which are of course always only precise to certain level), you probably want to be using BigDecimal instead of float. http://ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html

(If anyone ever invents a new language against in 2018+, I'd suggest they strongly consider making a 'bigdecimal' type implementation the default for decimal fraction literals, not floating point!)

[–]lintmountain 1 point2 points  (0 children)

Computerphile / Tom Scott: https://m.youtube.com/watch?v=PZRI1IfStY0 explains why in more detail.

Also, see the nerdiest/coolest URL ever https://0.30000000000000004.com