2002-07-29 11:00:10 +08:00
|
|
|
#include <math.h>
|
2004-04-22 12:38:24 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2002-06-13 18:20:48 +08:00
|
|
|
|
2003-01-11 18:52:20 +08:00
|
|
|
long long
|
2004-04-22 12:38:24 +08:00
|
|
|
llround (double x)
|
|
|
|
{
|
|
|
|
/* Add +/- 0.5, then round towards zero. */
|
|
|
|
double tmp = trunc (x + (x >= 0.0 ? 0.5 : -0.5));
|
|
|
|
if (!isfinite (tmp)
|
|
|
|
|| tmp > (double)LONG_LONG_MAX
|
|
|
|
|| tmp < (double)LONG_LONG_MIN)
|
|
|
|
{
|
|
|
|
errno = ERANGE;
|
|
|
|
/* Undefined behaviour, so we could return anything. */
|
|
|
|
/* return tmp > 0.0 ? LONG_LONG_MAX : LONG_LONG_MIN; */
|
|
|
|
}
|
|
|
|
return (long long)tmp;
|
2002-06-13 18:20:48 +08:00
|
|
|
}
|