xref: /minix3/lib/libm/noieee_src/n_log10.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*      $NetBSD: n_log10.c,v 1.7 2014/03/06 10:57:44 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras  * Copyright (c) 1985, 1993
42fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras  * are met:
92fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras  *    without specific prior written permission.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras  * SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)log10.c	8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* LOG10(X)
382fe8fb19SBen Gras  * RETURN THE BASE 10 LOGARITHM OF x
392fe8fb19SBen Gras  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
402fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 1/20/85;
412fe8fb19SBen Gras  * REVISED BY K.C. NG on 1/23/85, 3/7/85, 4/16/85.
422fe8fb19SBen Gras  *
432fe8fb19SBen Gras  * Required kernel function:
442fe8fb19SBen Gras  *	log(x)
452fe8fb19SBen Gras  *
462fe8fb19SBen Gras  * Method :
472fe8fb19SBen Gras  *			     log(x)
482fe8fb19SBen Gras  *		log10(x) = ---------  or  [1/log(10)]*log(x)
492fe8fb19SBen Gras  *			    log(10)
502fe8fb19SBen Gras  *
512fe8fb19SBen Gras  *    Note:
522fe8fb19SBen Gras  *	  [log(10)]   rounded to 56 bits has error  .0895  ulps,
532fe8fb19SBen Gras  *	  [1/log(10)] rounded to 53 bits has error  .198   ulps;
542fe8fb19SBen Gras  *	  therefore, for better accuracy, in VAX D format, we divide
552fe8fb19SBen Gras  *	  log(x) by log(10), but in IEEE Double format, we multiply
562fe8fb19SBen Gras  *	  log(x) by [1/log(10)].
572fe8fb19SBen Gras  *
582fe8fb19SBen Gras  * Special cases:
592fe8fb19SBen Gras  *	log10(x) is NaN with signal if x < 0;
602fe8fb19SBen Gras  *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
612fe8fb19SBen Gras  *	log10(NaN) is that NaN with no signal.
622fe8fb19SBen Gras  *
632fe8fb19SBen Gras  * Accuracy:
642fe8fb19SBen Gras  *	log10(X) returns the exact log10(x) nearly rounded. In a test run
652fe8fb19SBen Gras  *	with 1,536,000 random arguments on a VAX, the maximum observed
662fe8fb19SBen Gras  *	error was 1.74 ulps (units in the last place).
672fe8fb19SBen Gras  *
682fe8fb19SBen Gras  * Constants:
692fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
702fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
712fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
722fe8fb19SBen Gras  * shown.
732fe8fb19SBen Gras  */
742fe8fb19SBen Gras 
752fe8fb19SBen Gras #define _LIBM_STATIC
762fe8fb19SBen Gras #include "mathimpl.h"
772fe8fb19SBen Gras 
782fe8fb19SBen Gras vc(ln10hi, 2.3025850929940456790E0 ,5d8d,4113,a8ac,ddaa, 2, .935D8DDDAAA8AC)
792fe8fb19SBen Gras 
802fe8fb19SBen Gras ic(ivln10, 4.3429448190325181667E-1, -2, 1.BCB7B1526E50E)
812fe8fb19SBen Gras 
822fe8fb19SBen Gras #ifdef vccast
832fe8fb19SBen Gras #define	ln10hi	vccast(ln10hi)
842fe8fb19SBen Gras #endif
852fe8fb19SBen Gras 
862fe8fb19SBen Gras 
872fe8fb19SBen Gras double
log10(double x)882fe8fb19SBen Gras log10(double x)
892fe8fb19SBen Gras {
902fe8fb19SBen Gras #if defined(__vax__)||defined(tahoe)
912fe8fb19SBen Gras 	return(log(x)/ln10hi);
922fe8fb19SBen Gras #else	/* defined(__vax__)||defined(tahoe) */
932fe8fb19SBen Gras 	return(ivln10*log(x));
942fe8fb19SBen Gras #endif	/* defined(__vax__)||defined(tahoe) */
952fe8fb19SBen Gras }
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc float
log10f(float x)98*0a6a1f1dSLionel Sambuc log10f(float x)
99*0a6a1f1dSLionel Sambuc {
100*0a6a1f1dSLionel Sambuc #if defined(__vax__)||defined(tahoe)
101*0a6a1f1dSLionel Sambuc 	return(logf(x)/ln10hi);
102*0a6a1f1dSLionel Sambuc #else	/* defined(__vax__)||defined(tahoe) */
103*0a6a1f1dSLionel Sambuc 	return(ivln10*log(x));
104*0a6a1f1dSLionel Sambuc #endif	/* defined(__vax__)||defined(tahoe) */
105*0a6a1f1dSLionel Sambuc }
106