[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: LUA_COMPAT_FLOATSTRING
- From: Nagaev Boris <bnagaev@...>
- Date: Tue, 31 Mar 2015 19:59:13 +0000
On Tue, Mar 31, 2015 at 6:34 PM, Matthias Kluwe <email@example.com> wrote:
> When converting a number object to a string, luaO_tostring gets called,
> which adds a "float mark" to floating point numbers that come out of
> sprint looking like an int, to the effect that "0" becomes "0.0".
> This can be turned off by defining LUA_COMPAT_FLOATSTRING.
> luaconf.h says: "This macro is not on by default even in compatibility
> mode, because this is not really an incompatibility."
> I slightly disagree, as it introduces an incompatibility when using
> different locales for output.
> This example (expecting decimal comma representation)
> print( os.setlocale 'german' )
> print( 0.0, 0.1 )
> 0 0,1
> using Lua 5.2.4 and
> 0.0 0,1
> with Lua 5.3.0.
> I had to learn that the hard way yesterday when my output containing
> (floating point) zeros got severely borked. I *know* that using
> unformatted output after switching to 5.3 means asking for some trouble,
> but adding hard-coded '.0' to the number does not look like a sensible
> default to me.
Lua 5.3 manual says following :
> The conversion of a float to a string now adds a .0 suffix to the result if it looks like an integer. (For instance, the float 2.0 will be printed as 2.0, not as 2.) You should always use an explicit format when you need a specific format for numbers.
> (Formally this is not an incompatibility, because Lua does not specify how numbers are formatted as strings, but some programs assumed a specific format.)
If I understand correctly, it was not documented in Lua < 5.3, how
numbers are converted to strings. That is why this change is not
considered to be an incompatibility. When undefined behavior becomes
defined behavior, it is not called an incompatibility.