Apparent Math issues

Hello guys,

I’m just starting out with the Ruby language, and appear to either be
doing something wrong, or have run into a rather basic math bug. When
running “irb”, simple math additions seem to sometimes have weird
results. for instance.

1.9.3-p125 :022 > 120.02+82
=> 202.01999999999998

but then

1.9.3-p125 :023 > 120.03+82
=> 202.03

This anomoly seems to be repeatable on a friends box as well. At first,
I was only able to trigger this with a 3 digit number starting with a 1,
with .08 after, plus a 2 digit number. However, checked a few other
combinations have also had this kind of issue.

1.9.3-p125 :030 > 120.009+82
=> 202.00900000000001

Here is a few more examples

1.9.3-p125 :021 > 120.01+82
=> 202.01
1.9.3-p125 :022 > 120.02+82
=> 202.01999999999998
1.9.3-p125 :023 > 120.03+82
=> 202.03
1.9.3-p125 :024 > 120.04+82
=> 202.04000000000002
1.9.3-p125 :025 > 120.05+82
=> 202.05
1.9.3-p125 :026 > 120.06+82
=> 202.06
1.9.3-p125 :027 > 120.07+82
=> 202.07
1.9.3-p125 :028 > 120.08+82
=> 202.07999999999998
1.9.3-p125 :029 > 120.09+82
=> 202.09

Now since I’m quite new to this, I realize that this could of course be
something I’m doing wrong, but obviously, since I’m tyring to learn, I’d
like to know why I’m getting these results. Thanks in advance guys.

I’m not saying you should read all this, but this issue is a well
known fact about floating point math in programing languages, and it’s
present in lots of languages.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Gedece

On 3/30/12 9:16 AM, Shawn Carron wrote:

Gustavo Campanelli wrote in post #1054186:

Thanks for the link. We are both new to the language and were unsure of
what we were seeing.

If you need precision, you can look into Ruby’s BigDecimal standard
library. But often times float precision works OK for things that aren’t
money or similarly important values.

-greg

Gustavo Campanelli wrote in post #1054186:

I’m not saying you should read all this, but this issue is a well
known fact about floating point math in programing languages, and it’s
present in lots of languages.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Gedece

Thanks for the link. We are both new to the language and were unsure of
what we were seeing.

On Mar 30, 2012, at 5:36 PM, Steven Owens wrote:

Currency was my prime concerns with this. Thanks guys.

I would recommend to use one of the many money handling gems, for
example money :).

Currency was my prime concerns with this. Thanks guys.