Jump to content
  • Advertisement
Sign in to follow this  
gnomer

modulus

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At the risk of sounding like an idiot. Why does 10 % 15 = 10 and not 6? im confusing myself with this modulus operator in a function im using. It works as intended but I'm just confused why that is returning 10 and not 6. I guess I just don't totally understand the inner workings of modulus but I thought I knew how it worked :)

Share this post


Link to post
Share on other sites
Advertisement
Modulus returns the remainder of a division:
5 % 2 = 1 (5 / 2 = 2 remainder 1)
etc

I'd give you a better explanation, but thats about all I know.

Share this post


Link to post
Share on other sites
well 10/15 = 0.666666666666666667... so i was using the first 6. I guess that's wrong but that's what I was thinking.

Share this post


Link to post
Share on other sites
Quote:
Original post by gnomer
well 10/15 = 0.666666666666666667... so i was using the first 6. I guess that's wrong but that's what I was thinking.


That is correct, but modulus returns the remainder. So:
10 / 15 = 0.667
10 - 0.667 (rounds up)
10 - 0 = 10

Share this post


Link to post
Share on other sites
Quote:
Original post by gnomer
ok so basically anything that equals 0.something is gonna return the left operand?


yes the % operator is for integers only.

To do floating point modulus use the fmod or modf functions found in cmath.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!