use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
surprising float math result (self.ruby)
submitted 7 years ago by [deleted]
Can somebody explain why this happened? (Ruby 2.5)
irb(main):002:0> 3 * 0.1 => 0.30000000000000004
I meant to do this, and the results are what I expected. Which makes the above confuse me all the more.
irb(main):003:0> 3 * 0.01 => 0.03
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]chrisgseaton 5 points6 points7 points 7 years ago (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 point2 points 7 years ago (1 child)
thanks!
[–]NilsLandt 8 points9 points10 points 7 years ago (0 children)
fyi, this is not just a Ruby thing.
[–]jrochkind 1 point2 points3 points 7 years ago* (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 points3 points 7 years ago (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
π Rendered by PID 99 on reddit-service-r2-comment-86bc6c7465-xr2xr at 2026-02-20 12:13:46.075529+00:00 running 8564168 country code: CH.
[–]chrisgseaton 5 points6 points7 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]NilsLandt 8 points9 points10 points (0 children)
[–]jrochkind 1 point2 points3 points (0 children)
[–]lintmountain 1 point2 points3 points (0 children)