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