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