# % modulo operator with Mathf.PI

Hi
I am scratching my head :

``````120%60= 0 //OK
49.5%5.5=0 //OK
4.71%1.57=1.57 // how is is possible?
(3*(global::UnityEngine.Mathf.PI/2))%(global::UnityEngine.Mathf.PI/2)= 1.570796 ?
471%157 = 0
``````

It’s quite strange, it seems to link with the variable type.

I need to make this opération :

``````float d = X -X % (Mathf.PI / 2);
``````

Where X is between 0 and 2Mathf.PI , but the result is not the one expected when X = 3PI/2

The reason that you are seeing this, is because of floating point imprecision.

You can manually check whether the difference between the outcome and the modulus of the operation is smaller than a set epsilon (representing a very small number), and set it to zero.

For example:

``````float result = a % b;     // 1.570796

float epsilon = 0.0001f;
result = Mathf.Abs(result - b) < epsilon ? 0f : result;    // 0
``````

Or you can create a custom method:

``````public static float Modulo(float a, float b, float epsilon)
{
return Mathf.Abs(a % b - b) < epsilon ? 0 : a % b;
}
``````