From owner-freebsd-standards@FreeBSD.ORG Mon Jan 19 20:34:13 2004 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 C760116A4CE for ; Mon, 19 Jan 2004 20:34:13 -0800 (PST) Received: from mailout2.pacific.net.au (mailout2.pacific.net.au [61.8.0.85]) by mx1.FreeBSD.org (Postfix) with ESMTP id EEE6243D2F for ; Mon, 19 Jan 2004 20:34:11 -0800 (PST) (envelope-from bde@zeta.org.au) Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au [61.8.0.86])i0K4YAtd001992; Tue, 20 Jan 2004 15:34:10 +1100 Received: from gamplex.bde.org (katana.zip.com.au [61.8.7.246]) i0K4Y8fe001733; Tue, 20 Jan 2004 15:34:09 +1100 Date: Tue, 20 Jan 2004 15:34:09 +1100 (EST) From: Bruce Evans X-X-Sender: bde@gamplex.bde.org To: Steve Kargl In-Reply-To: <20040119221314.GA65652@troutmask.apl.washington.edu> Message-ID: <20040120145749.I2417@gamplex.bde.org> References: <20031129000133.GA30662@troutmask.apl.washington.edu> <20031129163105.GA32651@troutmask.apl.washington.edu> <20031201182219.O4431@gamplex.bde.org> <20031202091936.I8778@gamplex.bde.org> <20040119221314.GA65652@troutmask.apl.washington.edu> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-standards@FreeBSD.ORG 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: Tue, 20 Jan 2004 04:34:13 -0000 On Mon, 19 Jan 2004, Steve Kargl wrote: > On Mon, Jan 19, 2004 at 01:31:42PM -0800, David Schultz wrote: > > > > double > > round(double x) > > { > > double result; > > fp_except_t fe; > > > > fe = fpresetsticky(0); > > result = rint(x); > > if (fpgetsticky() & FP_X_IMP) { > > result = copysign(floor(fabs(x) + 0.5), x); > > fe |= FP_X_IMP; > > } > > fpresetsticky(fe); > > return (result); > > } > > > > Does this seem reasonable? > > Don't you need > > rnd = fpsetround(FP_RN); /* Set to known rounding mode */ > result = rint(x); > fpsetround(rnd); /* Reset to whatever the user had */ > > to ensure that rint() rounds to an expected value. rint() may (and apparently does in at least our i386 implementation) raise the inexact exception when it does any rounding. So if rounding is needed then it is needed only in the inexact exception case, and David's point is to avoid it there. > int main(void) { > double u, v, x; > x = 0.5; > fpsetround(FP_RM); > u = round(x); > fpsetround(FP_RP); > v = round(x); > /* Does u equal v ? */ > } Testing on i386's shows that it does :-). Bruce