From b9e3a5113c1c08e5785fc36866a8e4553a96300e Mon Sep 17 00:00:00 2001
From: Raymond Toy <toy.raymond@gmail.com>
Date: Thu, 24 Dec 2015 09:29:08 -0800
Subject: [PATCH] Use setexception to raise the inexact exception for tanh.

Note that original code didn't actually signal inexact probably
because the compiler constant-folded one - tiny to one.
---
 src/lisp/s_tanh.c | 4 +++-
 tests/fdlibm.lisp | 9 ++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/lisp/s_tanh.c b/src/lisp/s_tanh.c
index 6b770043d..f9ea4d469 100644
--- a/src/lisp/s_tanh.c
+++ b/src/lisp/s_tanh.c
@@ -78,7 +78,9 @@ static double one=1.0, two=2.0, tiny = 1.0e-300;
 	    }
     /* |x| > 22, return +-1 */
 	} else {
-	    z = one - tiny;		/* raised inexact flag */
+	    /* Always raise inexact flag */
+	    fdlibm_setexception(x, FDLIBM_INEXACT);
+	    z = one - tiny;
 	}
 	return (jx>=0)? z: -z;
 }
diff --git a/tests/fdlibm.lisp b/tests/fdlibm.lisp
index 4a1087745..d820db54f 100644
--- a/tests/fdlibm.lisp
+++ b/tests/fdlibm.lisp
@@ -88,7 +88,14 @@
   (assert-error 'floating-point-invalid-operation
 		(kernel:%tanh *snan*))
   (kernel::with-float-traps-masked (:invalid)
-    (assert-true (ext:float-nan-p (kernel:%tanh *snan*)))))
+    (assert-true (ext:float-nan-p (kernel:%tanh *snan*))))
+  ;; tanh(x) = +/- 1 for |x| > 22, raising inexact, always.
+  (let ((x 22.1d0))
+    (with-inexact-exception-enabled
+	;; This must throw an inexact exception for non-zero x even
+	;; though the result is exactly x.
+	(assert-error 'floating-point-inexact
+		      (kernel:%tanh x)))))
 
 (define-test %acosh.exceptions
   (:tag :fdlibm)
-- 
GitLab