Let's say I have a float called foo. And foo increases or decreases at certain intervals. How can I make foo start from zero again once it succeeded a specified number / and the same in reverse for the decreasing?
For example:
int max = 0;
int min = 50;
float foo = 45;
foo += 7.5;
foo
would be 52.5 now. But since I specified 50 at the max number, i want it to sort of overflow at that point so that the result is just 2.5.
Or:
int min = 0;
int max = 50;
float foo = 45;
foo += 108.3;
the result should be 3.3. It just overflowed 3 times.
And for the reverse:
int min = 0;
int max = 50;
float foo = 1;
foo -= 5.5;
the result would be -4.5 but it should be 44.5.
I was thinking that maybe something like this would solve the problem:
foo = foo % max;
if (foo <0)
foo+=max;
But % is only for integers and including a library just to get fmod feels overkill.
Anyway, I'm wondering if this would work, if it could be done with less code and if it could be done without fmod.