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 trialTucker Sowers
4,539 PointsWhy is 8/5 = 1? I am guessing ruby is rounding, but this was not discussed. Thank you.
Why is 8/5 = 1? I am guessing ruby is rounding, but this was not discussed. Thank you.
1 Answer
Jonathan Chua
4,136 PointsYou are using integers (whole numbers) in your equation, so Ruby automatically outputs an integer. An easy way to get a float (decimal number) is to change one of the numbers to a float. For example: 8.0.
Tucker Sowers
4,539 PointsTucker Sowers
4,539 PointsThis makes a lot of sense. Funny that no one mentioned that. Thank you very much.
Kyle Meyer
5,459 PointsKyle Meyer
5,459 PointsJonathan is correct. Since everything in Ruby is an object, what's happening is that you're calling the
/
(divide) method on8
, which is aFixnum
(aka an integer). That method looks to see if you're passing in another class of number with higher precision (like aFloat
), and returns that higher precision number.Likewise, if you called
8.0 / 5
, you're calling the/
(divide) method on8.0
which is aFloat
. Since you're passing in5
as the first parameter, it's going to doFloat
based math by default.