124602Szliu /* 224602Szliu * Copyright (c) 1985 Regents of the University of California. 324602Szliu * 424602Szliu * Use and reproduction of this software are granted in accordance with 524602Szliu * the terms and conditions specified in the Berkeley Software License 624602Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724602Szliu * source, and inclusion of this notice) with the additional understanding 824602Szliu * that all recipients should regard themselves as participants in an 924602Szliu * ongoing research project and hence should feel obligated to report 1024602Szliu * their experiences (good or bad) with these elementary function codes, 1124602Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224602Szliu */ 1324602Szliu 1424602Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31853Szliu "@(#)log10.c 1.2 (Berkeley) 8/21/85; 1.5 (ucb.elefunt) 07/13/87"; 17*31853Szliu #endif /* not lint */ 1824602Szliu 1924602Szliu /* LOG10(X) 2024602Szliu * RETURN THE BASE 10 LOGARITHM OF x 2124602Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224602Szliu * CODED IN C BY K.C. NG, 1/20/85; 2324602Szliu * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85. 2424602Szliu * 2524602Szliu * Required kernel function: 2624602Szliu * log(x) 2724602Szliu * 2824602Szliu * Method : 2924602Szliu * log(x) 3024602Szliu * log10(x) = --------- or [1/log(10)]*log(x) 3124602Szliu * log(10) 3224602Szliu * 3324602Szliu * Note: 3424602Szliu * [log(10)] rounded to 56 bits has error .0895 ulps, 3524602Szliu * [1/log(10)] rounded to 53 bits has error .198 ulps; 3624602Szliu * therefore, for better accuracy, in VAX D format, we divide 3724602Szliu * log(x) by log(10), but in IEEE Double format, we multiply 3824602Szliu * log(x) by [1/log(10)]. 3924602Szliu * 4024602Szliu * Special cases: 4124602Szliu * log10(x) is NaN with signal if x < 0; 4224602Szliu * log10(+INF) is +INF with no signal; log10(0) is -INF with signal; 4324602Szliu * log10(NaN) is that NaN with no signal. 4424602Szliu * 4524602Szliu * Accuracy: 4624602Szliu * log10(X) returns the exact log10(x) nearly rounded. In a test run 4724602Szliu * with 1,536,000 random arguments on a VAX, the maximum observed 4824602Szliu * error was 1.74 ulps (units in the last place). 4924602Szliu * 5024602Szliu * Constants: 5124602Szliu * The hexadecimal values are the intended ones for the following constants. 5224602Szliu * The decimal values may be used, provided that the compiler will convert 5324602Szliu * from decimal to binary accurately enough to produce the hexadecimal values 5424602Szliu * shown. 5524602Szliu */ 5624602Szliu 57*31853Szliu #if defined(vax)||defined(tahoe) /* VAX D format (56 bits) */ 58*31853Szliu #ifdef vax 5931812Szliu #define _0x(A,B) 0x/**/A/**/B 60*31853Szliu #else /* vax */ 6131812Szliu #define _0x(A,B) 0x/**/B/**/A 62*31853Szliu #endif /* vax */ 6324602Szliu /* static double */ 6424602Szliu /* ln10hi = 2.3025850929940456790E0 ; Hex 2^ 2 * .935D8DDDAAA8AC */ 6531812Szliu static long ln10hix[] = { _0x(5d8d,4113), _0x(a8ac,ddaa)}; 6624602Szliu #define ln10hi (*(double*)ln10hix) 67*31853Szliu #else /* defined(vax)||defined(tahoe) */ 6824602Szliu static double 6924602Szliu ivln10 = 4.3429448190325181667E-1 ; /*Hex 2^ -2 * 1.BCB7B1526E50E */ 70*31853Szliu #endif /* defined(vax)||defined(tahoe) */ 7124602Szliu 7224602Szliu double log10(x) 7324602Szliu double x; 7424602Szliu { 7524602Szliu double log(); 7624602Szliu 77*31853Szliu #if defined(vax)||defined(tahoe) 7824602Szliu return(log(x)/ln10hi); 79*31853Szliu #else /* defined(vax)||defined(tahoe) */ 8024602Szliu return(ivln10*log(x)); 81*31853Szliu #endif /* defined(vax)||defined(tahoe) */ 8224602Szliu } 83