Public Group

# 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.

## 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 on other sites
Why should it be 6? 10 / 15 = 0, remainder 10.

##### 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 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 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 on other sites
ok so basically anything that equals 0.something is gonna return the left operand?

##### 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 on other sites
ok i get it, thanks

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 15
• ### Forum Statistics

• Total Topics
633393
• Total Posts
3011641
• ### Who's Online (See full list)

There are no registered users currently online

×