lua-users home
lua-l archive

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

On Mon, Jun 06, 2011 at 09:25:13AM +0200, David Kastrup wrote:
> Of course they can come shorter and simpler: "as is".  As far as
> semantic rules to rely on go, it means quite the same.
> You have to read the underlying code _and_ the C language standard _and_
> the C implementation details to figure out the actual behavior for your
> implementation.
If something is implementation-dependent, then the fact is usually
documented somewhere in the standard, and in that case "as is" is
not reliable even from one gcc to the next on the same architecture.

> dak@lola:~/tex/knopf/out$ lua
> Lua 5.1.4  Copyright (C) 1994-2008, PUC-Rio
> > return math.max(0,-0)
> 0
> > return math.min(0,-0)
> 0
> > return math.max(-0,0)
> -0
> > return math.min(-0,0)
> -0
> > return math.abs(0)
> 0
> > return math.abs(-0)
> 0
> >
> Please tell me how the C standard would prepare you for guessing the
> outcome of the above without actually reading the underlying code.
Easy.  I don't even need the C standard.  The IEEE 754 standard is
enough.  The Wiki article I quoted is enough, if you trust a Wiki.  
You have to know:
    1. All zeros compare equal.
    2. The absolute value of a number has positive sign.

The obvious algorithm for computing the maximum using the smallest
possible number of comparisons is:

function max(a,...)
   for _,b in ipairs{...} do if b>a then a=b end end
   return a

So max(a,b) returns 'a' when called with (-0,0).  It seems a fair guess 
that math.max will not be based on an algorithm that requires an 
unnecessary extra comparison or start elsewhere than at the beginning,
which is what you would need to make max(-0,0) evaluate to 0.

The reason why the original post annoyed me was that the word 'bug'
was being applied to Lua when the behaviour objected to is perfectly 
in line with the IEEE 754 standard.