[LCP]Rounding questions
Chuck Martin
nrocinu at myrealbox.com
Thu Sep 5 22:26:05 UTC 2002
On Thu, Sep 05, 2002 at 11:41:36AM +0100, Vincent Penquerc'h wrote:
> > That returns "390.000000", and is similar (but not exactly
> > the same) to
> > what I was doing before that had a strange bug (apart from
> > the fact that
> > that would have to be modified to work with negative numbers).
>
> Really ? I had a quick thought about it, then realized that it
> was fine. You want the nearest number to the one you give, right ?
Yes, but if the fractional part of a number is .5, it's equally distant
from both the next integer larger and the next integer smaller. In
such a case, there are several ways to round. According to the man
pages, rint() will always round to whichever of the two integers is
even, and round() will round away from zero. For a negative number,
this would mean subtracting 0.5 and using ceil() instead of adding
0.5 and using floor(), so round(-3.5) should return -4, while your
method would return -3, which is another way of rounding, but doesn't
have a separate function to do it (probably because it's trivial to
add 0.5 and use floor()).
Chuck
More information about the linuxCprogramming
mailing list