Issue-#151 Extending the domain of Math.ArcTan2

Merged to the master branch

Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Thu Mar 09, 2017 7:46 pm

The proposal is to allow ArcTan2 to allow (0., 0.) as a valid input, and then return 0 (see https://redmine.blackboxframework.org/issues/151).

In applications (for example dealing with complex numbers) the pair (0., 0.) may be quite rare, but it is often inconvenient to guarantee that it will never occur. Hence I find that ArcTan2 is currently almost unuseable without calling it from within a "wrapper" function that tests for this case. This is ugly, inconvenient, and inefficient.

A far simpler solution is to simply remove the line
Code: Select all
ASSERT((y # 0)  OR (x # 0), 20);
from the current function. This will not affect (apart from a minor speed-up) any existing programs that do not currently TRAP.

Some comments that you might consider relevant:
1 - MATLAB allows (0., 0.), and returns 0.
2 - The alternative code at the bottom of the Math file allows (0., 0.), and returns 0.

I also propose removing the tests for INF, and thus further extending this function's domain. I accept that this situation probably occurs even less frequently, but removing these tests does no harm I can see, speeds up the function even more, and makes it align better with the IEEE 754 arithmetic standard.

One further comment about the alternative code: it returns the wrong sign when the y input = -0.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Josef Templ » Fri Mar 10, 2017 9:30 am

I like this proposal.

> One further comment about the alternative code: it returns the wrong sign when the y input = -0.

Is it possible to extend/change the alternative code such that it returns -0?

Please don't forget to also update the docu.

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Fri Mar 10, 2017 9:38 am

Josef Templ wrote:Is it possible to extend/change the alternative code such that it returns -0?
Please don't forget to also update the docu.

I hope to publish some diffs today, including the changes to Docus & Strings.

Regarding the alternative code ... Of course it is possible. But my position, as you know, is that it is better removed. I don't see the benefit in spending time working on it, and updating it gives the impression it has some merit. I don't want to give that impression without carefully checking all of it, and I don't want to do that.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Fri Mar 10, 2017 11:58 am

There are some preliminary diffs here https://redmine.blackboxframework.org/p ... 09ddc6ad20.

Two points:
1 - I have not yet updated the Docu Post conditions. Interesting cases for the inputs x & y are ±0., ±INF, & ± some finite number such as 1. This is 32 combinations (if you omit simple cases such as (+1., +1.)). Do we want to include all these, or none, or what subset?

2 - I have not tried to align the alternative code. Testing for -0, using only high level constructs, requires some expression such as
Code: Select all
xIsNegZero := 1. / x = -INF
By the time you have checked for all possibilities the code would be a monster.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Josef Templ » Fri Mar 10, 2017 12:03 pm

A simple and pragmatic solution would be to add a comment to the alternative version.
For example:
"(* note: special cases such as (0, 0) and (INF, INF) are not covered here *)"

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Fri Mar 10, 2017 12:57 pm

Some test results below.
In this printout "~0." means -0. exactly, and the NaNs are returned by a hacked version of the "Alternative"; the real version TRAPs without an explicit ASSERT for these four cases.

I am tempted to put the full table (first 3 columns only) into the Docu as post conditions.

Code: Select all
   x     y     Math.ArcTan2  "Alternative"
                × 180 / π      × 180 / π
  −∞    −∞         −135.         NaN     
  −∞    −1.        −180.          −180.   
  −∞    ~0.        −180.           180.   
  −∞     0.         180.           180.   
  −∞     1.         180.           180.   
  −∞     ∞          135.         NaN     

  −1.   −∞          −90.           −90.   
  −1.   −1.        −135.          −135.   
  −1.   ~0.        −180.           180.   
  −1.    0.         180.           180.   
  −1.    1.         135.           135.   
  −1.    ∞           90.            90.   

  ~0.   −∞          −90.           −90.   
  ~0.   −1.         −90.           −90.   
  ~0.   ~0.        −180.             0.   
  ~0.    0.         180.             0.   
  ~0.    1.          90.            90.   
  ~0.    ∞           90.            90.   

   0.   −∞          −90.           −90.   
   0.   −1.         −90.           −90.   
   0.   ~0.          ~0.             0.   
   0.    0.           0.             0.   
   0.    1.          90.            90.   
   0.    ∞           90.            90.   

   1.   −∞          −90.           −90.   
   1.   −1.         −45.           −45.   
   1.   ~0.          ~0.             0.   
   1.    0.           0.             0.   
   1.    1.          45.            45.   
   1.    ∞           90.            90.   

   ∞    −∞          −45.         NaN     
   ∞    −1.          ~0.            ~0.   
   ∞    ~0.          ~0.             0.   
   ∞     0.           0.             0.   
   ∞     1.           0.             0.   
   ∞     ∞           45.         NaN     
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Josef Templ » Fri Mar 10, 2017 1:53 pm

If the results are the same as those for some IEEE standard
it would be sufficient to refer to the standard.
Is atan2 covered by the IEEE standard?

The GO library defines it nicely. I guess it uses the same semantics.
This would be trivial to adapt.
Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +Pi
Atan2(-0, x<=-0) = -Pi
Atan2(y>0, 0) = +Pi/2
Atan2(y<0, 0) = -Pi/2
Atan2(+Inf, +Inf) = +Pi/4
Atan2(-Inf, +Inf) = -Pi/4
Atan2(+Inf, -Inf) = 3Pi/4
Atan2(-Inf, -Inf) = -3Pi/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +Pi
Atan2(y<0, -Inf) = -Pi
Atan2(+Inf, x) = +Pi/2
Atan2(-Inf, x) = -Pi/2

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Fri Mar 10, 2017 5:48 pm

Josef Templ wrote:If the results are the same as those for some IEEE standard
it would be sufficient to refer to the standard.
Is atan2 covered by the IEEE standard?
Many years ago I was quite familiar with the standard, and atan2 was not covered.
However I believe there is a newer version that does cover these functions.

Refering to the standard may be satisfactory is principle, but I guess most users will not be familiar with it, and so including a table of results would be helpful. I also believe that the standard is really quite expensive to purchase; the last time I looked it was not available for free.

Also the GO description does invite errors. Consider the two lines:
Code: Select all
Atan2(+0, x>=0) = +0
Atan2(+0, x<=-0) = +Pi

If you have x & y values that return TRUE for "x >= 0." and you know that y is +0, what result do you expect?
Answer: it could be either.

Putting in a table of results simply extends the format Oms already uses.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Robert » Fri Mar 10, 2017 7:38 pm

This is my suggestion for this issue. Diff: https://redmine.blackboxframework.org/p ... 09ddc6ad20.

I have included an illustrative results table, and added a comment to the alternative functions.
User avatar
Robert
 
Posts: 1023
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Issue-#151 Extending the domain of Math.ArcTan2

Postby Josef Templ » Sat Mar 11, 2017 7:55 am

Robert wrote:This is my suggestion for this issue. Diff: https://redmine.blackboxframework.org/p ... 09ddc6ad20.

I have included an illustrative results table, and added a comment to the alternative functions.


The problem with your table is that it defines only examples.
It is not complete. At the same time it is very long.

Robert wrote:If you have x & y values that return TRUE for "x >= 0." and you know that y is +0, what result do you expect?
Answer: it could be either.


What is the ambiguity in the GO definition?
Isn't it clear what the intention is?
The <= operator could be turned into a ≤ Unicode character
to make it clear that this is not the CP <= operator.

ArcTan2(+0, x ≥ 0) = +0
ArcTan2(+0, x ≤ -0) = +Pi

- Josef
User avatar
Josef Templ
 
Posts: 2038
Joined: Tue Sep 17, 2013 6:50 am

Next

Return to Resolved (Features)

Who is online

Users browsing this forum: No registered users and 3 guests

cron