lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


David Kastrup wrote:
That's all pretty useless since there is no standard behavior for
(int)x, and besides, (int)x may overflow.

If we disregard overflow, of course it is easiest to just do

double floor(double x)
{
  double y = (double)(int)x;
  return y > x ? y-1.0 : x;
}

You're right with regards to overflow. Your version of floor is however buggy. Here is a shortened version:

double floor(double x)
{
  double y = (double)(int)x;
  return (x >= 0 || x == y) ? y : y-1;
}

--
Shmuel