xref: /csrg-svn/lib/libm/common_source/log10.c (revision 34931)
134126Sbostic /*
224602Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
534126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34931Sbostic  * provided that the above copyright notice and this paragraph are
7*34931Sbostic  * duplicated in all such forms and that any documentation,
8*34931Sbostic  * advertising materials, and other materials related to such
9*34931Sbostic  * distribution and use acknowledge that the software was developed
10*34931Sbostic  * by the University of California, Berkeley.  The name of the
11*34931Sbostic  * University may not be used to endorse or promote products derived
12*34931Sbostic  * from this software without specific prior written permission.
13*34931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34931Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634126Sbostic  *
1734126Sbostic  * All recipients should regard themselves as participants in an ongoing
1834126Sbostic  * research project and hence should feel obligated to report their
1934126Sbostic  * experiences (good or bad) with these elementary function codes, using
2034126Sbostic  * the sendbug(8) program, to the authors.
2124602Szliu  */
2224602Szliu 
2324602Szliu #ifndef lint
24*34931Sbostic static char sccsid[] = "@(#)log10.c	5.3 (Berkeley) 06/30/88";
2534126Sbostic #endif /* not lint */
2624602Szliu 
2724602Szliu /* LOG10(X)
2824602Szliu  * RETURN THE BASE 10 LOGARITHM OF x
2924602Szliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
3024602Szliu  * CODED IN C BY K.C. NG, 1/20/85;
3124602Szliu  * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85.
3224602Szliu  *
3324602Szliu  * Required kernel function:
3424602Szliu  *	log(x)
3524602Szliu  *
3624602Szliu  * Method :
3724602Szliu  *			     log(x)
3824602Szliu  *		log10(x) = ---------  or  [1/log(10)]*log(x)
3924602Szliu  *			    log(10)
4024602Szliu  *
4124602Szliu  *    Note:
4224602Szliu  *	  [log(10)]   rounded to 56 bits has error  .0895  ulps,
4324602Szliu  *	  [1/log(10)] rounded to 53 bits has error  .198   ulps;
4424602Szliu  *	  therefore, for better accuracy, in VAX D format, we divide
4524602Szliu  *	  log(x) by log(10), but in IEEE Double format, we multiply
4624602Szliu  *	  log(x) by [1/log(10)].
4724602Szliu  *
4824602Szliu  * Special cases:
4924602Szliu  *	log10(x) is NaN with signal if x < 0;
5024602Szliu  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
5124602Szliu  *	log10(NaN) is that NaN with no signal.
5224602Szliu  *
5324602Szliu  * Accuracy:
5424602Szliu  *	log10(X) returns the exact log10(x) nearly rounded. In a test run
5524602Szliu  *	with 1,536,000 random arguments on a VAX, the maximum observed
5624602Szliu  *	error was 1.74 ulps (units in the last place).
5724602Szliu  *
5824602Szliu  * Constants:
5924602Szliu  * The hexadecimal values are the intended ones for the following constants.
6024602Szliu  * The decimal values may be used, provided that the compiler will convert
6124602Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6224602Szliu  * shown.
6324602Szliu  */
6424602Szliu 
6531853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
6631853Szliu #ifdef vax
6731812Szliu #define _0x(A,B)	0x/**/A/**/B
6831853Szliu #else	/* vax */
6931812Szliu #define _0x(A,B)	0x/**/B/**/A
7031853Szliu #endif	/* vax */
7124602Szliu /* static double */
7224602Szliu /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
7331812Szliu static long    ln10hix[] = { _0x(5d8d,4113), _0x(a8ac,ddaa)};
7424602Szliu #define   ln10hi    (*(double*)ln10hix)
7531853Szliu #else	/* defined(vax)||defined(tahoe)	*/
7624602Szliu static double
7724602Szliu ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
7831853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
7924602Szliu 
8024602Szliu double log10(x)
8124602Szliu double x;
8224602Szliu {
8324602Szliu 	double log();
8424602Szliu 
8531853Szliu #if defined(vax)||defined(tahoe)
8624602Szliu 	return(log(x)/ln10hi);
8731853Szliu #else	/* defined(vax)||defined(tahoe) */
8824602Szliu 	return(ivln10*log(x));
8931853Szliu #endif	/* defined(vax)||defined(tahoe) */
9024602Szliu }
91