xref: /netbsd/lib/libm/noieee_src/n_sinh.c (revision bf9ec67e)
1 /*      $NetBSD: n_sinh.c,v 1.4 1999/07/02 15:37:37 simonb Exp $ */
2 /*
3  * Copyright (c) 1985, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)sinh.c	8.1 (Berkeley) 6/4/93";
38 #endif
39 #endif /* not lint */
40 
41 /* SINH(X)
42  * RETURN THE HYPERBOLIC SINE OF X
43  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
44  * CODED IN C BY K.C. NG, 1/8/85;
45  * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85.
46  *
47  * Required system supported functions :
48  *	copysign(x,y)
49  *	scalb(x,N)
50  *
51  * Required kernel functions:
52  *	expm1(x)	...return exp(x)-1
53  *
54  * Method :
55  *	1. reduce x to non-negative by sinh(-x) = - sinh(x).
56  *	2.
57  *
58  *	                                      expm1(x) + expm1(x)/(expm1(x)+1)
59  *	    0 <= x <= lnovfl     : sinh(x) := --------------------------------
60  *			       		                      2
61  *     lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow)
62  * lnovfl+ln2 <  x <  INF        :  overflow to INF
63  *
64  *
65  * Special cases:
66  *	sinh(x) is x if x is +INF, -INF, or NaN.
67  *	only sinh(0)=0 is exact for finite argument.
68  *
69  * Accuracy:
70  *	sinh(x) returns the exact hyperbolic sine of x nearly rounded. In
71  *	a test run with 1,024,000 random arguments on a VAX, the maximum
72  *	observed error was 1.93 ulps (units in the last place).
73  *
74  * Constants:
75  * The hexadecimal values are the intended ones for the following constants.
76  * The decimal values may be used, provided that the compiler will convert
77  * from decimal to binary accurately enough to produce the hexadecimal values
78  * shown.
79  */
80 
81 #include "mathimpl.h"
82 
83 vc(mln2hi, 8.8029691931113054792E1   ,0f33,43b0,2bdb,c7e2,   7, .B00F33C7E22BDB)
84 vc(mln2lo,-4.9650192275318476525E-16 ,1b60,a70f,582a,279e, -50,-.8F1B60279E582A)
85 vc(lnovfl, 8.8029691931113053016E1   ,0f33,43b0,2bda,c7e2,   7, .B00F33C7E22BDA)
86 
87 ic(mln2hi, 7.0978271289338397310E2,    10, 1.62E42FEFA39EF)
88 ic(mln2lo, 2.3747039373786107478E-14, -45, 1.ABC9E3B39803F)
89 ic(lnovfl, 7.0978271289338397310E2,     9, 1.62E42FEFA39EF)
90 
91 #ifdef vccast
92 #define	mln2hi	vccast(mln2hi)
93 #define	mln2lo	vccast(mln2lo)
94 #define	lnovfl	vccast(lnovfl)
95 #endif
96 
97 #if defined(__vax__)||defined(tahoe)
98 static int max = 126                      ;
99 #else	/* defined(__vax__)||defined(tahoe) */
100 static int max = 1023                     ;
101 #endif	/* defined(__vax__)||defined(tahoe) */
102 
103 
104 double sinh(x)
105 double x;
106 {
107 	static const double  one=1.0, half=1.0/2.0 ;
108 	double t, sign;
109 #if !defined(__vax__)&&!defined(tahoe)
110 	if(x!=x) return(x);	/* x is NaN */
111 #endif	/* !defined(__vax__)&&!defined(tahoe) */
112 	sign=copysign(one,x);
113 	x=copysign(x,one);
114 	if(x<lnovfl)
115 	    {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));}
116 
117 	else if(x <= lnovfl+0.7)
118 		/* subtract x by ln(2^(max+1)) and return 2^max*exp(x)
119 	    		to avoid unnecessary overflow */
120 	    return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign));
121 
122 	else  /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */
123 	    return( expm1(x)*sign );
124 }
125