Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Numbers Ruby Numbers

Why does ruby do this?

irb(main):001:0> a = 1.1

=> 1.1

irb(main):002:0> b = 2.2

=> 2.2

irb(main):003:0> a + b

=> 3.3000000000000003

why does it add that three in the end even tho 1.1 + 2.2 = 3.3

2 Answers

Check out this video by Computerphile - Floating Point Numbers.

Thanks. That video was very helpful.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Miika,

I don't fully understand the 'math' behind this, but it's not just Ruby. Any compiled programming language will have this happen with floating point numbers. It has to do with how machine language (binary) deals with the decimal number.

If your really curious, you can search for something along the lines of "problems with floating point numbers in code"

:dizzy:

Thanks for the answer.