xref: /csrg-svn/lib/libm/common_source/log10.c (revision 31853)
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.5 (ucb.elefunt) 07/13/87";
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 #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
58 #ifdef vax
59 #define _0x(A,B)	0x/**/A/**/B
60 #else	/* vax */
61 #define _0x(A,B)	0x/**/B/**/A
62 #endif	/* vax */
63 /* static double */
64 /* ln10hi =  2.3025850929940456790E0     ; Hex   2^  2   *  .935D8DDDAAA8AC */
65 static long    ln10hix[] = { _0x(5d8d,4113), _0x(a8ac,ddaa)};
66 #define   ln10hi    (*(double*)ln10hix)
67 #else	/* defined(vax)||defined(tahoe)	*/
68 static double
69 ivln10 =  4.3429448190325181667E-1    ; /*Hex   2^ -2   *  1.BCB7B1526E50E */
70 #endif	/* defined(vax)||defined(tahoe)	*/
71 
72 double log10(x)
73 double x;
74 {
75 	double log();
76 
77 #if defined(vax)||defined(tahoe)
78 	return(log(x)/ln10hi);
79 #else	/* defined(vax)||defined(tahoe) */
80 	return(ivln10*log(x));
81 #endif	/* defined(vax)||defined(tahoe) */
82 }
83