lua-users home
lua-l archive

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


On Wed, Dec 14, 2011 at 10:49 AM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> I think the main difficulty is reproducibility of floating-point calculations

Most platforms now use IEEE 754 and so should yield reproducible
floating-point calculations, but perhaps you should use "make ansi"
to avoid all smart macros in luaconf.h.

Note that, unlike C code, Lua evaluates arithmetic expressions by always
storing intermediate results in memory and I think this alone should
ensure reproducibility, provided lua_Numbers have the same precision.
(In C intermediate results might be held in higher precision in the FP
processor.)

This is not true. There are still subtle issues with e.g. x87 even when all results are stored to memory and the significand is set to 53-bit precision. Results are rounded twice, first to 15-bit exponent then to 11-bit exponent when stored in memory. This can give the wrong result for multiplications and divisions where the result is subnormal (http://en.wikipedia.org/wiki/Rounding#Double_rounding). It's possible to fix even for x87, but quite expensive. On x86 it's best to make sure the compiler uses only SSE/SSE2 if you can. Sadly, you can't ensure that in all compilers (Visual C++ for example).

Then you have the whole problem of widely differing math library implementations. For example, I've noticed that even ceil is sometimes incorrect in Visual C++, and trigonometric functions are hopeless. The only solution there is to use some reproducible math library, like fdlibm or cephes.

/Erik