Skip to content
Advertisement

Can You Find Something Mod 2Pi in JavaScript?

EDIT: I have an angle from using Math.atan2() which I then want to add or subtract values from. However, this addition and subtraction sometimes means the angle is greater that pi or less than -pi and I’m looking for a way to get one of these outside angles back into the correct range.

I’m trying to find a value mod 2pi in JavaScipt using the following code:

JavaScript

However, it always just returns foo, even if I try this:

JavaScript

Can anyone explain to me why this doesn’t work?

DLiKS

Advertisement

Answer

You aren’t making an assignment:

JavaScript

Or:

JavaScript

EDIT:

To paraphrase your updated question, you have a value foo, which may be any angle, however you want foo to be in the range [-pi,pi]. You need to do this programatically:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement