Modulus operator Substitute for c#

I need to have a number be divisible by certain amounts and the remainders would then by posted and I’m programming in Unity’s C# but I need a replacement since the modulus operator doesn’t seem to function on it.

6 Answers

6

Just been stung by this and I couldn’t see a recent thread with a definitive answer.

Some languages, including Python, return the mathematical mod from %.
In C# it’s rem not mod. They behave differently with negative numbers.

int rem = -8 % 32;
Debug.Log(System.String.Format("-8 % 32 = {0}", rem));

Yields:

-8 % 32 = -8

This is an important difference when wrapping array indices.
The mathematical mod is 24, not -8 and can be calculated (somewhat inconveniently) as follows:

int Mod(int a, int b)
{
    return (a % b + b) % b;
}

Hope this helps someone else. I’m new to C# so if there’s a better way please post it.

O_o What? No, mod totally works.

using UnityEngine;
using System.Collections;
using System;

public class Blah : MonoBehaviour {
	void Start () {
		var x = 123213213;
		var factor = 10;
		var divs = x / factor;
		var remainder = x % factor;
		Debug.Log(String.Format("{0} / {1} = {2} // {0} % {1} = {3}", x, factor, divs, remainder));
	}
}

Yields:

123213213 / 10 = 12321321 // 123213213 % 10 = 3
UnityEngine.Debug:Log(Object)
Blah:Start() (at Assets/Blah.cs:11)

It wont work on floats though; maybe you need to run “((int) myVector[2]) % 5” or something.

It works fine with floats actually.

It keeps saying that it can't convert to a from an int to a bool when I try to compare the value is a multiple of a number. Example: if(1000 % 140){ //Run code here } error CS0029: Cannot implicitly convert type int' to bool'

ha! You want if((1000 % 10) == 0) (no remainder) or if ((1000 % 11) != 0) (remainder) (wow, I haven't seen code like that since I wrote C. :)

The modulus function works just fine in C#, not to mention Unityscript and Boo.

Try Mathf.Repeat

For those who are more curious, this article examines the fastest way to get the remainder between using modulus or alternative methods such as algebraic equations:

It’s obviously more for speed and micro-optimization junkies, but you’ll see several different faster ways than the straight modulus operation.

This is an old thread, but I felt compelled to write something for anybody who needs the answer to be explained and a quick example.

Short answer: Convert the int’s to var. Mod will then work.

// Wont work

int index = 3;

if(index % 2 == 0) {
}


//Will work

var indexVar = index;

var factor = 2;

if(index % factor == 0) {
}

How does this explain the answer? It's not even correct - % works just fine with ints as previously demonstrated. Your first code sample, commented // Wont work, works. It won't execute whatever is in the brackets, because 3 % 2 != 0....

You don't seem to understand what var does. It uses type inference to declare the type of a variable, which is purely a compiler convenience, nothing else. There is no difference whatsoever between "var x = 5" and "int x = 5". The variable is of type int in either case. And it is completely unrelated to the modulus operator in any case.