lua-users home
lua-l archive

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

Shmuel Zeigerman <> writes:

> Luiz Henrique de Figueiredo wrote:
>> See for a sample
>> implementation with pointers to whatever is missing.
> The implementation of floor works incorrectly for negative x.
> The following seems to work OK:
> double floor(double x)
> {
>   double y;
>   if (x >= 0) return (double)(int)x;
>   y = (double)(int)(x-1);
>   return x - y == 1 ? x : y;
> }

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;

However, disregarding overflow seems imprudent.  Perhaps one should use
(long long) instead of (int).  That should have a somewhat better chance
of working.  _If_ long long is defined.

David Kastrup