lua-users home
lua-l archive

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


2007/3/13, Miles Bader <miles@gnu.org>:
"Jérôme VUARAND" <jerome.vuarand@gmail.com> writes:
>> *Microseconds*
>
> Since numbers in Lua are floating point you should use plain units
> without multiplier, that is seconds in that case.

Doesn't this run into problems with precision if "float" is the basic
number type?  Actually if numbers are floats it seems that any
unix-like date representation will have problems...

The problem already is also present with doubles. I made the following
quick test, and it appears that about half numbers between one and one
million get rounding errors with the conversion we are talking about
(an integer in microseconds to a float in seconds).

me@home:~$ cat test.c
#include <stdio.h>

int main()
{
       int i, j;
       float f;
       double d;
       int ferrors = 0;
       int derrors = 0;
       for (i=0; i<1000000; ++i)
       {
               f = (float)i/1000000.f;
               d = (double)i/1000000.;
               j = f*1000000.f;
               if (i!=j)
                       ++ferrors;
               j = d*1000000.;
               if (i!=j)
                       ++derrors;
       }
       printf("Microseconds losing precision as floats: %d (%f%%)\n",
ferrors, (float)ferrors/10000.f);
       printf("Microseconds losing precision as doubles: %d
(%f%%)\n", derrors, (float)derrors/10000.f);
       return 0;
}

me@home:~$ make test
cc     test.c   -o test
me@home:~$ ./test
Microseconds losing precision as floats: 499956 (49.995600%)
Microseconds losing precision as doubles: 499711 (49.971100%)
me@home:~$