I am currently performing some inverse trigonometric operations. The actual formula I am using is at the bottom of this post, but is not super important. Minimally, I am taking an arctangent atan2(x,y)
result in [-π,π)
. However, for reasons, I need the "equivalent" result on [0,2π)
, i.e. where -π
maps to π
and -π/2
maps to 3π/2
, etc. I can achieve this by taking the sign of the result, and if it is negative adding 2π
:
function fix_range(angle float) float :=
if angle < 0:
angle += 2π
return angle
...but I'd prefer to have a "math" solution to this. Is there a convenient mathematical formulation to adjust periodic ranges into a desired range? For example, is there a formula (without any logical control structures) so that:
function adjust_val(val float, min_range float, period float) := ?
So that adjust_val(-π/2, 0, 2π)
gives 3π/2
?
Common "math" operations allowed, for example anything available to numpy
or other math libraries in other languages.
Appendix:
Here's the formula I'm combatting with:
atan2(sin(lat_1)*sin(lon_1)*cos(lat_2)*cos(lon_2) - sin(lat_2)*sin(lon_1)*cos(lat_1) + sin(lon_2)*cos(lat_2)*cos(lon_1), sin(lat_1)*cos(lat_2)*cos(lon_1)*cos(lon_2) - sin(lat_2)*cos(lat_1)*cos(lon_1) - sin(lon_1)*sin(lon_2)*cos(lat_2)) - pi