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

Steve Kargl sgk at troutmask.apl.washington.edu
Fri Nov 28 16:01:35 PST 2003


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,

#include <math.h>

float roundf(float x) {
	float t;
	if (x >= 0.0) {
		t = ceilf(x);
		if ((t - x) > 0.5) t -= 1.0;
		return t;
	} else {
		t = ceilf(-x);
		if ((t + x) > 0.5) t -= 1.0;
		return -t;
	}
}


-- 
Steve


More information about the freebsd-standards mailing list