Browse Source

* fixed and improved generic ArcTan2 implementation by Paolo Valle, resolves #39861

(cherry picked from commit f18d6f1c852579643b1d7c07d991242d3031c436)
florian 3 years ago
parent
commit
4e32172df0
1 changed files with 16 additions and 12 deletions
  1. 16 12
      rtl/objpas/math.pp

+ 16 - 12
rtl/objpas/math.pp

@@ -906,22 +906,26 @@ end;
 {$ifndef FPC_MATH_HAS_ARCTAN2}
 {$ifndef FPC_MATH_HAS_ARCTAN2}
 function arctan2(y,x : float) : float;
 function arctan2(y,x : float) : float;
   begin
   begin
-    if (x=0) then
+    if x=0 then
       begin
       begin
         if y=0 then
         if y=0 then
-          arctan2:=0.0
+          result:=0.0
         else if y>0 then
         else if y>0 then
-          arctan2:=pi/2
-        else if y<0 then
-          arctan2:=-pi/2;
+          result:=pi/2
+        else
+          result:=-pi/2;
       end
       end
-    else
-      ArcTan2:=ArcTan(y/x);
-    if x<0.0 then
-      ArcTan2:=ArcTan2+pi;
-    if ArcTan2>pi then
-      ArcTan2:=ArcTan2-2*pi;
-  end;
+    else 
+      begin
+        if X > 0 then
+          result:=ArcTan(y/x)
+        else
+          if Y < 0.0 then
+            result:=ArcTan(y/x)-pi
+          else          
+            result:=ArcTan(y/x)+pi;
+      end;              
+  end;  
 {$endif FPC_MATH_HAS_ARCTAN2}
 {$endif FPC_MATH_HAS_ARCTAN2}