0

I'm learning Ruby for fun, and for creating websites also (but that's irrelevant). While playing with it, i noticed something "weird"

When I compute 4.21 + 5 with irb, it answers 9.21 (weird, right?)

when I compute 4.23 + 5, it gives 9.23 (waw, that's definitely weird).

and when i type 4.22 + 5, it answers 9.21999... (w...wait! that's really weird).

Hence my question: what's going on? I'd understand this behavior with division or really big numbers, but in this simple case....???

Does it mean that i can't develop an accounting app with Ruby? Is there a patch or something to be applied? (to my brains, most likely)

5
  • what's wrong with 4.21 + 5 = 9.21? Commented Mar 24, 2011 at 13:35
  • "Does it mean that i can't develop an accounting app with Ruby?" No. Ruby is a good language but you have to be aware of what numeric type to use when calculating, which is true in other languages. Commented Mar 24, 2011 at 15:12
  • possible duplicate of ruby: converting from float to integer in ruby produces strange results Commented Mar 24, 2011 at 22:16
  • @the Tin Man: You're right..It's just that I didn't know much about floating point representation and didn't know about any alternative to float in Ruby. Now BeepDog gave a solution with the Decimal package. @Andrew: Don't think it's a duplicate. Both are about Floating point representation but this question also rise the problem of developing financial apps - plus it's for much simpler calculations Commented Mar 25, 2011 at 12:49
  • 1/10 in binary is 0.0(0011) where the terms in the parenthesis are repeated digits. Try writing the entire value of 1/7 in decimal and then we'll talk. :) Commented Mar 13, 2013 at 20:33

2 Answers 2

5

You should read up on floating point representation in computers. This guide is a good place to start. Here's the short of it:

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

By the way, I'm not sure why you think 4.21 + 5 = 9.21 or 4.23 + 5 = 9.23 is weird. If you think it's weird because one literal is an integer and one is a float, which ends up as a float response, that's how Ruby and some other languages handle differences in number types. It wouldn't be handy if Ruby dropped your float's precision and gave you just an integer back (9), so any integer literals are effectively changed to floats.

As for using floating-point numbers in a financial application, check out Developing financial application. The main takeaway is use the Decimal object vs. Float.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the link!I used some other languages in the past, and it's the first time i see this behavior with such small numbers... Is there a way to work around that? I mean, if i want to develop a program for an accountant, there must be something somewhere to prevent that...
@user614167 - I updated my answer to include info on financial software. Btw this isn't a Ruby only scenario. Any language that uses the IEEE 754 Floating Point standard will run into the same issues. You must have gotten lucky my friend :D
Yeah, I'm a lucky guy in general.See..? I even got that nice answer from you in less than 10 minutes!
Thanks McStretch... P.S: I don't think 4.21 + 5 = 9.21 or 4.23 + 5 = 9.23 is weird. I was just creating suspens. I was unsuccessful and i won't do it again, I promise...:)
Haha yeah. Keep in mind, going forward, that you should be wary of any languages floating-point implementation when considering its use in financial applications (read: avoid like the plague). Each language will generally have another object to use or a workaround that you should take note of. Finally, if this answer is correct can you accept it please?
|
1

http://ruby-decimal.rubyforge.org/

This is the package you'd want to do reliable Floating Point maths in ruby. The Developint Financial Applications link doesn't reference it, but it makes the important point of using Decimal data types in your database.

Just noting this since it was non obvious for me :)

1 Comment

Thanks for the link BeepDog! That's just what I was looking for. Note:Should work on my google skills....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.