Annotation of 43BSDTahoe/usr.lib/libm/log1p.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1985 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that the above copyright notice and this paragraph are
        !             7:  * duplicated in all such forms and that any documentation,
        !             8:  * advertising materials, and other materials related to such
        !             9:  * distribution and use acknowledge that the software was developed
        !            10:  * by the University of California, Berkeley.  The name of the
        !            11:  * University may not be used to endorse or promote products derived
        !            12:  * from this software without specific prior written permission.
        !            13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            16:  *
        !            17:  * All recipients should regard themselves as participants in an ongoing
        !            18:  * research project and hence should feel obligated to report their
        !            19:  * experiences (good or bad) with these elementary function codes, using
        !            20:  * the sendbug(8) program, to the authors.
        !            21:  */
        !            22: 
        !            23: #ifndef lint
        !            24: static char sccsid[] = "@(#)log1p.c    5.3 (Berkeley) 6/30/88";
        !            25: #endif /* not lint */
        !            26: 
        !            27: /* LOG1P(x) 
        !            28:  * RETURN THE LOGARITHM OF 1+x
        !            29:  * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
        !            30:  * CODED IN C BY K.C. NG, 1/19/85; 
        !            31:  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
        !            32:  * 
        !            33:  * Required system supported functions:
        !            34:  *     scalb(x,n) 
        !            35:  *     copysign(x,y)
        !            36:  *     logb(x) 
        !            37:  *     finite(x)
        !            38:  *
        !            39:  * Required kernel function:
        !            40:  *     log__L(z)
        !            41:  *
        !            42:  * Method :
        !            43:  *     1. Argument Reduction: find k and f such that 
        !            44:  *                     1+x  = 2^k * (1+f), 
        !            45:  *        where  sqrt(2)/2 < 1+f < sqrt(2) .
        !            46:  *
        !            47:  *     2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
        !            48:  *              = 2s + 2/3 s**3 + 2/5 s**5 + .....,
        !            49:  *        log(1+f) is computed by
        !            50:  *
        !            51:  *                     log(1+f) = 2s + s*log__L(s*s)
        !            52:  *        where
        !            53:  *             log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
        !            54:  *
        !            55:  *        See log__L() for the values of the coefficients.
        !            56:  *
        !            57:  *     3. Finally,  log(1+x) = k*ln2 + log(1+f).  
        !            58:  *
        !            59:  *     Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
        !            60:  *                n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last 
        !            61:  *                20 bits (for VAX D format), or the last 21 bits ( for IEEE 
        !            62:  *                double) is 0. This ensures n*ln2hi is exactly representable.
        !            63:  *             2. In step 1, f may not be representable. A correction term c
        !            64:  *                for f is computed. It follows that the correction term for
        !            65:  *                f - t (the leading term of log(1+f) in step 2) is c-c*x. We
        !            66:  *                add this correction term to n*ln2lo to attenuate the error.
        !            67:  *
        !            68:  *
        !            69:  * Special cases:
        !            70:  *     log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
        !            71:  *     log1p(INF) is +INF; log1p(-1) is -INF with signal;
        !            72:  *     only log1p(0)=0 is exact for finite argument.
        !            73:  *
        !            74:  * Accuracy:
        !            75:  *     log1p(x) returns the exact log(1+x) nearly rounded. In a test run 
        !            76:  *     with 1,536,000 random arguments on a VAX, the maximum observed
        !            77:  *     error was .846 ulps (units in the last place).
        !            78:  *
        !            79:  * Constants:
        !            80:  * The hexadecimal values are the intended ones for the following constants.
        !            81:  * The decimal values may be used, provided that the compiler will convert
        !            82:  * from decimal to binary accurately enough to produce the hexadecimal values
        !            83:  * shown.
        !            84:  */
        !            85: 
        !            86: #if defined(vax)||defined(tahoe)       /* VAX D format */
        !            87: #include <errno.h>
        !            88: #ifdef vax
        !            89: #define _0x(A,B)       0x/**/A/**/B
        !            90: #else  /* vax */
        !            91: #define _0x(A,B)       0x/**/B/**/A
        !            92: #endif /* vax */
        !            93: /* static double */
        !            94: /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
        !            95: /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
        !            96: /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
        !            97: static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
        !            98: static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
        !            99: static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
        !           100: #define    ln2hi    (*(double*)ln2hix)
        !           101: #define    ln2lo    (*(double*)ln2lox)
        !           102: #define    sqrt2    (*(double*)sqrt2x)
        !           103: #else  /* defined(vax)||defined(tahoe) */
        !           104: static double
        !           105: ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
        !           106: ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
        !           107: sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
        !           108: #endif /* defined(vax)||defined(tahoe) */
        !           109: 
        !           110: double log1p(x)
        !           111: double x;
        !           112: {
        !           113:        static double zero=0.0, negone= -1.0, one=1.0, 
        !           114:                      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
        !           115:        double logb(),copysign(),scalb(),log__L(),z,s,t,c;
        !           116:        int k,finite();
        !           117: 
        !           118: #if !defined(vax)&&!defined(tahoe)
        !           119:        if(x!=x) return(x);     /* x is NaN */
        !           120: #endif /* !defined(vax)&&!defined(tahoe) */
        !           121: 
        !           122:        if(finite(x)) {
        !           123:           if( x > negone ) {
        !           124: 
        !           125:           /* argument reduction */
        !           126:              if(copysign(x,one)<small) return(x);
        !           127:              k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
        !           128:              if(z+t >= sqrt2 ) 
        !           129:                  { k += 1 ; z *= half; t *= half; }
        !           130:              t += negone; x = z + t;
        !           131:              c = (t-x)+z ;             /* correction term for x */
        !           132: 
        !           133:           /* compute log(1+x)  */
        !           134:               s = x/(2+x); t = x*x*half;
        !           135:              c += (k*ln2lo-c*x);
        !           136:              z = c+s*(t+log__L(s*s));
        !           137:              x += (z - t) ;
        !           138: 
        !           139:              return(k*ln2hi+x);
        !           140:           }
        !           141:        /* end of if (x > negone) */
        !           142: 
        !           143:            else {
        !           144: #if defined(vax)||defined(tahoe)
        !           145:                extern double infnan();
        !           146:                if ( x == negone )
        !           147:                    return (infnan(-ERANGE));   /* -INF */
        !           148:                else
        !           149:                    return (infnan(EDOM));      /* NaN */
        !           150: #else  /* defined(vax)||defined(tahoe) */
        !           151:                /* x = -1, return -INF with signal */
        !           152:                if ( x == negone ) return( negone/zero );
        !           153: 
        !           154:                /* negative argument for log, return NaN with signal */
        !           155:                else return ( zero / zero );
        !           156: #endif /* defined(vax)||defined(tahoe) */
        !           157:            }
        !           158:        }
        !           159:     /* end of if (finite(x)) */
        !           160: 
        !           161:     /* log(-INF) is NaN */
        !           162:        else if(x<0) 
        !           163:             return(zero/zero);
        !           164: 
        !           165:     /* log(+INF) is INF */
        !           166:        else return(x);      
        !           167: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.