Skip to content

Comments

Align tonumber with current Luau number syntax#160

Open
gaymeowing wants to merge 2 commits intoluau-lang:masterfrom
gaymeowing:align-tonumber-with-number-syntax
Open

Align tonumber with current Luau number syntax#160
gaymeowing wants to merge 2 commits intoluau-lang:masterfrom
gaymeowing:align-tonumber-with-number-syntax

Conversation

@gaymeowing
Copy link
Contributor

@jackdotink
Copy link
Contributor

imo the backwards compatibility loss isnt worth the change here

@alicesaidhi
Copy link
Contributor

what backwards compatibility will be lost here?

@jackdotink
Copy link
Contributor

today, a string passing tonumber means that every character is an ascii digit - this breaks that guarantee

@gaymeowing
Copy link
Contributor Author

gaymeowing commented Dec 25, 2025

imo the backwards compatibility loss isnt worth the change here

I haven't found any code that this would break, and majority of the cases where I've seen people ensuring a string is valid ASCII uses the string library to do so and not tonumber. Albeit my search wasn't that comprensive, but if its had any significance I would expect it to appear in a github search.
https://github.com/search?q=(language%3ALua+OR+language%3ALuau)+is+ascii+tonumber&type=code

Also I don't think hexadecimal strings are made up of solely digits? As this is valid today:

print(tonumber("0xFFFF")) --> 65535

@jackdotink
Copy link
Contributor

that code is not valid today - you have to pass a second argument to tonumber to make it parse hexadecimal. just because you cant find code that would break doesnt mean that a case this simple doesnt exist somewhere

@jackdotink
Copy link
Contributor

the idea that breaking changes only matter if they affect significant customers is a bad one

@alicesaidhi
Copy link
Contributor

alicesaidhi commented Dec 25, 2025

today, a string passing tonumber means that every character is an ascii digit - this breaks that guarantee

this is definitely not true. to number for example supports "nan", "inf", "1e3", "nan(...)", "1.4". ascii digits are only 0-9, and tonumber already supports scientific notation and numbers not normally represented through mere digits.

@jackdotink
Copy link
Contributor

okay sure, its still breaking backwards compatibility without adding significant value - use gsub first if you really want this. as is frequently the case, the solution to your problem is not more language features, but is instead writing more code

@alicesaidhi
Copy link
Contributor

what backwards compat is it breaking?

@alicesaidhi
Copy link
Contributor

alicesaidhi commented Dec 26, 2025

tonumber also supports hexadecimal, without specifying base, like lynn posted right now already. aligning it would only make sense.

@gaymeowing
Copy link
Contributor Author

that code is not valid today - you have to pass a second argument to tonumber to make it parse hexadecimal. just because you cant find code that would break doesnt mean that a case this simple doesnt exist somewhere

what do you mean i ran that with the luau cli

makes the motivation section focus more so on `tonumber`'s current odd behavior
@vegorov-rbx
Copy link
Collaborator

It is pretty common in programming languages to have literals in the source code and runtime number parsing differences.

tonumber relies on the underlying host implementation of double number parsing and supports many forms that are not valid as Luau number literals (nan, inf, hex floats, decimal separators if locale is changed from C).
This RFC doesn't achieve alignment.
Since all C99 features have to be supported for backwards compatibility, we will have to reimplement number parsing to preserve all C99 features to add additional forms. Approach taken in the current parser with temporary string construction while filtering '_' will negatively impact runtime performance.
Even if manually implemented, many of the valid C99 strtod/strtoul strings will remain to be invalid literals in Luau.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants