Post Reply 
ArcTan(x,y)?
04-05-2017, 12:07 PM
Post: #1
ArcTan(x,y)?
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(xy)
begin
  local a
;
  if 
== 0 then
    
if == 0 then
      a 
:= 0;
    else
      
:= sign(y) * pi 2
    end
;
  else
    
:= atan(x);
    if 
0 then
      
if 0 then
        a 
:= pi;
      else
        
:= pi;
      
end;
    
end;
  
end;

  return 
a;

end

Tom L
My wife's judgement is much better than mine.
Look who we each married!
Find all posts by this user
Quote this message in a reply
04-05-2017, 12:20 PM
Post: #2
RE: ArcTan(x,y)?
(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).

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 12:26 PM
Post: #3
RE: ArcTan(x,y)?
(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! Smile

Tom L

Tom L
My wife's judgement is much better than mine.
Look who we each married!
Find all posts by this user
Quote this message in a reply
04-05-2017, 03:00 PM (This post was last modified: 04-05-2017 03:08 PM by StephenG1CMZ.)
Post: #4
RE: ArcTan(x,y)?
(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! Smile

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;

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 03:39 PM (This post was last modified: 04-05-2017 03:42 PM by toml_12953.)
Post: #5
RE: ArcTan(x,y)?
(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! Smile

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

Tom L
My wife's judgement is much better than mine.
Look who we each married!
Find all posts by this user
Quote this message in a reply
04-05-2017, 06:22 PM (This post was last modified: 04-05-2017 06:42 PM by StephenG1CMZ.)
Post: #6
RE: ArcTan(x,y)?
(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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 07:28 PM
Post: #7
RE: ArcTan(x,y)?
(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.
Find all posts by this user
Quote this message in a reply
04-05-2017, 07:33 PM
Post: #8
RE: ArcTan(x,y)?
(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.
Find all posts by this user
Quote this message in a reply
04-05-2017, 07:47 PM
Post: #9
RE: ArcTan(x,y)?
(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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 07:53 PM (This post was last modified: 04-05-2017 07:58 PM by StephenG1CMZ.)
Post: #10
RE: ArcTan(x,y)?
(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).

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 08:10 PM
Post: #11
RE: ArcTan(x,y)?
Harbison and Steele.

Except if x and y are 0, what angle should you return?
Find all posts by this user
Quote this message in a reply
04-05-2017, 09:08 PM
Post: #12
RE: ArcTan(x,y)?
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).
Find all posts by this user
Quote this message in a reply
04-05-2017, 09:17 PM (This post was last modified: 04-05-2017 09:29 PM by StephenG1CMZ.)
Post: #13
RE: ArcTan(x,y)?
(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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 09:42 PM (This post was last modified: 04-05-2017 09:43 PM by KeithB.)
Post: #14
RE: ArcTan(x,y)?
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.
Find all posts by this user
Quote this message in a reply
04-05-2017, 10:38 PM
Post: #15
RE: ArcTan(x,y)?
(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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2017, 10:41 PM
Post: #16
RE: ArcTan(x,y)?
So if the limit of atan2(x,x) as x approaches 0 is 45, maybe that is what atan2() should return?
Find all posts by this user
Quote this message in a reply
04-05-2017, 10:47 PM
Post: #17
RE: ArcTan(x,y)?
(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.
Find all posts by this user
Quote this message in a reply
04-06-2017, 10:27 AM
Post: #18
RE: ArcTan(x,y)?
(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
Find all posts by this user
Quote this message in a reply
04-06-2017, 02:38 PM
Post: #19
RE: ArcTan(x,y)?
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
Find all posts by this user
Quote this message in a reply
04-06-2017, 03:22 PM
Post: #20
RE: ArcTan(x,y)?
(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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)