0

So I saw the post asking how to convert from atan to atan2. But I haven't seen the reverse. So how would one convert from atan2 to atan? Any help is much appreciated!

I am actually extremely surprised that I could not find this question anywhere on stack overflow (my searching skills may be limited lol). I want to go this route since atan2 seems more numerically stable than atan (but I may be wrong).

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
yosmo78
  • 489
  • 4
  • 13
  • 1
    Can you provide the link to the other post? – AbdelAziz AbdelLatef Feb 19 '21 at 03:44
  • https://stackoverflow.com/questions/64463152/how-to-convert-from-atan-to-atan2 – yosmo78 Feb 19 '21 at 03:44
  • 1
    You want to implement `atan()` in terms of `atan2()`? It's just `atan(x) == atan2(x,1.0)`. (The other way around is what the other question explains.) But I can't think of any sense in which this would be "more numerically stable" than just calling `atan(x)` itself, and it's probably slower due to the redundant division. – Nate Eldredge Feb 19 '21 at 03:52
  • 2
    `atan2` is only useful if you have separate `x` and `y` coordinates. If all you have is `y/x`, then `atan2` doesn't do anything for you that `atan` doesn't already do. – user3386109 Feb 19 '21 at 03:55
  • 1
    This is a good point, do you have the inputs of form `x` and `y`, or just a single number? – AbdelAziz AbdelLatef Feb 19 '21 at 03:59
  • I have both numbers, but atan2 has the nice property that I can let x = 0, in terms of y/x (it handles the divide by zero case). So I was just wondering if I could just shift the output so that it is in terms of atan – yosmo78 Feb 19 '21 at 04:02

1 Answers1

1

You can do the opposite from what was done in this question.

double myatan(double y, double x)
{
    double pi = 3.14159265358979323846;
    if (x >= 0)
        return atan2(y, x);
    else if (y >= 0)
        return atan2(y, x) - pi;
    else
        return atan2(y, x) + pi;
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52