Re: round not in math.h?
"Richard A. O'Keefe" <[email protected]> Tue, 20 Sep 2005 14:15:54 +1200 (NZST)
| Newsgroups | gmane.comp.lang.eiffel.smalleiffel |
|---|---|
| Message-ID | <[email protected]> |
Wolfgang Jansen <[email protected]> wrote: I have to improve the program splitter as follows: EIF_REAL_64 x; EIF_INTEGER_64 n; int sign = (x < 0 ? -1 : 1); x = fabs(x)+0.5; if (x<ULLONG_MAX) { n = x; x = n; } if (sign<0) x = -x; There is a much simpler and more portable way. There is a round() function in C99; there wasn't in C89. But C89 does have floor() and ceil(), and the value of round() must always be equal to one of those. And indeed we can use the "add a half with same sign then chop back towards zero" technique. So #include <math.h> double round64(double x) { return x < 0.0 ? ceil(x-0.5) : floor(x+0.5); } does the job easily.