Browse Source

* fixed Math.Tanh as proposed by Paolo Valle, resolves #39867

florian 3 years ago
parent
commit
9e14dee1c3
2 changed files with 21 additions and 7 deletions
  1. 10 7
      rtl/objpas/math.pp
  2. 11 0
      tests/webtbs/tw39867.pp

+ 10 - 7
rtl/objpas/math.pp

@@ -945,15 +945,18 @@ function sinh(x : float) : float;
      sinh:=copysign(0.5*(temp-1.0/temp),x);
      sinh:=copysign(0.5*(temp-1.0/temp),x);
   end;
   end;
 
 
-Const MaxTanh = 5678.22249441322; // Ln(MaxExtended)/2
-
 function tanh(x : float) : float;
 function tanh(x : float) : float;
-  var Temp : float;
+  var
+    tmp:float;
   begin
   begin
-     if x>MaxTanh then exit(1.0)
-     else if x<-MaxTanh then exit (-1.0);
-     temp:=exp(-2*x);
-     tanh:=(1-temp)/(1+temp)
+    if x < 0 then begin
+      tmp:=exp(2*x); 
+      result:=(tmp-1)/(1+tmp)
+    end
+    else begin
+      tmp:=exp(-2*x);
+      result:=(1-tmp)/(1+tmp)
+    end;
   end;
   end;
 
 
 function arccosh(x : float) : float; inline;
 function arccosh(x : float) : float; inline;

+ 11 - 0
tests/webtbs/tw39867.pp

@@ -0,0 +1,11 @@
+uses 
+  math;
+  
+begin
+  writeln(tanh(-354));
+  if tanh(-354)<>-1 then
+    halt(1);
+  writeln(tanh(-355));
+  if tanh(-355)<>-1 then
+    halt(1);
+end.