From cfbdf90ab02f618d6c23980b74ed3bea1ee70284 Mon Sep 17 00:00:00 2001 From: florian Date: Mon, 2 Jan 2023 18:55:23 +0100 Subject: [PATCH] * patch by Rika to optimize ArcCos, resolves #40078 --- rtl/objpas/math.pp | 24 +++--------------------- tests/test/units/math/tsincos.pp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/rtl/objpas/math.pp b/rtl/objpas/math.pp index 288a29c1d1..a0e7b8c394 100644 --- a/rtl/objpas/math.pp +++ b/rtl/objpas/math.pp @@ -1179,37 +1179,19 @@ end; {$ifdef FPC_HAS_TYPE_SINGLE} function Arccos(x : Single) : Single; begin - if abs(x)=1.0 then - if x<0.0 then - arccos:=Pi - else - arccos:=0 - else - arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); + arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); end; {$ENDIF} {$ifdef FPC_HAS_TYPE_DOUBLE} function Arccos(x : Double) : Double; begin - if abs(x)=1.0 then - if x<0.0 then - arccos:=Pi - else - arccos:=0 - else - arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); + arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); end; {$ENDIF} {$ifdef FPC_HAS_TYPE_EXTENDED} function Arccos(x : Extended) : Extended; begin - if abs(x)=1.0 then - if x<0.0 then - arccos:=Pi - else - arccos:=0 - else - arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); + arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x); end; {$ENDIF} diff --git a/tests/test/units/math/tsincos.pp b/tests/test/units/math/tsincos.pp index 660ea31a00..04efbadfd7 100644 --- a/tests/test/units/math/tsincos.pp +++ b/tests/test/units/math/tsincos.pp @@ -37,6 +37,26 @@ begin halt(11); if not(SameValue(e2,-1)) then halt(12); + + { ArcCos relies on ArcTan2 edge cases. } + s1:=arccos(single(1)); + if s1<>0 then + halt(13); + s1:=arccos(single(-1)); + if not SameValue(s1,single(pi)) then + halt(14); + d1:=arccos(double(1)); + if d1<>0 then + halt(15); + d1:=arccos(double(-1)); + if not SameValue(d1,double(pi)) then + halt(16); + e1:=arccos(extended(1)); + if e1<>0 then + halt(17); + e1:=arccos(extended(-1)); + if not SameValue(e1,extended(pi),1e-12) then + halt(18); writeln('ok'); end.