Jump to content
• What is your GameDev Story?

• Advertisement

Public Group

modulus

This topic is 4909 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

Share on other sites
Advertisement
Why should it be 6? 10 / 15 = 0, remainder 10.

Share this post

Share on other sites
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

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

Share on other sites
Quote:
 Original post by gnomerwell 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

Share on other sites
ok so basically anything that equals 0.something is gonna return the left operand?

Share this post

Share on other sites
Quote:
 Original post by gnomerok 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

Share on other sites
ok i get it, thanks

Share this post

Share on other sites

• Advertisement
• Advertisement
• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• Popular Now

• 9
• 9
• 34
• 16
• 11
• Advertisement
• Forum Statistics

• Total Topics
634123
• Total Posts
3015651
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!