I have to adjust the angle’s radian value between [0,2pi) if it is negative angle or too big angle than 2pi.
My code is as follows, and I haven’t found the related library:
def normalize(angle) do
rem(round(angle * 1000), round((2 * :math.pi * 1000))) / 1000
end
Is there a better way to improve it?
>Solution :
If I properly understood the intent, what you need is to add/subtract the integer number of 2πs to bring the angle to the interval. Use Kernel.floor/1 to calculate the number of full turnarounds. Luckily enough, it works for negative numbers out of the box, thanks to wise floor/1 implementation.
angle - 2 * :math.pi * floor(angle / (2 * :math.pi))