lua-users home
lua-l archive

Re: Lua minus null (-0)

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


Dirk Laurie <dpl@sun.ac.za> writes:
> On Thu, Jun 09, 2011 at 11:15:00AM +0200, David Kastrup wrote:
>> > Maybe _because_ it's hard to distinguish them otherwise? :-)
>> > Anyway, this is a question to ask of the people who define
>> > the behaviour of C formatting.
>> 
>> C has a separate integer type.
>
> The design decision to be made by the Lua designers is basically
> what to put in luaconf.h. At present, it says:
>
> #define LUA_NUMBER_FMT "%.14g"
> #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
>
> Are you (who obviously are arguing for the sheer joy of it) or any
> of the people who are genuinely perplexed by -0, seriously suggesting
> that Luiz should replace these two clear, effective lines by some
> tortuous coding involving a test on n? Plus the inevitable need
> for explaining why Lua behaves differently from C in this case?
Huh? Lua is _not_ C. The above definitions are _internals_ of Lua. C
does not even _have_ a generic tostring function, so it is nonsensical
to talk about "Lua behaves differently from C". As opposed to a %f
format string, the user has _not_ specified that he wishes an explicit
C-like floating-point formatting to occur.
By the way, I also don't consider it a particularly endearing language
feature that %d and %x formatting produces garbage when you are leaving
the range of C integers but are still well within the domain of Lua
integers.
That is not a language feature, but a bug, since the size of C integers
should not be relevant for a language not _having_ integers or an
interface to them. A bug tolerated in order to save code size.
> All that, when all it takes for the Lua user who wants to print 0
> instead of -0, to put at the top of each Lua program he writes
>
> debug.setmetatable(0, {__tostring = 
> function(n) local s=string.format("%.14g",n) 
> if s=='-0' then s='0' end return s 
> end})
>
> Dirk
lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> debug.setmetatable(0, {__tostring = 
>> function(n) local s=string.format("%.14g",n) 
>> if s=='-0' then s='0' end return s 
>> end})
> print(-0)
0
> print("Oh"..-0)
Oh-0
> 
-- 
David Kastrup

AltStyle によって変換されたページ (->オリジナル) /