Re: round not in math.h?

"Richard A. O'Keefe" <[email protected]> Tue, 20 Sep 2005 10:38:23 +1200 (NZST)
Newsgroups gmane.comp.lang.eiffel.smalleiffel
Message-ID <[email protected]>
Wolfgang Jansen <[email protected]> wrote:
	I think `round' is quite meaningless in C, simply use
	double->int conversion instead:

Unfortunately, double -> int conversion in C has always been defined
to TRUNCATE, not round.

Rounding can be done thus:

    #include <limits.h>

    int round(double x) {
	if (x >= 0.0) {
	    if (x >= INT_MAX+0.5) abort();
	    return (int)(x+0.5);
	} else {
	    if (x <= INT_MIN-0.5) abort();
	    return (int)(x-0.5);
	}
    }

where abort() should be replaced by some better error-reporting method.