Browse Source

* patch by Rika: Improve Math.CotH, resolves #40084

florian 2 years ago
parent
commit
a260f1987c
1 changed files with 42 additions and 16 deletions
  1. 42 16
      rtl/objpas/math.pp

+ 42 - 16
rtl/objpas/math.pp

@@ -1314,32 +1314,58 @@ end;
 {$ifdef FPC_HAS_TYPE_SINGLE}
 function CotH(const X: Single): Single;
 var
-  Ex, Emx: ValReal;
-begin
-  //CotH = (e^X + e^-X) / (e^X - e^-X)
-  Ex:=Exp(X);
-  Emx:=1/Ex;
-  CotH:=(Ex+Emx)/(Ex-Emx);
+  e2: ValReal;
+begin
+  if x < 0 then begin
+    e2:=exp(2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(e2-1)
+  end
+  else begin
+    e2:=exp(-2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(1-e2)
+  end;
 end;
 {$ENDIF}
 {$ifdef FPC_HAS_TYPE_DOUBLE}
 function CotH(const X: Double): Double;
 var
-  Ex, Emx: ValReal;
-begin
-  Ex:=Exp(X);
-  Emx:=1/Ex;
-  CotH:=(Ex+Emx)/(Ex-Emx);
+  e2: ValReal;
+begin
+  if x < 0 then begin
+    e2:=exp(2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(e2-1)
+  end
+  else begin
+    e2:=exp(-2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(1-e2)
+  end;
 end;
 {$ENDIF}
 {$ifdef FPC_HAS_TYPE_EXTENDED}
 function CotH(const X: Extended): Extended;
 var
-  Ex, Emx: Extended;
-begin
-  Ex:=Exp(X);
-  Emx:=1/Ex;
-  CotH:=(Ex+Emx)/(Ex-Emx);
+  e2: Extended;
+begin
+  if x < 0 then begin
+    e2:=exp(2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(e2-1)
+  end
+  else begin
+    e2:=exp(-2*x);
+    if e2=1 then
+      exit(1/x);
+    result:=(1+e2)/(1-e2)
+  end;
 end;
 {$ENDIF}