0_X_0_e_0 = 224? Exploring Weird Number Notations in Luau

Hello!

This is not really a ‘tutorial’? This is just a brief dive into how cursed numbers can be in Luau (not much research was done when writing this topic to be honest). It should never be used for any work (obviously), so just treat these as fun facts. I think tutorial is the best topic that fits in this category, please tell me if I’m wrong.

This blog is pretty long, and I may not be that good at English, so please tell me if I can do anything to improve the blog.


Number notation in Luau

Well, to be clear on the cursed stuffs, we need to know the basics first. To make sure we are on the same page, if you’re not clear about any of these, feel free to click in:

Decimal numbers (10)

We have the decimal numbers (aka base-10 numbers), for example: 10, -22.4, etc. Which is the standard way of writing numbers.

Scientific notation (3e2)

For scientific notation, for example: 3e2 = 300, or another way of writing this is 3e+2 = 300. The e2 is just multiplying by 100. You can also use 3e-2 as representing 3 / 100 = 0.03. You can also write it as 3E2, 3E+2 or 3E-2 if you wish.

For the sake of consistency, I will use e, e+ and e- for the rest of the topic.

Hexadecimal numbers (0x3F)

For hexadecimal numbers (aka base-16 numbers), which is notated as putting a 0x or 0X in front, then followed by digits 0~9 or A~F (or a~f if you like). For example: 0xA9 = (10*16^1) + (9*16^0) = 169.

Also here’s a random fun fact: Did you know 0xDEADBEEF evaluates to 3735928559? There’s also an entire Wikipedia article dedicated to these: Magic number (programming) - Wikipedia.

Binary numbers (0b1010)

For binary numbers (aka base-2 numbers), which is more-or-less same with hex, but you can only use 0s and 1s. For example: 0b1010 = (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0) = 10.


Underscores!

We can also add underscores anywhere at the numbers, for example 1_000_000 = 1000000, and 0xFF_FF_FF = 0xFFFFFF. You can also abuse this fact by doing: 2______________________________3 = 23. The underscores don’t do anything, it is only for readability.

You can abuse the underscore trick for any number, creating insane looking numerals that still evaluate correctly. The only places you can’t put underscores are in front of a number, between the e+, before/after a decimal point, or before/after a unary operator. For example, 3e_+2 is invalid.


Unary operations:

Some cool things you can apply to these numbers is putting unary operators + and - (which are unary plus and unary minus) in front of a number, for example we use the unary minus - to indicate a negative number: -3. Otherwise it is defaulted as a positive number (we don’t talk about 0): +3. You can put however much you wish to put in front of a number too: ++++++++++3 = 3.

Oh, and also a fun fact is if you put two unary minus - together, it will become a comment, so that’s kind of sad.


0_X_0_e_0 = 224?

For the number I put in the title 0_X_0_e_0, it is the same as 0x0E0 without the underscores, which is 0xE0 without the leading zero, then it evaluates to (14*16^1) + (0*16^0) = 224.

In this case, the e is prioritized as a hex value, not a scientific notation.


e+ and +

The one I’m interested in is the notation e+ and e- specifically. How will e+ and e- differ with the normal operations + and -?

Turns out e+ and e- in numbers will always be treated as how it is normally, but with one edge case. Since the letter e is included in a~f, which means you can use it as hex. And when you do 0x3e+2 for example, the 0x3e is separated from the plus operation +. So 0x3e+2 is NOT the same as 0x300. 0x3e+2 will be evaluated as 62 + 2 = 64.


Final thoughts

This isn’t really practical knowledge, but it’s pretty fun, and I enjoyed writing a blog for this. You can now try cursed expressions like:

print(0_x_0_0_e_0 / 0_x_0_0_b_0_0 * 0_x_0_b+0 + 0_e+_0)

… Evaluates to 0.875??

Anyways, feel free to post any cursed expressions you can come up with! I kind of want to see what you guys can come up with these.

Please let me know if I put this in the wrong topic, or I made a mistake somewhere. Thanks!


Sources:

Numbers | Documentation
Syntax - Luau

7 Likes

you forgot the legendary 0xbad_c0ffee_0dd_f00d

6 Likes