From: Bruno Haible Date: Sat, 19 Apr 2008 15:55:05 +0000 (+0200) Subject: Make floorlog10 function more precise. X-Git-Tag: v0.1~7531 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=ee761da90342d654b308626640bfe1128a78a2ee;p=gnulib.git Make floorlog10 function more precise. --- diff --git a/ChangeLog b/ChangeLog index a9cd1557c..87c3de2eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2008-04-19 Bruno Haible + * lib/vasnprintf.c (floorlog10l, floorlog10): Reduce maximum error + from 0.0058 to less than 10^-7. + +2008-04-19 Bruno Haible + Fix rounding when a precision is given. * lib/vasnprintf.c (is_borderline): New function. (VASNPRINTF): For %e and %g, consider replacing the digits 10....0 with diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 2ee0c05ce..fbf13c8ef 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1301,9 +1301,9 @@ floorlog10l (long double x) } /* Now 0.95 <= z <= 1.01. */ z = 1 - z; - /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... + /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...) Four terms are enough to get an approximation with error < 10^-7. */ - l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); + l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); /* Finally multiply with log(2)/log(10), yields an approximation for log10(x). */ l *= 0.30102999566398119523; @@ -1392,9 +1392,9 @@ floorlog10 (double x) } /* Now 0.95 <= z <= 1.01. */ z = 1 - z; - /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... + /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...) Four terms are enough to get an approximation with error < 10^-7. */ - l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); + l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); /* Finally multiply with log(2)/log(10), yields an approximation for log10(x). */ l *= 0.30102999566398119523;