lua-users home
lua-l archive

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


On Jun 5, 2011 5:47 PM, "Martin Guy" <martinwguy@gmail.com> wrote:
>
> On 5 June 2011 23:43, Dirk Laurie <dpl@sun.ac.za> wrote:
> > On Sun, Jun 05, 2011 at 10:44:24PM +0200, Henk Boom wrote:
> >>
> >> If I understand the Sebastian properly, he is not complaining that lua
> >> doesn't differentiate between them, but that these two numbers which
> >> are not normally differentiated do not print the same:
> >>
> >> On Sun, Jun 05, 2011 at 12:47:39PM +0200, Sebastian wrote:
> >> > The problem of the output from -0 is that -0 does'nt look good.
> >> > The user (for example a wow addon user) can be disoriented,
> >> > when he read the addon output: "(-20) * 0 = -0.
> >>
> >
> > That, too, is not a bug, it's a feature.  Reuben's point applies:
> > Lua relies on the host C compiler and its libraries.  Since C converts
> > a negative zero to the string -0, so does Lua.
>
> main()
> {
>    printf("%f\n", -20.0 * 0.0);
> }
> prints
> -0.0000
> with GNU libc.
>
> > It is a good thing, not a bad thing.
>
> It's bizarre to expose such an esoteric implementation detail so
> baldly. Only an IEEE validation suite specialist would be worried
> about seeing the "-"; for normal people, the "-" contains no
> information and is just surprising/worrying.
>
> I suppose the workaround is to redefine tostring so that it checks for
> numeric x == 0 (which also succeeds when x has the IEEE value -0) and
> return "0" in that case.
>
> print(-0)
> -0
>
> do
>  local old = tostring
>  tostring = function(x) return (x == 0) and "0" or old(x) end
> end
>
> print(-0)
> 0
>
> It's still bizarre though, whether glibc thinks so or not.
> tostring() is not constrined to follow what glibc does. Already it
> does a range of different things according to the type and value of x.
> In this case, printing "0" is obviously the right thing to do.
>
> There may be an argument that printing -0.0000001 to five digits'
> precision could return -0.00000 but even that is debatable.
>
>    M
>

It is a bit strange, but I don't see the harm in showing a meaningless minus sign? The alternative would be modifying tostring to do an additional check, and that has its own issues: adding code to "fix" a harmless "bug", not "fixing" it completely (what about string.format and other places a -0 could sneak through?) causing inconsistency, formatting (how to display -0.0000000000001?), and people inevitably complaining when it *doesn't* show -0, to name a few.

On Jun 5, 2011 5:47 PM, "Martin Guy" <martinwguy@gmail.com> wrote: