ArcTan2 update
Summary
current Arctan2 is implemented with some inefficiency.
System Information
- Operating system: windows 10 64bit
- Processor architecture: x86-64
- Device: Computer
Example Project
current implementation Arctan2 in unit math
function arctan2(y,x : float) : float;
begin
if (x=0) then
begin
if y=0 then
arctan2:=0.0
else if y>0 then
arctan2:=pi/2
else if y<0 then <--- 2: no need for this check
arctan2:=-pi/2;
end
else <--- 1:missed "begin"
ArcTan2:=ArcTan(y/x);
if x<0.0 then
ArcTan2:=ArcTan2+pi;
if ArcTan2>pi then <--- 3: it should avoid this sort of add and subtract
ArcTan2:=ArcTan2-2*pi;
<---- 1: missed "end"
end;
this can be corrected/improved in 3 points:
- it seems missed a couple "begin".."end". This causes, for example if input is X=Y=0, to go to the last two "if" even if the computation is already done.
- on the first branch Y is tested against =0 and then >0, so why test furether if Y < 0 ?
- the last part is such that for some input first is added "pi" then subtracted "2pi", that is not necessary (this also introduce some numerical noise in the final computation of arcant2)
once the above are corrected/modified as below some speed is also gained.
is should be:
function arctan2(y,x : float) : float;
begin
if (x=0) then
begin
if y=0 then
result:=0.0
else if y>0 then
result:=pi/2
else <--- 2: removed last if check
result:=-pi/2;
end
else begin <--- 1:added "begin"
if X > 0 then <--- 3: rewritten avoding unecessary sum+subtract
result:=ArcTan(y/x)
else
if Y < 0.0 then
result:=ArcTan(y/x)-pi
else
result:=ArcTan(y/x)+pi;
end; <---- 1: added "end"
end;
Relevant 3rd party information
Edited by Paolo Valle