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