Browse Source

* avoid overflow for tanh(<extended>) as well, was an oversight

florian 3 weeks ago
parent
commit
c177cf7da2
1 changed files with 20 additions and 12 deletions
  1. 20 12
      rtl/objpas/math.pp

+ 20 - 12
rtl/objpas/math.pp

@@ -1475,18 +1475,26 @@ function tanh(x : Extended) : Extended;
   var
     tmp:Extended;
   begin
-    if x < 0 then begin
-      tmp:=exp(2*x);
-      if tmp=1 then
-        exit(x);
-      result:=(tmp-1)/(1+tmp)
-    end
-    else begin
-      tmp:=exp(-2*x);
-      if tmp=1 then
-        exit(x);
-      result:=(1-tmp)/(1+tmp)
-    end;
+    if abs(x)>25 then
+      begin
+        result:=sign(x);
+        exit;
+      end;
+
+    if x < 0 then
+      begin
+        tmp:=exp(2*x);
+        if tmp=1 then
+          exit(x);
+        result:=(tmp-1)/(1+tmp)
+      end
+    else
+      begin
+        tmp:=exp(-2*x);
+        if tmp=1 then
+          exit(x);
+        result:=(1-tmp)/(1+tmp)
+      end;
   end;
 {$ENDIF}