Q:

How to add floats but keep/wrap the total within the range 0..1?

I'm doing some animation on several alpha values and want to set the values relative to a reference value 
obj1.alpha = refAlpha + 0.25f;
obj2.alpha = refAlpha + 0.75f;

If refAlpha is 0.5 for example then I want obj2.alpha to be 0.25 not 1.25. 
What math function is there to do that?

A:

The mod operator % will do the trick in terms of wrapping. (it works on floats as well as ints). 
obj1.alpha = (refAlpha + 0.25f) % 1f
obj2.alpha = (refAlpha + 0.75f) % 1f

public static void main(String[] args) {
  System.out.println(1.25f % 1f); // --> 0.25
}

Unfortunately, java doesn't quite implement mod to the mathematical definition. a % b returns a value in the range (-b, b), as opposed to [0, b). If you are ever subtracting values, the result could therefore be negative. If this is a usecase you are worried about, consider creating a helper function to handle it:
public static float mod(float a, float b){
    return ((a % b) + b) % b;
}

This will make the result in the range [0,b).

