xref: /csrg-svn/lib/libm/common_source/log10.c (revision 24706)
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
15*24706Selefunt static char sccsid[] =
16*24706Selefunt "@(#)log10.c	1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/11/85";
1724602Szliu #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 
5724602Szliu #ifdef VAX	/* VAX D format (56 bits) */
5824602Szliu /* static double */
5924602Szliu /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
6024602Szliu static long    ln10hix[] = { 0x5d8d4113, 0xa8acddaa};
6124602Szliu #define   ln10hi    (*(double*)ln10hix)
6224602Szliu #else	/* IEEE double */
6324602Szliu static double
6424602Szliu ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
6524602Szliu #endif
6624602Szliu 
6724602Szliu double log10(x)
6824602Szliu double x;
6924602Szliu {
7024602Szliu 	double log();
7124602Szliu 
7224602Szliu #ifdef VAX
7324602Szliu 	return(log(x)/ln10hi);
7424602Szliu #else	/* IEEE double */
7524602Szliu 	return(ivln10*log(x));
7624602Szliu #endif
7724602Szliu }
78