xref: /csrg-svn/lib/libm/common_source/sinh.c (revision 34126)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * All recipients should regard themselves as participants in an ongoing
13  * research project and hence should feel obligated to report their
14  * experiences (good or bad) with these elementary function codes, using
15  * the sendbug(8) program, to the authors.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)sinh.c	5.2 (Berkeley) 04/29/88";
20 #endif /* not lint */
21 
22 /* SINH(X)
23  * RETURN THE HYPERBOLIC SINE OF X
24  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
25  * CODED IN C BY K.C. NG, 1/8/85;
26  * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85.
27  *
28  * Required system supported functions :
29  *	copysign(x,y)
30  *	scalb(x,N)
31  *
32  * Required kernel functions:
33  *	expm1(x)	...return exp(x)-1
34  *
35  * Method :
36  *	1. reduce x to non-negative by sinh(-x) = - sinh(x).
37  *	2.
38  *
39  *	                                      expm1(x) + expm1(x)/(expm1(x)+1)
40  *	    0 <= x <= lnovfl     : sinh(x) := --------------------------------
41  *			       		                      2
42  *     lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow)
43  * lnovfl+ln2 <  x <  INF        :  overflow to INF
44  *
45  *
46  * Special cases:
47  *	sinh(x) is x if x is +INF, -INF, or NaN.
48  *	only sinh(0)=0 is exact for finite argument.
49  *
50  * Accuracy:
51  *	sinh(x) returns the exact hyperbolic sine of x nearly rounded. In
52  *	a test run with 1,024,000 random arguments on a VAX, the maximum
53  *	observed error was 1.93 ulps (units in the last place).
54  *
55  * Constants:
56  * The hexadecimal values are the intended ones for the following constants.
57  * The decimal values may be used, provided that the compiler will convert
58  * from decimal to binary accurately enough to produce the hexadecimal values
59  * shown.
60  */
61 #if defined(vax)||defined(tahoe)
62 #ifdef vax
63 #define _0x(A,B)	0x/**/A/**/B
64 #else	/* vax */
65 #define _0x(A,B)	0x/**/B/**/A
66 #endif	/* vax */
67 /* static double */
68 /* mln2hi =  8.8029691931113054792E1     , Hex  2^  7   *  .B00F33C7E22BDB */
69 /* mln2lo = -4.9650192275318476525E-16   , Hex  2^-50   * -.8F1B60279E582A */
70 /* lnovfl =  8.8029691931113053016E1     ; Hex  2^  7   *  .B00F33C7E22BDA */
71 static long    mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)};
72 static long    mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)};
73 static long    lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)};
74 #define   mln2hi    (*(double*)mln2hix)
75 #define   mln2lo    (*(double*)mln2lox)
76 #define   lnovfl    (*(double*)lnovflx)
77 #else	/* defined(vax)||defined(tahoe) */
78 static double
79 mln2hi =  7.0978271289338397310E2     , /*Hex  2^ 10   *  1.62E42FEFA39EF */
80 mln2lo =  2.3747039373786107478E-14   , /*Hex  2^-45   *  1.ABC9E3B39803F */
81 lnovfl =  7.0978271289338397310E2     ; /*Hex  2^  9   *  1.62E42FEFA39EF */
82 #endif	/* defined(vax)||defined(tahoe) */
83 
84 #if defined(vax)||defined(tahoe)
85 static max = 126                      ;
86 #else	/* defined(vax)||defined(tahoe) */
87 static max = 1023                     ;
88 #endif	/* defined(vax)||defined(tahoe) */
89 
90 
91 double sinh(x)
92 double x;
93 {
94 	static double  one=1.0, half=1.0/2.0 ;
95 	double expm1(), t, scalb(), copysign(), sign;
96 #if !defined(vax)&&!defined(tahoe)
97 	if(x!=x) return(x);	/* x is NaN */
98 #endif	/* !defined(vax)&&!defined(tahoe) */
99 	sign=copysign(one,x);
100 	x=copysign(x,one);
101 	if(x<lnovfl)
102 	    {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));}
103 
104 	else if(x <= lnovfl+0.7)
105 		/* subtract x by ln(2^(max+1)) and return 2^max*exp(x)
106 	    		to avoid unnecessary overflow */
107 	    return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign));
108 
109 	else  /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */
110 	    return( expm1(x)*sign );
111 }
112