Add a // operator for floored division to Roblox Lua

As a Roblox developer, it is currently a bit annoying to have to do:

math.floor(7/3) --> 2
or 
math.floor(8/2) --> 4
or
math.floor(9/5) --> 1

When in other languages you can simply do:

7//3 --> 2
or 
8//2 --> 4
or
9//5 --> 1

Having an operator for floored devision would simply make programming a slight bit more convenient and also will potentially help new developers coming from backgrounds in other programming languages have an easier transition to Roblox lua.

27 Likes

Should x // 0 error zero division error or return Infinity?
Should there be an __idiv metamethod as well?

Other languages? This isn’t actually common in my point of view. The only language I know that uses this syntax is Python 3+ and Lua 5.3+.
In C, C++, C#, Java, Rust, etc (mostly languages that uses a syntax based on C) it’s the / operator. (though it’s not really floor division but truncate division) Additionally in those languages, // is actually a syntax for comment.
ECMAScript don’t have this type of division for doubles.

7 Likes

Keyword potentially. I think // is not so common, actually,

Shorthand is generally less obvious to new learners than explicit functions, and I would preferentially use the math library to do this particular type of flooring even if that operator existed, just because I need to share legible code structures with people who have greatly differing familiarity with Lua, and programming broadly. I also don’t regularly encounter the need to floor division, and I’ve been around the block… so I don’t think this new operator would be very useful to me. :frowning:

Also I want to ask, why isn’t there an equal desire for a shorthand operator to get the math.ceiling of the quotient? Would another operator exist for that? If so what would it look like?

4 Likes

// was added in Lua 5.3 because of the addition of the integer subtype. Luau won’t add an integer subtype (many incompatibilities), so this operator makes less sense to add. Just being shorthand for math.floor(a/b) isn’t enough reason for adding a new operator (change in syntax, new opcodes, new metamethods), especially when the shorthand syntax would be infrequently used.

Since there wouldn’t be an integer subtype, presumably it would result in x/0 (NaN if x is NaN or 0, +inf if x > 0, -inf if x < 0). This would be the same as the behavior of Lua 5.3+ with floats in floor division. __idiv should be added too if this is added, to make the behavior in line with Lua 5.3+.

6 Likes

:pray: :pray: :pray:

This. Programmers typically aren’t bottlenecked by how many lines of code they can type but rather how quickly they can design & structure from start to end.

And when you might be completing a task in an existing codebase with thousands of lines, the bottleneck becomes how fast you can read what it’s currently doing to understand where your changes should be implemented. So shorthand that isn’t frequently used by everyone serves as a majority negative addition rather than a positive one.

tl;dr nothing wrong with the API but overall doesn’t seem helpful

3 Likes

FWIW this is covered on Compatibility - Luau already.

8 Likes