[geos-devel] Is this symmetric rounding algorithm?

Mateusz Łoskot mateusz at loskot.net
Fri Feb 24 09:23:06 EST 2006


Hi,
As strk suggested, I tried to google for a while and learn about 
(a)symmetric rounding algos.
Here I put round_vc functionwhich is a changed version of rint_vc 
function. IMHO round_vc does symmetring rounding, but I'd like someone 
to check :-)

/*
Symmetric Rounding Algorithm
*/
double round_vc(double val)
{
	double n;
	double f = fabs(modf(val, &n));
	if (val >= 0) {
		if (f < 0.5) {
			return floor(val);
		} else if (f > 0.5) {
			return ceil(val);
		} else {
			return (n + 1.0);
		}
	} else {
		if (f < 0.5) {
			return ceil(val);
		} else if (f > 0.5) {
			return floor(val);
		} else {
			return (n - 1.0);
		}
	}
}

/////////////////////////////////////////////////////
// TEST
#include <math.h>
#include <stdio.h>
int main()
{
	double x = 5.5;
	double x1 = -5.5;
	double y = 6.5;
	double y1 = -6.5;

	printf("x:   %f \n", x);
	printf("x1: %f \n", x1);
	printf("y:   %f \n", y);
	printf("y1: %f \n", y1);

	printf("--- rint -------------------\n");
	printf("x:   %f \n", rint_vc(x));
	printf("x1: %f \n", rint_vc(x1));
	printf("y:   %f \n", rint_vc(y));
	printf("y1: %f \n", rint_vc(y1));

	printf("--- round ------------------\n");
	printf("x:   %f \n", round_vc(x));
	printf("x1: %f \n", round_vc(x1));
	printf("y:   %f \n", round_vc(y));
	printf("y1: %f \n", round_vc(y1));

}

-- 
Mateusz Łoskot
http://mateusz.loskot.net



More information about the geos-devel mailing list