lua-users home
lua-l archive

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


> I have a question:
> 
> Is there any problem defining x//y := math.floor(x/y) for all cases
> where either x or y are not integral?

No, just it would be a different operations. (Currently, // is integer
division, not a division with integer results.)

Consider the following examples (assuming your proposal):

  x = (1 << 60) + 2
  print(x // 2)     --> 576460752303423489
  print(x // 2.0)   --> 576460752303423488

  x = (1 << 62)
  print(x // 3)     --> 1537228672809129301
  print(x // 3.0)   --> 1537228672809129216

In both cases, the result is an integer, there was no overflow at the
computation, but the results are different. A key point in Lua is that
any operation performed on integers or floats give exactly the same
result, unless either an operand or the result cannot be expressed in
the given representation (integer or float).

But that is not a big problem. We could as well define it to be x//y
:= math.floor(x/y)+0.0 (that is, giving a float as result for float
operands). It is a matter of choice.

-- Roberto