Implementing C99's roundf(), round(), and roundl()

Steve Kargl sgk at troutmask.apl.washington.edu
Mon Jan 19 14:13:19 PST 2004


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.

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 ? */
}

BTW, thanks for working on this issue.

-- 
Steve


More information about the freebsd-standards mailing list