lua-users home
lua-l archive

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


> Beyond the issue of finite floating point precision, there is also the
> non-trivial issue of converting losslessly between strings and floats.
...
> I would have expected a conversion from "0.1" to double and back
> to string to be lossless, even when printing extra digits.

The nontriviality is the same as converting noiselessly between inches
and centimetres.  Suppose we're allowed to go down to 1/256 of an
inch or 0.01mm.  Almost always, since those units are very close,
the conversion back and forth is lossless.  But only almost, since
"very close" is not "the same".

When one says "%.17f", the unit is 10^(-17), whereas the unit for
numbers between 0.125 and 0.25 in IEEE double is about 2.5 times that.

> Out of curiosity, how does Lua perform its float<->string conversions?
...
> Does Lua depend on (often poor) native sprintf implementations? or does it
> use some variant of David Gay's fp routines?

In lconfig.h:

#define lua_number2str(s,n)     sprintf((s), LUA_NUMBER_FMT, (n))
...
#define lua_str2number(s,p)     strtod((s), (p))

Which means, Lua does depend on your C library, but you are just two
defines away from replacing it by your own choice.

> I admit a large degree of ignorance here, but I have read the papers
> cited below.

Those papers were very topical and necessary when they appeared back
in the early 90's when C89 wasn't even called that yet and very few
C compilers were standard-compliant.  Did you see David Gay's reference
to VAX arithmetic?

Nowadays even you-know-who has quite a decent compiler, even if it is
"um saco mesmo" in comparison to Gnu C.