From e90e91d466888ae39d2b5e70841e1792dbb6dded Mon Sep 17 00:00:00 2001
From: Raymond Toy <toy.raymond@gmail.com>
Date: Wed, 23 Dec 2015 19:43:17 -0800
Subject: [PATCH] Use setexception to raise the inexact exception for sinh.

---
 src/lisp/e_sinh.c | 10 ++++++++--
 tests/fdlibm.lisp | 15 +++++++++++++--
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/lisp/e_sinh.c b/src/lisp/e_sinh.c
index 9d7aec89a..80b7a62a9 100644
--- a/src/lisp/e_sinh.c
+++ b/src/lisp/e_sinh.c
@@ -67,8 +67,14 @@ static double one = 1.0, shuge = 1.0e307;
 	if (jx<0) h = -h;
     /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */
 	if (ix < 0x40360000) {		/* |x|<22 */
-	    if (ix<0x3e300000) 		/* |x|<2**-28 */
-		if(shuge+x>one) return x;/* sinh(tiny) = tiny with inexact */
+	    if (ix<0x3e300000) {		/* |x|<2**-28 */
+		/* sinh(tiny) = tiny with inexact */
+		if (x != 0) {
+		    fdlibm_setexception(x, FDLIBM_INEXACT);
+		}
+
+		return x;		
+	    }
 	    t = fdlibm_expm1(fabs(x));
 	    if(ix<0x3ff00000) return h*(2.0*t-t*t/(t+one));
 	    return h*(t+t/(t+one));
diff --git a/tests/fdlibm.lisp b/tests/fdlibm.lisp
index 700b64e2d..9ca9d3001 100644
--- a/tests/fdlibm.lisp
+++ b/tests/fdlibm.lisp
@@ -68,8 +68,19 @@
 		  (kernel:%sinh ext:double-float-negative-infinity)))
   ;; Test NaN
   (kernel::with-float-traps-masked (:invalid)
-    (assert-true (ext:float-nan-p (kernel:%sinh *qnan*)))))
-
+    (assert-true (ext:float-nan-p (kernel:%sinh *qnan*))))
+  ;; sinh(x) = x for |x| < 2^-28.  Should signal inexact unless x = 0.
+  (let ((x (scale-float 1d0 -29))
+	(x0 0d0))
+    (with-inexact-exception-enabled
+	;; This must not throw an inexact exception because the result
+	;; is exact when the arg is 0.
+	(assert-eql 0d0 (kernel:%sinh x0)))
+    (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:%sinh x)))))
 
 (define-test %tanh.exceptions
   (:tag :fdlibm)
-- 
GitLab