# HP Forums

Full Version: ArcTan(x,y)?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Is there a single function on the Prime to compute ArcTan(x,y)? I know the function below returns the correct values for the different quadrants but I'd like to use a built-in function if possible. I cant find anything on it in the manual.

Tom L

PHP Code:
`arctanxy(x, y)begin  local a;  if x == 0 then    if y == 0 then      a := 0;    else      a := sign(y) * pi / 2    end;  else    a := atan(y / x);    if x < 0 then      if y < 0 then        a := a - pi;      else        a := a + pi;      end;    end;  end;  return a;end; `
(04-05-2017 12:07 PM)toml_12953 Wrote: [ -> ]Is there a single function on the Prime to compute ArcTan(x,y)?

ARG((x,y)) = ARG(x+y*i) is like ATAN(y/x) but it respects the quadrants better than ATAN. Here's its description from the 50g AUR (same info applies to the Prime):

ARG
Type: Function
Description: Argument Function: Returns the (real) polar angle θ of a complex number (x, y).
The polar angle θ is equal to:
• atan y/x for x ≥ 0
• atan y/x + π sign y for x < 0, Radians mode
• atan y/x + 180 sign y for x < 0, Degrees mode
• atan y/x + 200 sign y for x < 0, Grads mode
A real argument x is treated as the complex argument (x,0).
(04-05-2017 12:20 PM)Joe Horn Wrote: [ -> ]
(04-05-2017 12:07 PM)toml_12953 Wrote: [ -> ]Is there a single function on the Prime to compute ArcTan(x,y)?

ARG((x,y)) = ARG(x+y*i) is like ATAN(y/x) but it respects the quadrants better than ATAN.

Wow! Thanks, Joe! You always answer so quickly, you must have a small parish to have this much time for the calculators! Tom L
(04-05-2017 12:26 PM)toml_12953 Wrote: [ -> ]
(04-05-2017 12:20 PM)Joe Horn Wrote: [ -> ]ARG((x,y)) = ARG(x+y*i) is like ATAN(y/x) but it respects the quadrants better than ATAN.

Wow! Thanks, Joe! You always answer so quickly, you must have a small parish to have this much time for the calculators! Tom L

However, unlike ATAN2, ARG will not always work

Code:
```  ATAN2_UNUSEDYX(YY,XX)   BEGIN //MY ATAN2   IF XX==0 AND YY==0 THEN    RETURN 0; //CONVENIENT;   END;   //-π..π    RETURN ARG(XX+*YY);//BUILT-IN HANDLES MOST // the heiroglyphic is imaginary i  END;```
(04-05-2017 03:00 PM)StephenG1CMZ Wrote: [ -> ]
(04-05-2017 12:26 PM)toml_12953 Wrote: [ -> ]Wow! Thanks, Joe! You always answer so quickly, you must have a small parish to have this much time for the calculators! Tom L

However, unlike ATAN2, ARG will not always work

Code:
``` ATAN2_UNUSEDYX(YY,XX)   BEGIN //MY ATAN2   IF XX==0 AND YY==0 THEN    RETURN 0; //CONVENIENT;   END;   //-π..π    RETURN ARG(XX+*YY);//BUILT-IN HANDLES MOST // the heiroglyphic is imaginary i  END;```

I don't understand what the first test is for. ARG((0,0)) returns 0 even without it.

Tom L
(04-05-2017 03:39 PM)toml_12953 Wrote: [ -> ]
(04-05-2017 03:00 PM)StephenG1CMZ Wrote: [ -> ]However, unlike ATAN2, ARG will not always work

Code:
``` ATAN2_UNUSEDYX(YY,XX)   BEGIN //MY ATAN2   IF XX==0 AND YY==0 THEN    RETURN 0; //CONVENIENT;   END;   //-π..π    RETURN ARG(XX+*YY);//BUILT-IN HANDLES MOST // the heiroglyphic is imaginary i  END;```

I don't understand what the first test is for. ARG((0,0)) returns 0 even without it.

Tom L

Mathematically, (0,0) is undefined, although in the interests of having the code not break mid-flight, atan2 implementations normally return 0.

On the 8151 Android emulator, ARG fails for 0,0 rather than returning 0...
Glad to see this has improved.

By the way, I notice there is an error in the Help for ARG (in the new version too):

The Syntax is described as ARG(3+3i) but this gives a syntax error.
Actually (3+3*i) is required.
(04-05-2017 06:22 PM)StephenG1CMZ Wrote: [ -> ]The Syntax is described as ARG(3+3i) but this gives a syntax error.
Actually (3+3*i) is required.
Not on my Prime, it works fine. Guess I have a special one.
(04-05-2017 06:22 PM)StephenG1CMZ Wrote: [ -> ]Mathematically, (0,0) is undefined, although in the interests of having the code not break mid-flight, atan2 implementations normally return 0.

While it might return 0 - I don't have a compiler handy to check it out - H&S say that for atan2(0,0) "a domain error occurs".

i.e., it might return 0, but set errno to EDOM.
(04-05-2017 07:28 PM)KeithB Wrote: [ -> ]
(04-05-2017 06:22 PM)StephenG1CMZ Wrote: [ -> ]The Syntax is described as ARG(3+3i) but this gives a syntax error.
Actually (3+3*i) is required.
Not on my Prime, it works fine. Guess I have a special one.

Unless I made a typo, both 8151 and the new Android app required 3+3*i rather than 3+3i.
(04-05-2017 07:33 PM)KeithB Wrote: [ -> ]
(04-05-2017 06:22 PM)StephenG1CMZ Wrote: [ -> ]Mathematically, (0,0) is undefined, although in the interests of having the code not break mid-flight, atan2 implementations normally return 0.

While it might return 0 - I don't have a compiler handy to check it out - H&S say that for atan2(0,0) "a domain error occurs".

i.e., it might return 0, but set errno to EDOM.

H&S?
Health&Safety?
I'd prefer ATAN2 to deliver what it is supposed to - an ANGLE.
Then you know what angle to move your flaps to.
Having it return a Domain Error is not what I want as a passenger (though it might be useful whilst developing on the ground).
Harbison and Steele.

Except if x and y are 0, what angle should you return?
As a general comment on atan2 / ArcTan and floating-point: this is one of the places where signed zeroes can light a clear path. (For those less familiar with the numerical details of the prime: some of the number systems used in the Prime have signed zeroes, some do not.)

Signed zeroes and atan2 / ArcTan can also surprise some programmers (depending on their numerical outlook).
(04-05-2017 08:10 PM)KeithB Wrote: [ -> ]Harbison and Steele.

Except if x and y are 0, what angle should you return?

Judging by the reviews, H&S looks like a useful update of K&R.

Most of my programming has been functional, with the emphasis on returning real-world values.
I'd be looking for:
(A) Values not significantly different from atan2(0 .000001,0.00001) etc., assuming the 0 is a rounding error - no discontinuous jumps.
(B) Values that are portable and compatible. Every atan2 that I have seen since the 1980's has used 0, so I would need some convincing that a different incompatible value were useful.
(C) For real-world use: In the case of Atan2, I don't see 0,0 as being a real domain error, assuming the 0,0 was a rounding error from valid inputs like 0.001,0.001...what the pilot wants to see is an angle... I'd keep domain error for truly exceptional error conditions, like if the input (0,0) were instead (NaN,NaN) because the compass had dropped off, and you really cannot return an angle.
(D) But in maths class, I would like to see it highlighted as indeterminate.
Note that EDOM is a "back channel error", this is not the return from the function, but a value in a separate errno variable. I believe the function still returns 0.

ETA:
atan2(0.0001, 0.0001) != 0.
(04-05-2017 09:42 PM)KeithB Wrote: [ -> ]Note that EDOM is a "back channel error", this is not the return from the function, but a value in a separate errno variable. I believe the function still returns 0.

ETA:
atan2(0.0001, 0.0001) != 0.

Oh...
I should have checked that example instead of assuming it would be near 0.
So if the limit of atan2(x,x) as x approaches 0 is 45, maybe that is what atan2() should return?
(04-05-2017 07:28 PM)KeithB Wrote: [ -> ]
(04-05-2017 06:22 PM)StephenG1CMZ Wrote: [ -> ]The Syntax is described as ARG(3+3i) but this gives a syntax error.
Actually (3+3*i) is required.
Not on my Prime, it works fine. Guess I have a special one.

I think I just stumbled across the problem you had:
arg(1+i1) is OK
arg(.1+i.1) is a syntax error
arg(0.1+i0.1) is OK
arg(.1+.1i) is OK

So you get the error when the decimal point is adjacent to i.
(04-05-2017 10:47 PM)KeithB Wrote: [ -> ]I think I just stumbled across the problem you had:
arg(1+i1) is OK
arg(.1+i.1) is a syntax error
arg(0.1+i0.1) is OK
arg(.1+.1i) is OK

So you get the error when the decimal point is adjacent to i.

Implicit multiplication isn't supported:

arg(.1+i.1) is a syntax error
arg(.1+i*.1) ==> returns ARG(0.1+i*0.1); 0.79
implicit multiplication seems to work just fine for:
arg(1+i1) is OK
arg(0.1+i0.1) is OK
arg(.1+.1i) is OK

It seems to be the decimal point that causes the Prime to be confused
(04-06-2017 10:27 AM)DrD Wrote: [ -> ]
(04-05-2017 10:47 PM)KeithB Wrote: [ -> ]I think I just stumbled across the problem you had:
arg(1+i1) is OK
arg(.1+i.1) is a syntax error
arg(0.1+i0.1) is OK
arg(.1+.1i) is OK

So you get the error when the decimal point is adjacent to i.

Implicit multiplication isn't supported:

arg(.1+i.1) is a syntax error
arg(.1+i*.1) ==> returns ARG(0.1+i*0.1); 0.79

It seems to work for some, and is specified in the ARG HELP - but ARG implicit multiplication doesnt work for me on the Android, regardless of decimal point.
Pages: 1 2
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :