134126Sbostic /* 224606Szliu * Copyright (c) 1985 Regents of the University of California. 334126Sbostic * All rights reserved. 434126Sbostic * 534126Sbostic * Redistribution and use in source and binary forms are permitted 6*34931Sbostic * provided that the above copyright notice and this paragraph are 7*34931Sbostic * duplicated in all such forms and that any documentation, 8*34931Sbostic * advertising materials, and other materials related to such 9*34931Sbostic * distribution and use acknowledge that the software was developed 10*34931Sbostic * by the University of California, Berkeley. The name of the 11*34931Sbostic * University may not be used to endorse or promote products derived 12*34931Sbostic * from this software without specific prior written permission. 13*34931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634126Sbostic * 1734126Sbostic * All recipients should regard themselves as participants in an ongoing 1834126Sbostic * research project and hence should feel obligated to report their 1934126Sbostic * experiences (good or bad) with these elementary function codes, using 2034126Sbostic * the sendbug(8) program, to the authors. 2124606Szliu */ 2224606Szliu 2324606Szliu #ifndef lint 24*34931Sbostic static char sccsid[] = "@(#)sinh.c 5.3 (Berkeley) 06/30/88"; 2534126Sbostic #endif /* not lint */ 2624606Szliu 2724606Szliu /* SINH(X) 2824606Szliu * RETURN THE HYPERBOLIC SINE OF X 2924606Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 3024606Szliu * CODED IN C BY K.C. NG, 1/8/85; 3124606Szliu * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85. 3224606Szliu * 3324606Szliu * Required system supported functions : 3424606Szliu * copysign(x,y) 3524606Szliu * scalb(x,N) 3624606Szliu * 3724606Szliu * Required kernel functions: 3824606Szliu * expm1(x) ...return exp(x)-1 3924606Szliu * 4024606Szliu * Method : 4124606Szliu * 1. reduce x to non-negative by sinh(-x) = - sinh(x). 4224606Szliu * 2. 4324606Szliu * 4424606Szliu * expm1(x) + expm1(x)/(expm1(x)+1) 4524606Szliu * 0 <= x <= lnovfl : sinh(x) := -------------------------------- 4624606Szliu * 2 4724606Szliu * lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow) 4824606Szliu * lnovfl+ln2 < x < INF : overflow to INF 4924606Szliu * 5024606Szliu * 5124606Szliu * Special cases: 5224606Szliu * sinh(x) is x if x is +INF, -INF, or NaN. 5324606Szliu * only sinh(0)=0 is exact for finite argument. 5424606Szliu * 5524606Szliu * Accuracy: 5624606Szliu * sinh(x) returns the exact hyperbolic sine of x nearly rounded. In 5724606Szliu * a test run with 1,024,000 random arguments on a VAX, the maximum 5824606Szliu * observed error was 1.93 ulps (units in the last place). 5924606Szliu * 6024606Szliu * Constants: 6124606Szliu * The hexadecimal values are the intended ones for the following constants. 6224606Szliu * The decimal values may be used, provided that the compiler will convert 6324606Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6424606Szliu * shown. 6524606Szliu */ 6631853Szliu #if defined(vax)||defined(tahoe) 6731853Szliu #ifdef vax 6831812Szliu #define _0x(A,B) 0x/**/A/**/B 6931853Szliu #else /* vax */ 7031812Szliu #define _0x(A,B) 0x/**/B/**/A 7131853Szliu #endif /* vax */ 7226893Selefunt /* static double */ 7324606Szliu /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 7424606Szliu /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 7524606Szliu /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 7631812Szliu static long mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)}; 7731812Szliu static long mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)}; 7831812Szliu static long lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)}; 7924606Szliu #define mln2hi (*(double*)mln2hix) 8024606Szliu #define mln2lo (*(double*)mln2lox) 8124606Szliu #define lnovfl (*(double*)lnovflx) 8231853Szliu #else /* defined(vax)||defined(tahoe) */ 8326893Selefunt static double 8424606Szliu mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 8524606Szliu mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 8624606Szliu lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 8731853Szliu #endif /* defined(vax)||defined(tahoe) */ 8824606Szliu 8931853Szliu #if defined(vax)||defined(tahoe) 9024606Szliu static max = 126 ; 9131853Szliu #else /* defined(vax)||defined(tahoe) */ 9224606Szliu static max = 1023 ; 9331853Szliu #endif /* defined(vax)||defined(tahoe) */ 9424606Szliu 9524606Szliu 9624606Szliu double sinh(x) 9724606Szliu double x; 9824606Szliu { 9924606Szliu static double one=1.0, half=1.0/2.0 ; 10024606Szliu double expm1(), t, scalb(), copysign(), sign; 10131853Szliu #if !defined(vax)&&!defined(tahoe) 10224606Szliu if(x!=x) return(x); /* x is NaN */ 10331853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 10424606Szliu sign=copysign(one,x); 10524606Szliu x=copysign(x,one); 10624606Szliu if(x<lnovfl) 10724606Szliu {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));} 10824606Szliu 10924606Szliu else if(x <= lnovfl+0.7) 11024606Szliu /* subtract x by ln(2^(max+1)) and return 2^max*exp(x) 11124606Szliu to avoid unnecessary overflow */ 11224606Szliu return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign)); 11324606Szliu 11424606Szliu else /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */ 11524606Szliu return( expm1(x)*sign ); 11624606Szliu } 117