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.6 (ucb.elefunt) 07/13/87"; 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 #if defined(vax)||defined(tahoe) 59 #ifdef vax 60 #define _0x(A,B) 0x/**/A/**/B 61 #else /* vax */ 62 #define _0x(A,B) 0x/**/B/**/A 63 #endif /* vax */ 64 /* static double */ 65 /* mln2hi = 8.8029691931113054792E1 , Hex 2^ 7 * .B00F33C7E22BDB */ 66 /* mln2lo = -4.9650192275318476525E-16 , Hex 2^-50 * -.8F1B60279E582A */ 67 /* lnovfl = 8.8029691931113053016E1 ; Hex 2^ 7 * .B00F33C7E22BDA */ 68 static long mln2hix[] = { _0x(0f33,43b0), _0x(2bdb,c7e2)}; 69 static long mln2lox[] = { _0x(1b60,a70f), _0x(582a,279e)}; 70 static long lnovflx[] = { _0x(0f33,43b0), _0x(2bda,c7e2)}; 71 #define mln2hi (*(double*)mln2hix) 72 #define mln2lo (*(double*)mln2lox) 73 #define lnovfl (*(double*)lnovflx) 74 #else /* defined(vax)||defined(tahoe) */ 75 static double 76 mln2hi = 7.0978271289338397310E2 , /*Hex 2^ 10 * 1.62E42FEFA39EF */ 77 mln2lo = 2.3747039373786107478E-14 , /*Hex 2^-45 * 1.ABC9E3B39803F */ 78 lnovfl = 7.0978271289338397310E2 ; /*Hex 2^ 9 * 1.62E42FEFA39EF */ 79 #endif /* defined(vax)||defined(tahoe) */ 80 81 #if defined(vax)||defined(tahoe) 82 static max = 126 ; 83 #else /* defined(vax)||defined(tahoe) */ 84 static max = 1023 ; 85 #endif /* defined(vax)||defined(tahoe) */ 86 87 88 double sinh(x) 89 double x; 90 { 91 static double one=1.0, half=1.0/2.0 ; 92 double expm1(), t, scalb(), copysign(), sign; 93 #if !defined(vax)&&!defined(tahoe) 94 if(x!=x) return(x); /* x is NaN */ 95 #endif /* !defined(vax)&&!defined(tahoe) */ 96 sign=copysign(one,x); 97 x=copysign(x,one); 98 if(x<lnovfl) 99 {t=expm1(x); return(copysign((t+t/(one+t))*half,sign));} 100 101 else if(x <= lnovfl+0.7) 102 /* subtract x by ln(2^(max+1)) and return 2^max*exp(x) 103 to avoid unnecessary overflow */ 104 return(copysign(scalb(one+expm1((x-mln2hi)-mln2lo),max),sign)); 105 106 else /* sinh(+-INF) = +-INF, sinh(+-big no.) overflow to +-INF */ 107 return( expm1(x)*sign ); 108 } 109