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[] = "@(#)expm1.c	1.2 (Berkeley) 8/21/85";
  16: #endif not lint
  17: 
  18: /* EXPM1(X)
  19:  * RETURN THE EXPONENTIAL OF X MINUS ONE
  20:  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
  21:  * CODED IN C BY K.C. NG, 1/19/85;
  22:  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
  23:  *
  24:  * Required system supported functions:
  25:  *	scalb(x,n)
  26:  *	copysign(x,y)
  27:  *	finite(x)
  28:  *
  29:  * Kernel function:
  30:  *	exp__E(x,c)
  31:  *
  32:  * Method:
  33:  *	1. Argument Reduction: given the input x, find r and integer k such
  34:  *	   that
  35:  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
  36:  *	   r will be represented as r := z+c for better accuracy.
  37:  *
  38:  *	2. Compute EXPM1(r)=exp(r)-1 by
  39:  *
  40:  *			EXPM1(r=z+c) := z + exp__E(z,c)
  41:  *
  42:  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
  43:  *
  44:  * 	Remarks:
  45:  *	   1. When k=1 and z < -0.25, we use the following formula for
  46:  *	      better accuracy:
  47:  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
  48:  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
  49:  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
  50:  *	      when k>56.
  51:  *
  52:  * Special cases:
  53:  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
  54:  *	EXPM1(-INF)= -1;
  55:  *	for finite argument, only EXPM1(0)=0 is exact.
  56:  *
  57:  * Accuracy:
  58:  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
  59:  *	1,166,000 random arguments on a VAX, the maximum observed error was
  60:  *	.872 ulps (units of the last place).
  61:  *
  62:  * Constants:
  63:  * The hexadecimal values are the intended ones for the following constants.
  64:  * The decimal values may be used, provided that the compiler will convert
  65:  * from decimal to binary accurately enough to produce the hexadecimal values
  66:  * shown.
  67:  */
  68: 
  69: #ifdef VAX  /* VAX D format */
  70: /* double static */
  71: /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
  72: /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
  73: /* lnhuge =  9.4961163736712506989E1     , Hex  2^  7   *  .BDEC1DA73E9010 */
  74: /* invln2 =  1.4426950408889634148E0     ; Hex  2^  1   *  .B8AA3B295C17F1 */
  75: static long     ln2hix[] = { 0x72174031, 0x0000f7d0};
  76: static long     ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1};
  77: static long    lnhugex[] = { 0xec1d43bd, 0x9010a73e};
  78: static long    invln2x[] = { 0xaa3b40b8, 0x17f1295c};
  79: #define    ln2hi    (*(double*)ln2hix)
  80: #define    ln2lo    (*(double*)ln2lox)
  81: #define   lnhuge    (*(double*)lnhugex)
  82: #define   invln2    (*(double*)invln2x)
  83: #else   /* IEEE double */
  84: double static
  85: ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
  86: ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
  87: lnhuge =  7.1602103751842355450E2     , /*Hex  2^  9   *  1.6602B15B7ECF2 */
  88: invln2 =  1.4426950408889633870E0     ; /*Hex  2^  0   *  1.71547652B82FE */
  89: #endif
  90: 
  91: double expm1(x)
  92: double x;
  93: {
  94:     double static one=1.0, half=1.0/2.0;
  95:     double scalb(), copysign(), exp__E(), z,hi,lo,c;
  96:     int k,finite();
  97: #ifdef VAX
  98:     static prec=56;
  99: #else   /* IEEE double */
 100:     static prec=53;
 101: #endif
 102: #ifndef VAX
 103:     if(x!=x) return(x); /* x is NaN */
 104: #endif
 105: 
 106:     if( x <= lnhuge ) {
 107:         if( x >= -40.0 ) {
 108: 
 109:             /* argument reduction : x - k*ln2 */
 110:             k= invln2 *x+copysign(0.5,x);   /* k=NINT(x/ln2) */
 111:             hi=x-k*ln2hi ;
 112:             z=hi-(lo=k*ln2lo);
 113:             c=(hi-z)-lo;
 114: 
 115:             if(k==0) return(z+exp__E(z,c));
 116:             if(k==1)
 117:                 if(z< -0.25)
 118:                 {x=z+half;x +=exp__E(z,c); return(x+x);}
 119:                 else
 120:                 {z+=exp__E(z,c); x=half+z; return(x+x);}
 121:             /* end of k=1 */
 122: 
 123:             else {
 124:                 if(k<=prec)
 125:                   { x=one-scalb(one,-k); z += exp__E(z,c);}
 126:                 else if(k<100)
 127:                   { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
 128:                 else
 129:                   { x = exp__E(z,c)+z; z=one;}
 130: 
 131:                 return (scalb(x+z,k));
 132:             }
 133:         }
 134:         /* end of x > lnunfl */
 135: 
 136:         else
 137:              /* expm1(-big#) rounded to -1 (inexact) */
 138:              if(finite(x))
 139:              { ln2hi+ln2lo; return(-one);}
 140: 
 141:              /* expm1(-INF) is -1 */
 142:              else return(-one);
 143:     }
 144:     /* end of x < lnhuge */
 145: 
 146:     else
 147:     /*  expm1(INF) is INF, expm1(+big#) overflows to INF */
 148:         return( finite(x) ?  scalb(one,5000) : x);
 149: }

Defined functions

expm1 defined in line 91; used 9 times

Defined variables

invln2x defined in line 78; used 1 times
  • in line 82
ln2hi defined in line 85; never used
ln2hix defined in line 75; used 1 times
  • in line 79
ln2lox defined in line 76; used 1 times
  • in line 80
lnhugex defined in line 77; used 1 times
  • in line 81
sccsid defined in line 15; never used

Defined macros

invln2 defined in line 82; used 2 times
ln2hi defined in line 79; used 2 times
ln2lo defined in line 80; used 3 times
lnhuge defined in line 81; used 2 times
Last modified: 1985-08-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1127
Valid CSS Valid XHTML 1.0 Strict