From owner-freebsd-standards@FreeBSD.ORG Mon Dec 1 00:05:25 2003 Return-Path: Delivered-To: freebsd-standards@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 33F7216A4CE; Mon, 1 Dec 2003 00:05:25 -0800 (PST) Received: from mailman.zeta.org.au (mailman.zeta.org.au [203.26.10.16]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2630B43F93; Mon, 1 Dec 2003 00:05:22 -0800 (PST) (envelope-from bde@zeta.org.au) Received: from gamplex.bde.org (katana.zip.com.au [61.8.7.246]) by mailman.zeta.org.au (8.9.3p2/8.8.7) with ESMTP id TAA09987; Mon, 1 Dec 2003 19:05:18 +1100 Date: Mon, 1 Dec 2003 19:05:18 +1100 (EST) From: Bruce Evans X-X-Sender: bde@gamplex.bde.org To: David Schultz In-Reply-To: <20031130213951.GA37082@VARK.homeunix.com> Message-ID: <20031201182219.O4431@gamplex.bde.org> References: <20031129000133.GA30662@troutmask.apl.washington.edu> <20031129163105.GA32651@troutmask.apl.washington.edu> <20031130213951.GA37082@VARK.homeunix.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-standards@freebsd.org cc: Steve Kargl Subject: Re: Implementing C99's roundf(), round(), and roundl() X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Dec 2003 08:05:25 -0000 On Sun, 30 Nov 2003, David Schultz wrote: > On Sat, Nov 29, 2003, Steve Kargl wrote: > > On Sat, Nov 29, 2003 at 12:09:11AM -0800, David Schultz wrote: > > > On Fri, Nov 28, 2003, Steve Kargl wrote: > > > > Can the math functions round[fl]() be implemented in > > > > terms of other math(3) functions and still conform to the > > > > C99 and POSIX standard? For example, > > > > [code moved later] > > > > > > This looks correct to me at first glance, modulo possible problems > > > with overflow. ... > > > > I don't undrestand your overflow comment. ceil[f]() can return Inf > > and nan, but in those cases round[f]() should also return Inf and nan. > > The two operations, (t-x) and (t+x), should yield a value in the > > range [0,1). I'll submit a PR with a man page. > > The concern was that ceil() could round a number up to infinity > when round() is supposed to round the number down. But now that I > think about it at a reasonable hour, this concern is clearly > bogus. In base two floating point representations, there isn't > enough precision to get numbers that large with nonzero fractional > parts. It's not completely obvious. I thought of it soon but wondered if I thought of all the cases. Steve's remark about Infs and NaNs points to possible problems: > > > > #include > > > > > > > > float roundf(float x) { > > > > float t; > > > > if (x >= 0.0) { Suppose x is a NaN. Then it will compare strangely with everything and we won't get here. > > > > t = ceilf(x); > > > > if ((t - x) > 0.5) t -= 1.0; > > > > return t; > > > > } else { We get here for NaNs. > > > > t = ceilf(-x); And we really shouldn't do arithmetic on the NaNs. ceilf() should return its arg for a NaN, but it's not clear what happens for -x. Well, I checked what happens starting with the QNaN x= 0.0 / 0.0. Almost everything is broken: - gcc miscomputes 0.0 / 0.0 at compile time. It gives a positive NaN, but the npx would give a negative NaN ("Real Indefinite" = the same one except with the opposite sign). - gcc invalidly optimizes -x by ORing in the sign bit (even without -O). It should do the same as the npx (which is to not change anything). - printf() doesn't print the sign bit for NaNs. - C99 requires printf() to misspell "NaN" as "nan" > > > > if ((t + x) > 0.5) t -= 1.0; > > > > return -t; Now if x is NaN, t is NaN and we do lots of arithmetic on it. I think gcc's invalild optimizations cancel out so NaNs will be returned unchanged. This is not clear :-). > > > > } > > > > } All the other corner cases need to be checked. It's possibly to check all 2^32 cases for floats (once you know the correct results). Other things to check: setting of exception flags. I'm not sure if the settings by ceil() are the right ones and the only ones. Bruce