124606Szliu /* 224606Szliu * Copyright (c) 1985 Regents of the University of California. 324606Szliu * 424606Szliu * Use and reproduction of this software are granted in accordance with 524606Szliu * the terms and conditions specified in the Berkeley Software License 624606Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724606Szliu * source, and inclusion of this notice) with the additional understanding 824606Szliu * that all recipients should regard themselves as participants in an 924606Szliu * ongoing research project and hence should feel obligated to report 1024606Szliu * their experiences (good or bad) with these elementary function codes, 1124606Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224606Szliu */ 1324606Szliu 1424606Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31790Szliu "@(#)sinh.c 4.3 (Berkeley) 8/21/85; 1.4 (ucb.elefunt) 07/07/87"; 1724606Szliu #endif not lint 1824606Szliu 1924606Szliu /* SINH(X) 2024606Szliu * RETURN THE HYPERBOLIC SINE OF X 2124606Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224606Szliu * CODED IN C BY K.C. NG, 1/8/85; 2324606Szliu * REVISED BY K.C. NG on 2/8/85, 3/7/85, 3/24/85, 4/16/85. 2424606Szliu * 2524606Szliu * Required system supported functions : 2624606Szliu * copysign(x,y) 2724606Szliu * scalb(x,N) 2824606Szliu * 2924606Szliu * Required kernel functions: 3024606Szliu * expm1(x) ...return exp(x)-1 3124606Szliu * 3224606Szliu * Method : 3324606Szliu * 1. reduce x to non-negative by sinh(-x) = - sinh(x). 3424606Szliu * 2. 3524606Szliu * 3624606Szliu * expm1(x) + expm1(x)/(expm1(x)+1) 3724606Szliu * 0 <= x <= lnovfl : sinh(x) := -------------------------------- 3824606Szliu * 2 3924606Szliu * lnovfl <= x <= lnovfl+ln2 : sinh(x) := expm1(x)/2 (avoid overflow) 4024606Szliu * lnovfl+ln2 < x < INF : overflow to INF 4124606Szliu * 4224606Szliu * 4324606Szliu * Special cases: 4424606Szliu * sinh(x) is x if x is +INF, -INF, or NaN. 4524606Szliu * only sinh(0)=0 is exact for finite argument. 4624606Szliu * 4724606Szliu * Accuracy: 4824606Szliu * sinh(x) returns the exact hyperbolic sine of x nearly rounded. In 4924606Szliu * a test run with 1,024,000 random arguments on a VAX, the maximum 5024606Szliu * observed error was 1.93 ulps (units in the last place). 5124606Szliu * 5224606Szliu * Constants: 5324606Szliu * The hexadecimal values are the intended ones for the following constants. 5424606Szliu * The decimal values may be used, provided that the compiler will convert 5524606Szliu * from decimal to binary accurately enough to produce the hexadecimal values 5624606Szliu * shown. 5724606Szliu */ 58*31790Szliu #if (defined(VAX)||defined(TAHOE)) 5926893Selefunt /* static double */ 6024606Szliu /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 6124606Szliu /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 6224606Szliu /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 6324606Szliu static long mln2hix[] = { 0x0f3343b0, 0x2bdbc7e2}; 6424606Szliu static long mln2lox[] = { 0x1b60a70f, 0x582a279e}; 6524606Szliu static long lnovflx[] = { 0x0f3343b0, 0x2bdac7e2}; 6624606Szliu #define mln2hi (*(double*)mln2hix) 6724606Szliu #define mln2lo (*(double*)mln2lox) 6824606Szliu #define lnovfl (*(double*)lnovflx) 6924606Szliu #else /* IEEE double */ 7026893Selefunt static double 7124606Szliu mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 7224606Szliu mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 7324606Szliu lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 7424606Szliu #endif 7524606Szliu 76*31790Szliu #if (defined(VAX)||defined(TAHOE)) 7724606Szliu static max = 126 ; 7824606Szliu #else /* IEEE double */ 7924606Szliu static max = 1023 ; 8024606Szliu #endif 8124606Szliu 8224606Szliu 8324606Szliu double sinh(x) 8424606Szliu double x; 8524606Szliu { 8624606Szliu static double one=1.0, half=1.0/2.0 ; 8724606Szliu double expm1(), t, scalb(), copysign(), sign; 88*31790Szliu #if (!defined(VAX)&&!defined(TAHOE)) 8924606Szliu if(x!=x) return(x); /* x is NaN */ 9024606Szliu #endif 9124606Szliu sign=copysign(one,x); 9224606Szliu x=copysign(x,one); 9324606Szliu if(x<lnovfl) 9424606Szliu {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));} 9524606Szliu 9624606Szliu else if(x <= lnovfl+0.7) 9724606Szliu /* subtract x by ln(2^(max+1)) and return 2^max*exp(x) 9824606Szliu to avoid unnecessary overflow */ 9924606Szliu return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign)); 10024606Szliu 10124606Szliu else /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */ 10224606Szliu return( expm1(x)*sign ); 10324606Szliu } 104