Skip to content

fix Number constants#1698

Open
RealColdFry wants to merge 1 commit intoTypeScriptToLua:masterfrom
RealColdFry:fix/number-constants
Open

fix Number constants#1698
RealColdFry wants to merge 1 commit intoTypeScriptToLua:masterfrom
RealColdFry:fix/number-constants

Conversation

@RealColdFry
Copy link
Copy Markdown
Contributor

@RealColdFry RealColdFry commented Mar 26, 2026

For reference, the correct ordering is: NEGATIVE_INFINITY < MIN_SAFE_INTEGER < 0 < MIN_VALUE < EPSILON < MAX_SAFE_INTEGER < MAX_VALUE < POSITIVE_INFINITY

All four Number constants emit wrong Lua — they all end up as inf or -inf:

Constant Before (wrong) After (correct)
MAX_SAFE_INTEGER 2 ^ 1024inf 2 ^ 53 - 1
MIN_SAFE_INTEGER -2 ^ 1074-inf -(2 ^ 53 - 1)
MIN_VALUE -2 ^ 1074-inf 2 ^ -1074
MAX_VALUE 2 ^ 1024inf (2 - 2 ^ -52) * 2 ^ 1023

Also MIN_SAFE_INTEGER and MIN_VALUE were emitting identical code, same for MAX_SAFE_INTEGER and MAX_VALUE.

You can verify with:

console.log("MAX_SAFE_INTEGER:", Number.MAX_SAFE_INTEGER);  // 9007199254740991
console.log("MIN_SAFE_INTEGER:", Number.MIN_SAFE_INTEGER);  // -9007199254740991
console.log("MIN_VALUE:", Number.MIN_VALUE);                // 5e-324
console.log("MAX_VALUE:", Number.MAX_VALUE);                // 1.7976931348623157e+308

See Number on MDN for the correct values.

Repro in the TSTL playground: https://typescripttolua.github.io/play/#code/5.4/MYewdgziA2CmB00QHMAUAiAsgQQBoH0BlbAMQFF8BJAOQBUyBxMgJQC50AaAAmoFcBbAEawATvBwFi5KnUYsAlAG4uXAPSquATgAM2gOwBGTZoBMAVgAsei9uMGAUKEgwESNFhpFSFGvSZtOHgFhMUxPKR9ZfyUVdS4AWh19I1NLa1tNBycoOEQUDDDqfAA1bAAZAFUydm4+IVFxT1LKshiVdo7OtQ0zWHiAZhMLR3Ac13ysPBLyqpqg+tCp5qq2rrXurgN4PU09ADZNfoN+iwAOPZMjsz1YAGp+7VOgA

The existing test didn't catch this because it used expectToEqual(true) with comparisons that the wrong values accidentally satisfied (e.g. -inf <= -inf is true). Switched to expectToMatchJsResult with stricter comparisons that actually verify the ordering and specific values.

…mitting wrong Lua

All four constants evaluated to Infinity or -Infinity in Lua. Additionally,
MIN_SAFE_INTEGER and MIN_VALUE emitted identical code, as did MAX_SAFE_INTEGER
and MAX_VALUE. The existing test did not catch this because it only checked
Lua-side results with comparisons that the wrong values accidentally satisfied.
@RealColdFry RealColdFry marked this pull request as ready for review March 27, 2026 10:17
Copy link
Copy Markdown
Member

@lolleko lolleko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice good catches i think this has been broken since we added these constants in #1492
Switching to expectToMatchJsResult in tests gives me confidence that everything is correct.

In theory this breaks API / behvaiour, in this case that is warranted though.

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.

2 participants