xref: /csrg-svn/lib/libm/common_source/log10.c (revision 34126)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  *
12  * All recipients should regard themselves as participants in an ongoing
13  * research project and hence should feel obligated to report their
14  * experiences (good or bad) with these elementary function codes, using
15  * the sendbug(8) program, to the authors.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)log10.c	5.2 (Berkeley) 04/29/88";
20 #endif /* not lint */
21 
22 /* LOG10(X)
23  * RETURN THE BASE 10 LOGARITHM OF x
24  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
25  * CODED IN C BY K.C. NG, 1/20/85;
26  * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85.
27  *
28  * Required kernel function:
29  *	log(x)
30  *
31  * Method :
32  *			     log(x)
33  *		log10(x) = ---------  or  [1/log(10)]*log(x)
34  *			    log(10)
35  *
36  *    Note:
37  *	  [log(10)]   rounded to 56 bits has error  .0895  ulps,
38  *	  [1/log(10)] rounded to 53 bits has error  .198   ulps;
39  *	  therefore, for better accuracy, in VAX D format, we divide
40  *	  log(x) by log(10), but in IEEE Double format, we multiply
41  *	  log(x) by [1/log(10)].
42  *
43  * Special cases:
44  *	log10(x) is NaN with signal if x < 0;
45  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
46  *	log10(NaN) is that NaN with no signal.
47  *
48  * Accuracy:
49  *	log10(X) returns the exact log10(x) nearly rounded. In a test run
50  *	with 1,536,000 random arguments on a VAX, the maximum observed
51  *	error was 1.74 ulps (units in the last place).
52  *
53  * Constants:
54  * The hexadecimal values are the intended ones for the following constants.
55  * The decimal values may be used, provided that the compiler will convert
56  * from decimal to binary accurately enough to produce the hexadecimal values
57  * shown.
58  */
59 
60 #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
61 #ifdef vax
62 #define _0x(A,B)	0x/**/A/**/B
63 #else	/* vax */
64 #define _0x(A,B)	0x/**/B/**/A
65 #endif	/* vax */
66 /* static double */
67 /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
68 static long    ln10hix[] = { _0x(5d8d,4113), _0x(a8ac,ddaa)};
69 #define   ln10hi    (*(double*)ln10hix)
70 #else	/* defined(vax)||defined(tahoe)	*/
71 static double
72 ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
73 #endif	/* defined(vax)||defined(tahoe)	*/
74 
75 double log10(x)
76 double x;
77 {
78 	double log();
79 
80 #if defined(vax)||defined(tahoe)
81 	return(log(x)/ln10hi);
82 #else	/* defined(vax)||defined(tahoe) */
83 	return(ivln10*log(x));
84 #endif	/* defined(vax)||defined(tahoe) */
85 }
86