Floor/int division

It would be very convenient if we could have the floor division operator //

As a Roblox developer right now every time we want to do floor division we have to inline the function:

local function floordivide(n,m)
return math.floor(n/m)
end
4 Likes

Isn’t this pretty much the same as math.modf(n/m) ?

Or even better: (n-n%m)/m (for positives)

1 Like

Isn’t Lua supposed to be simple and lightweight? The code you’ve provided does exactly what you want in a single line of code. I don’t see how this feature would fit in with the principles of Lua at all.

Floor division (or more accurately, integer division) exists in Lua 5.3 because of the integer subtype and it takes the form //. What with how we’re getting a bit library (which requires integers) and how we’re getting first-class support for some functions in the new VM, a built-in function for this would be helpful since it’ll be a more common operation and it won’t ever be as fast to write it in Lua as it will be to have it built-in.

10 Likes

Perhaps instead of adding new operators, the new VM could instead be optimized to give math.floor the same performance as an operator.

6 Likes

This could possibly also apply to these four other functions too:

math.ceil
math.rad
math.deg
math.pow (idk who uses this, but consistency?)
3 Likes