xref: /netbsd-src/lib/libm/src/e_sinh.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /* @(#)e_sinh.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 #ifndef lint
14 static char rcsid[] = "$Id: e_sinh.c,v 1.3 1994/02/18 02:25:49 jtc Exp $";
15 #endif
16 
17 /* __ieee754_sinh(x)
18  * Method :
19  * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
20  *	1. Replace x by |x| (sinh(-x) = -sinh(x)).
21  *	2.
22  *		                                    E + E/(E+1)
23  *	    0        <= x <= 22     :  sinh(x) := --------------, E=expm1(x)
24  *			       			        2
25  *
26  *	    22       <= x <= lnovft :  sinh(x) := exp(x)/2
27  *	    lnovft   <= x <= ln2ovft:  sinh(x) := exp(x/2)/2 * exp(x/2)
28  *	    ln2ovft  <  x	    :  sinh(x) := x*shuge (overflow)
29  *
30  * Special cases:
31  *	sinh(x) is |x| if x is +INF, -INF, or NaN.
32  *	only sinh(0)=0 is exact for finite x.
33  */
34 
35 #include <math.h>
36 
37 #ifdef __STDC__
38 static const double one = 1.0, shuge = 1.0e307;
39 #else
40 static double one = 1.0, shuge = 1.0e307;
41 #endif
42 
43 #ifdef __STDC__
44 	double __ieee754_sinh(double x)
45 #else
46 	double __ieee754_sinh(x)
47 	double x;
48 #endif
49 {
50 	double t,w,h;
51 	int ix,jx;
52 	unsigned lx;
53 
54     /* High word of |x|. */
55 	jx = *( (((*(int*)&one)>>29)^1) + (int*)&x);
56 	ix = jx&0x7fffffff;
57 
58     /* x is INF or NaN */
59 	if(ix>=0x7ff00000) return x+x;
60 
61 	h = 0.5;
62 	if (jx<0) h = -h;
63     /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */
64 	if (ix < 0x40360000) {		/* |x|<22 */
65 	    if (ix<0x3e300000) 		/* |x|<2**-28 */
66 		if(shuge+x>one) return x;/* sinh(tiny) = tiny with inexact */
67 	    t = expm1(fabs(x));
68 	    if(ix<0x3ff00000) return h*(2.0*t-t*t/(t+one));
69 	    return h*(t+t/(t+one));
70 	}
71 
72     /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */
73 	if (ix < 0x40862E42)  return h*__ieee754_exp(fabs(x));
74 
75     /* |x| in [log(maxdouble), overflowthresold] */
76 	lx = *( (((*(unsigned*)&one)>>29)) + (unsigned*)&x);
77 	if (ix<0x408633CE || (ix==0x408633ce)&&(lx<=(unsigned)0x8fb9f87d)) {
78 	    w = __ieee754_exp(0.5*fabs(x));
79 	    t = h*w;
80 	    return t*w;
81 	}
82 
83     /* |x| > overflowthresold, sinh(x) overflow */
84 	return x*shuge;
85 }
86