lua-users home
lua-l archive

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



On 2-Mar-05, at 12:28 PM, Roberto Ierusalimschy wrote:

What is wrong with the following code?

  math.nan = 0/0
  math.inf = 1/0

If you've compiled Lua with integer Numbers rather than floating point
ones, this will crash and burn, probably.

Is there any reason why anyone would use such library in that case?

If they haven't, then the assignment will crash and burn, which is not desirable either.

However, I do tend to use "a" math library (called "math") even in integer installs; it contains compatible integer implementations of the non-floating point dependent math library standard functions (random, min, max, mod, and a few others).

It is not clear to me under what circumstances you might actually want to use a math.nan constant since you cannot actually compare it to anything, but it would be useful to have the math.inf constants, even with integer arithmetic. (They're convenient initial values for max and min, for example).

With that sort of use case in mind, one could argue that math.infinity (and possibly math.minusinfinity) might be useful additions to the (specification of) a standard math library interface.

math.nan is rather a different category, though, since technically math.nan ~= math.nan, which renders the whole thing somewhat academic. On the other hand, if a nan had a boolean coercion to false, you could write, for example:

a = assert(math.sqrt(x), "Negatives only have round roots")