xref: /csrg-svn/lib/libm/common_source/log10.c (revision 34126)
1*34126Sbostic /*
224602Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34126Sbostic  * All rights reserved.
4*34126Sbostic  *
5*34126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34126Sbostic  * provided that this notice is preserved and that due credit is given
7*34126Sbostic  * to the University of California at Berkeley. The name of the University
8*34126Sbostic  * may not be used to endorse or promote products derived from this
9*34126Sbostic  * software without specific prior written permission. This software
10*34126Sbostic  * is provided ``as is'' without express or implied warranty.
11*34126Sbostic  *
12*34126Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34126Sbostic  * research project and hence should feel obligated to report their
14*34126Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34126Sbostic  * the sendbug(8) program, to the authors.
1624602Szliu  */
1724602Szliu 
1824602Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)log10.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124602Szliu 
2224602Szliu /* LOG10(X)
2324602Szliu  * RETURN THE BASE 10 LOGARITHM OF x
2424602Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
2524602Szliu  * CODED IN C BY K.C. NG, 1/20/85;
2624602Szliu  * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85.
2724602Szliu  *
2824602Szliu  * Required kernel function:
2924602Szliu  *	log(x)
3024602Szliu  *
3124602Szliu  * Method :
3224602Szliu  *			     log(x)
3324602Szliu  *		log10(x) = ---------  or  [1/log(10)]*log(x)
3424602Szliu  *			    log(10)
3524602Szliu  *
3624602Szliu  *    Note:
3724602Szliu  *	  [log(10)]   rounded to 56 bits has error  .0895  ulps,
3824602Szliu  *	  [1/log(10)] rounded to 53 bits has error  .198   ulps;
3924602Szliu  *	  therefore, for better accuracy, in VAX D format, we divide
4024602Szliu  *	  log(x) by log(10), but in IEEE Double format, we multiply
4124602Szliu  *	  log(x) by [1/log(10)].
4224602Szliu  *
4324602Szliu  * Special cases:
4424602Szliu  *	log10(x) is NaN with signal if x < 0;
4524602Szliu  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
4624602Szliu  *	log10(NaN) is that NaN with no signal.
4724602Szliu  *
4824602Szliu  * Accuracy:
4924602Szliu  *	log10(X) returns the exact log10(x) nearly rounded. In a test run
5024602Szliu  *	with 1,536,000 random arguments on a VAX, the maximum observed
5124602Szliu  *	error was 1.74 ulps (units in the last place).
5224602Szliu  *
5324602Szliu  * Constants:
5424602Szliu  * The hexadecimal values are the intended ones for the following constants.
5524602Szliu  * The decimal values may be used, provided that the compiler will convert
5624602Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5724602Szliu  * shown.
5824602Szliu  */
5924602Szliu 
6031853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
6131853Szliu #ifdef vax
6231812Szliu #define _0x(A,B)	0x/**/A/**/B
6331853Szliu #else	/* vax */
6431812Szliu #define _0x(A,B)	0x/**/B/**/A
6531853Szliu #endif	/* vax */
6624602Szliu /* static double */
6724602Szliu /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
6831812Szliu static long    ln10hix[] = { _0x(5d8d,4113), _0x(a8ac,ddaa)};
6924602Szliu #define   ln10hi    (*(double*)ln10hix)
7031853Szliu #else	/* defined(vax)||defined(tahoe)	*/
7124602Szliu static double
7224602Szliu ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
7331853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
7424602Szliu 
7524602Szliu double log10(x)
7624602Szliu double x;
7724602Szliu {
7824602Szliu 	double log();
7924602Szliu 
8031853Szliu #if defined(vax)||defined(tahoe)
8124602Szliu 	return(log(x)/ln10hi);
8231853Szliu #else	/* defined(vax)||defined(tahoe) */
8324602Szliu 	return(ivln10*log(x));
8431853Szliu #endif	/* defined(vax)||defined(tahoe) */
8524602Szliu }
86