Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to normalize angle radian value to 2*pi?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If I properly understood the intent, what you need is to add/subtract the integer number of 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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading