What does mathf.repeat actually do?

I was reading the unity 3d documentation and there is a math function called mathf.repeat.

It says it loops the value t so its value is between 0 and the length you enter…

What could this be used for and what exactly does it mean to “loop the value t”?

This function returns the remainder of the division firstArgument/secondArgument. It’s like the modulo (%) operator, but works for float numbers as well. Actually, the modulo operator also works with floats in Unity - these two alternatives print the same result:

print(Mathf.Repeat(2.45, 2.0).ToString("F2")); // prints 0.45
print((2.45 % 2.0).ToString("F2")); // also prints 0.45

Two of the most frequent uses of Mathf.Repeat or % in Unity are:

1- Keep angles in the 0…360 range for eulerAngles or localEulerAngles (an angle 375 becomes 15, which is the same thing):

transform.eulerAngles = Vector3(0, curAngle % 360, 0);

2- Keep texture offset inside the 0…1 range (shaders may render ugly artifacts when big values are passed as texture offsets):

renderer.material.mainTextureOffset = Vector2 (Time.time % 1, 0);

Simple example is an angle: If you want to make sure your angle is always between 0…360 (because your algorithm required it), but your input value can be anything (because the user can input anything), you can use it. For example, -100 and 260 and 620 all represent the same angle, which is 260. This will map your arbitrary value back to the 0…360 interval:

actualAngle=Mathf.Repeat(inputAngle,360);

Note that this is a nice convenience function, but if you need to evaluate it several thousand times per frame for some reason, you will be much faster implementing it by hand:

actualAngle=((inputAngle/360f)-(int)(inputAngle/360f))*360f;

EDIT: even more optimized, thanks @Bunny83:

actualAngle=inputAngle-((int)(inputAngle/360f))*360f;

EDIT2: somehow I missed that (int) will not handle negative values correctly. So when using that optimization, you’d need to catch that case (which will still be much faster than Mathf.Repeat() or using Mathf.Floor()):

actualAngle=inputAngle-((int)(inputAngle/360f))*360f;
if(actualAngle<0)
    actualAngle+=360f;

That method looks something like this:

return t - Mathf.Floor(t/length) * length;

Example:

Mathf.Repeat(253.22f, 250f) = 3.22f // You can fit one 250 in 253.22f, and the rest is 3.22f 
Mathf.Repeat(503.22f, 250f) = 3.22f // You can fit two 250 in 503.22f, and the rest is 3.22f

You can use it to get the proper Euler angle, ‘Mathf.Repeat(someAngle, 360.0f);’.
If ‘someAngle’ is 362, it will return 2. (And you know that when it comes to Euler angles, 362 is same as 2)