The difficulty was to make it work for arbitrary values (expecially large ones), the solution proposed here is to "cut" the amount of unite circle made in excess:
float change_length(float number) {
float two_pi = 6.283185;
// If the input number is greater than 2*pi :
// we will have to cut to the number of excess circles.
// Count the number of revolutions made by the number:
float number_circle = number / two_pi;
// round down the number of circles made (put in an int)
// and as this is the number of circles made
// we have to remember it with "* two_pi"
float number_circle_rounded_down = (int)number_circle * two_pi;
// Cut the number of excess circles
// if the input number is less than 2*pi
// it will just do : number -= 0.0;
number -= number_circle_rounded_down;
return number;
}
The code is in radians, but it can also work in degrees by replacing 2
float change_length(float number) {
//float two_pi = 6.283185;
float two_pi = 360;
float number_circle = number / two_pi;
float number_circle_rounded_down = (int)number_circle * two_pi;
number -= number_circle_rounded_down;
return number;
}
Here how I find it :
With a better representation to work with because of the periodicity :
Here are one of the few esample to demonstrate it (the others are in the exemples directory):
We can find
in the code :
number -= number_circle_rounded_down;
But We have still to find
that makes :
inthe code :
float change_length(float number) {
float two_pi = 6.283185;
float number_circle = number / two_pi;
float number_circle_rounded_down = (int)number_circle * two_pi;
number -= number_circle_rounded_down;
return number;
}