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 trialMiika Vuorio
8,018 PointsWhy 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
gregsmith5
32,615 PointsCheck out this video by Computerphile - Floating Point Numbers.
Jason Anders
Treehouse Moderator 145,860 PointsHey 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"
Miika Vuorio
8,018 PointsThanks for the answer.
Miika Vuorio
8,018 PointsMiika Vuorio
8,018 PointsThanks. That video was very helpful.