From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/4827 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: printf issues Date: Mon, 7 Apr 2014 14:04:26 -0400 Message-ID: <20140407180426.GD26358@brightrain.aerifal.cx> References: <20140404185413.GH3034@port70.net> <20140404210837.GS26358@brightrain.aerifal.cx> <20140407072930.GB26358@brightrain.aerifal.cx> <20140407153611.GM3034@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1396893888 25719 80.91.229.3 (7 Apr 2014 18:04:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 7 Apr 2014 18:04:48 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-4831-gllmg-musl=m.gmane.org@lists.openwall.com Mon Apr 07 20:04:41 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1WXDuR-0000vz-Cq for gllmg-musl@plane.gmane.org; Mon, 07 Apr 2014 20:04:39 +0200 Original-Received: (qmail 7608 invoked by uid 550); 7 Apr 2014 18:04:38 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 7600 invoked from network); 7 Apr 2014 18:04:38 -0000 Content-Disposition: inline In-Reply-To: <20140407153611.GM3034@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:4827 Archived-At: On Mon, Apr 07, 2014 at 05:36:11PM +0200, Szabolcs Nagy wrote: > * Morten Welinder [2014-04-07 10:13:26 -0400]: > > The frequency of this problem is something like 1 in 5e6. > > Observations: > > > > * I only seem to be able to trigger it for %g even though all my > > samples print in "e" form. > > > > * The numbers are all roughly the same size: 1e15 > > > > * The numbers are all integers ending in 05. (Except two cases > > where the 05 is followed by zeros.) > > > > * The precision is always just below the value that would have > > make an exact representation. > > printf("%.12g\n", 1000000000005.0); > printf("%.11g\n", 500000000045.0); > printf("%.11g\n", 275000000025.0); > > prints > > 1.00000000001e+12 > 5.0000000005e+11 > 2.7500000003e+11 > > in fmt_fp > if (x || d+1!=z) { > long double round = CONCAT(0x1p,LDBL_MANT_DIG); > long double small; > if (*d/i & 1) round += 2; > if (x else if (x==i/2 && d+1==z) small=0x1.0p0; > else small=0x1.8p0; > ... > > here > i == 10 > x == *d%i == 5 == i/2 > but the half-way case does not trigger because z-d == 2 instead of 1 > and z[-1] == 0 which should not happen here Thanks for making the analysis needed to fix this. I've committed the fix. Hopefully this is the last of such bugs. Perhaps we should devise a stress test with random inputs and assertions to search for other bugs. Some ideas that come to mind: 1. Printing both full-precision and roundings to 0...25 places and asserting that the roundings are correct based on the full-prec. 2. For full precision outputs, asserting that sum(digits)%3 == numerator%3 for diadic rationals of the form numerator/denominator. 3. Round tripping with strtold. 4. Assertions about formatting such as lack of trailing zeros. ...? Rich