lua-users home
lua-l archive

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

On Fri, Aug 21, 2015 at 09:03:19AM -0300, Soni L. wrote:
> On 21/08/15 08:54 AM, Philipp Janda wrote:
> >Am 21.08.2015 um 13:43 schröbte Liam Devine:
> >>Lua 5.3 returns positive infinity for a negative zero denominator, which
> >>is a change from 5.1, 5.2 and LuaJIT. Unless I am missing something,
> >>this does not seem to be fixed in 5.3.1 or listed on the bug page[1].
> >>
> >>liam@dmail00:~$ lua53
> >>Lua 5.3.0  Copyright (C) 1994-2015, PUC-Rio
> >>>return 1/-0
> >>inf
> >>>os.exit()
> >
> >There is no negative zero integer. Try `1/-0.0`.
> >
> >Philipp
> >
> >
> >

In C, at least, where signed magnitude and ones' complement representations
are used for signed integer, negative 0 is permitted to be a trap

	... Which of these applies is implementation-defined, as is whether the
	value with sign bit 1 and all value bits zero (for the first two),
	or with sign bit and all value bits 1 (for ones' complement), is a
	trap representation or a normal value. In the case of sign and
	magnitude and ones' complement, if this representation is a normal
	value it is called a negative zero. C11 (N1570)

In other words, just because the machine uses ones' complement doesn't mean
there's a negative 0. Even more importantly, even where negative 0 exists
arithmetic expressions only produce negative 0 when one of the operands is
already negative 0.

	If the implementation supports negative zeros, they shall be
	generated only by:

	— the &, |, ^, ~, <<, and >> operators with operands that produce
	such a value;

	— the +, -, *, /, and % operators where one operand is a negative
	zero and the result is zero;

	— compound assignment operators based on the above cases.

	It is unspecified whether these cases actually generate a negative
	zero or a normal zero, and whether a negative zero becomes a normal
	zero when stored in an object.

	C11 (N1570)

So you can't produce it without either using a literal "-0" in your code, or
perhaps via conversion from a floating point value.

I post this only because your comment piqued my interest. I didn't know any
of this beforehand, I swear ;)