1*24604Szliu /*
2*24604Szliu  * Copyright (c) 1985 Regents of the University of California.
3*24604Szliu  *
4*24604Szliu  * Use and reproduction of this software are granted  in  accordance  with
5*24604Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
6*24604Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
7*24604Szliu  * source, and inclusion of this notice) with the additional understanding
8*24604Szliu  * that  all  recipients  should regard themselves as participants  in  an
9*24604Szliu  * ongoing  research  project and hence should  feel  obligated  to report
10*24604Szliu  * their  experiences (good or bad) with these elementary function  codes,
11*24604Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12*24604Szliu  */
13*24604Szliu 
14*24604Szliu #ifndef lint
15*24604Szliu static char sccsid[] = "@(#)log__L.c	1.1 (ELEFUNT) 09/06/85";
16*24604Szliu #endif not lint
17*24604Szliu 
18*24604Szliu /* log__L(Z)
19*24604Szliu  *		LOG(1+X) - 2S			       X
20*24604Szliu  * RETURN      ---------------  WHERE Z = S*S,  S = ------- , 0 <= Z <= .0294...
21*24604Szliu  *		      S				     2 + X
22*24604Szliu  *
23*24604Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS)
24*24604Szliu  * KERNEL FUNCTION FOR LOG; TO BE USED IN LOG1P, LOG, AND POW FUNCTIONS
25*24604Szliu  * CODED IN C BY K.C. NG, 1/19/85;
26*24604Szliu  * REVISED BY K.C. Ng, 2/3/85, 4/16/85.
27*24604Szliu  *
28*24604Szliu  * Method :
29*24604Szliu  *	1. Polynomial approximation: let s = x/(2+x).
30*24604Szliu  *	   Based on log(1+x) = log(1+s) - log(1-s)
31*24604Szliu  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
32*24604Szliu  *
33*24604Szliu  *	   (log(1+x) - 2s)/s is computed by
34*24604Szliu  *
35*24604Szliu  *	       z*(L1 + z*(L2 + z*(... (L7 + z*L8)...)))
36*24604Szliu  *
37*24604Szliu  *	   where z=s*s. (See the listing below for Lk's values.) The
38*24604Szliu  *	   coefficients are obtained by a special Remez algorithm.
39*24604Szliu  *
40*24604Szliu  * Accuracy:
41*24604Szliu  *	Assuming no rounding error, the maximum magnitude of the approximation
42*24604Szliu  *	error (absolute) is 2**(-58.49) for IEEE double, and 2**(-63.63)
43*24604Szliu  *	for VAX D format.
44*24604Szliu  *
45*24604Szliu  * Constants:
46*24604Szliu  * The hexadecimal values are the intended ones for the following constants.
47*24604Szliu  * The decimal values may be used, provided that the compiler will convert
48*24604Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
49*24604Szliu  * shown.
50*24604Szliu  */
51*24604Szliu 
52*24604Szliu #ifdef VAX	/* VAX D format (56 bits) */
53*24604Szliu /* static double */
54*24604Szliu /* L1     =  6.6666666666666703212E-1    , Hex  2^  0   *  .AAAAAAAAAAAAC5 */
55*24604Szliu /* L2     =  3.9999999999970461961E-1    , Hex  2^ -1   *  .CCCCCCCCCC2684 */
56*24604Szliu /* L3     =  2.8571428579395698188E-1    , Hex  2^ -1   *  .92492492F85782 */
57*24604Szliu /* L4     =  2.2222221233634724402E-1    , Hex  2^ -2   *  .E38E3839B7AF2C */
58*24604Szliu /* L5     =  1.8181879517064680057E-1    , Hex  2^ -2   *  .BA2EB4CC39655E */
59*24604Szliu /* L6     =  1.5382888777946145467E-1    , Hex  2^ -2   *  .9D8551E8C5781D */
60*24604Szliu /* L7     =  1.3338356561139403517E-1    , Hex  2^ -2   *  .8895B3907FCD92 */
61*24604Szliu /* L8     =  1.2500000000000000000E-1    , Hex  2^ -2   *  .80000000000000 */
62*24604Szliu static long        L1x[] = { 0xaaaa402a, 0xaac5aaaa};
63*24604Szliu static long        L2x[] = { 0xcccc3fcc, 0x2684cccc};
64*24604Szliu static long        L3x[] = { 0x49243f92, 0x578292f8};
65*24604Szliu static long        L4x[] = { 0x8e383f63, 0xaf2c39b7};
66*24604Szliu static long        L5x[] = { 0x2eb43f3a, 0x655ecc39};
67*24604Szliu static long        L6x[] = { 0x85513f1d, 0x781de8c5};
68*24604Szliu static long        L7x[] = { 0x95b33f08, 0xcd92907f};
69*24604Szliu static long        L8x[] = { 0x00003f00, 0x00000000};
70*24604Szliu #define       L1    (*(double*)L1x)
71*24604Szliu #define       L2    (*(double*)L2x)
72*24604Szliu #define       L3    (*(double*)L3x)
73*24604Szliu #define       L4    (*(double*)L4x)
74*24604Szliu #define       L5    (*(double*)L5x)
75*24604Szliu #define       L6    (*(double*)L6x)
76*24604Szliu #define       L7    (*(double*)L7x)
77*24604Szliu #define       L8    (*(double*)L8x)
78*24604Szliu #else	/* IEEE double */
79*24604Szliu static double
80*24604Szliu L1     =  6.6666666666667340202E-1    , /*Hex  2^ -1   *  1.5555555555592 */
81*24604Szliu L2     =  3.9999999999416702146E-1    , /*Hex  2^ -2   *  1.999999997FF24 */
82*24604Szliu L3     =  2.8571428742008753154E-1    , /*Hex  2^ -2   *  1.24924941E07B4 */
83*24604Szliu L4     =  2.2222198607186277597E-1    , /*Hex  2^ -3   *  1.C71C52150BEA6 */
84*24604Szliu L5     =  1.8183562745289935658E-1    , /*Hex  2^ -3   *  1.74663CC94342F */
85*24604Szliu L6     =  1.5314087275331442206E-1    , /*Hex  2^ -3   *  1.39A1EC014045B */
86*24604Szliu L7     =  1.4795612545334174692E-1    ; /*Hex  2^ -3   *  1.2F039F0085122 */
87*24604Szliu #endif
88*24604Szliu 
89*24604Szliu double log__L(z)
90*24604Szliu double z;
91*24604Szliu {
92*24604Szliu #ifdef VAX
93*24604Szliu     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*(L7+z*L8))))))));
94*24604Szliu #else	/* IEEE double */
95*24604Szliu     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*L7)))))));
96*24604Szliu #endif
97*24604Szliu }
98