xref: /csrg-svn/lib/libm/common_source/log10.c (revision 24706)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)log10.c	1.2 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/11/85";
17 #endif not lint
18 
19 /* LOG10(X)
20  * RETURN THE BASE 10 LOGARITHM OF x
21  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
22  * CODED IN C BY K.C. NG, 1/20/85;
23  * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85.
24  *
25  * Required kernel function:
26  *	log(x)
27  *
28  * Method :
29  *			     log(x)
30  *		log10(x) = ---------  or  [1/log(10)]*log(x)
31  *			    log(10)
32  *
33  *    Note:
34  *	  [log(10)]   rounded to 56 bits has error  .0895  ulps,
35  *	  [1/log(10)] rounded to 53 bits has error  .198   ulps;
36  *	  therefore, for better accuracy, in VAX D format, we divide
37  *	  log(x) by log(10), but in IEEE Double format, we multiply
38  *	  log(x) by [1/log(10)].
39  *
40  * Special cases:
41  *	log10(x) is NaN with signal if x < 0;
42  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
43  *	log10(NaN) is that NaN with no signal.
44  *
45  * Accuracy:
46  *	log10(X) returns the exact log10(x) nearly rounded. In a test run
47  *	with 1,536,000 random arguments on a VAX, the maximum observed
48  *	error was 1.74 ulps (units in the last place).
49  *
50  * Constants:
51  * The hexadecimal values are the intended ones for the following constants.
52  * The decimal values may be used, provided that the compiler will convert
53  * from decimal to binary accurately enough to produce the hexadecimal values
54  * shown.
55  */
56 
57 #ifdef VAX	/* VAX D format (56 bits) */
58 /* static double */
59 /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
60 static long    ln10hix[] = { 0x5d8d4113, 0xa8acddaa};
61 #define   ln10hi    (*(double*)ln10hix)
62 #else	/* IEEE double */
63 static double
64 ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
65 #endif
66 
67 double log10(x)
68 double x;
69 {
70 	double log();
71 
72 #ifdef VAX
73 	return(log(x)/ln10hi);
74 #else	/* IEEE double */
75 	return(ivln10*log(x));
76 #endif
77 }
78