lua-users home
lua-l archive

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


On 14/11/2012 07:27, Dirk Laurie wrote:
2012/11/14 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
There
will be two divisions: float division (the usual '/') always have
a float result, integer division ('//'??) always have an integer
result.

A new operator requires a new metamethod. Suggestion:

__divmod(op1, op2)
    Returns two results: quotient and remainder.

Semantics exactly as for other functions with multiple return values.
Only the first return value is kept in expressions, list constructors
except for the last item, etc.

This definition screams for the symbol `/%`.

q, r = 42 /% 10  --> 4,2

Dirk

I have no idea whether it can be that useful to override /%. However, since the metafield machinery exist...

For the operator /% itself, I vote +++. Lua returning several results, let us use it. As an aside, having this operator would help avoiding confusion and errors, I guess, even if there also is a simple quotient op, as they would be presented together in introductory material. Note:
       6.9 /% 2.2 --> 3, 0.3
(From a thread 6.9 m long, I can get 3 smaller threads of 2.2 m, and 30 cm remain.)

If there ever is an int div without quotient, the sign '//' looks fine as well (indeed, '\' is unavailable).

Denis