lua-users home
lua-l archive

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


2017-06-25 12:27 GMT+02:00 John Hind <john.hind@zen.co.uk>:

> However depending on NaN being the *only* value that is not equal to itself
> arguably fails my stipulation of robustness, and intuitive it ain't! Is
> 'minus infinity' equal to itself? Is 'minus infinity' equal to 'plus
> infinity' even? (remembering we we not relying on IEEE conformance). I think
> we enter deep philosophical waters!

Not philosophical. IEEE 754 is a specification. It says exactly what the bit
patterns are and what they mean.

function hex(x)
if math.type(x)=='float' then
return ("%016x"):format(string.unpack("L",string.pack("d",x)))
end
end

For example:

inf=1/0
hex(inf) --> 7ff0000000000000
hex(-inf) --> fff0000000000000
NaN=0/0
hex(NaN) --> fff8000000000000

However, whereas inf and -inf have unique bit patterns, and therefore
-inf does not equal +inf, NaN doesn't. NaN literally is equivalent to "any
bit pattern not conforming to the specifications for a valid double".
If the thirteen hex digits after 'fff' or '7ff' are anything except zeros,
it counts as a NaN.  (That's why it was possible for Lua 5.2 to have
a "NaN trick": those hex digits were exploited.)