BugsBunny
BugsBunny

Reputation: 99

Ruby Fixnum Division Not Calculating As Expected

In irb, the result of -3/4 is -1, not 0. However, the result of 3/4 is 0, as expected.

Does anyone know what happened?

Thank you!

I get the floor idea, thank you!

Upvotes: 0

Views: 67

Answers (1)

MatayoshiMariano
MatayoshiMariano

Reputation: 2096

This happen because the / operator performs the floor operation.

So -3/4 is equal to -1 and not zero. And 3/4 is equal to zero and not 1.

fix_div is the method ruby uses to do the division. And fix_div uses fix_divide, and here you can see where the floor method is called.

The fix_div and fix_divide are C methods.

Upvotes: 6

Related Questions