Skip to content

feat: fold enum arithmetic over doubles, as tsc does - #1075

Merged
mbouaziz merged 2 commits into
mainfrom
enum-fold-doubles
Nov 19, 2026
Merged

mbouaziz merged 2 commits into
mainfrom
enum-fold-doubles

Conversation

@mbouaziz

Copy link
Copy Markdown
Contributor

Stacked on #1074. Merge that one first; this PR's base will then switch to main.

Enum member initializers used to fold only exact integers within 2^53, so everything else widened to number. They now fold with JS double semantics: parse each operand with jsNumberLiteralDouble, apply the operator on Float, and name the result with jsNumberToString. This matches tsc:

Initializer Before Now (= tsc)
0.1 + 0.2 number 0.30000000000000004
1.5 * 2 number 3
10 / 3 number 3.3333333333333335
2 ** 53 + 1 number 9007199254740992
1e308 * 10 number Infinity
1.5 | 0, -2.7 >> 0, ~1.5, 1e300 | 0 number 1, -2, -2, 0
auto-increment after 1.5 number 2.5 (removes a FIXME)

How each operator is handled:

  • % is computed exactly: a truncated remainder by repeated exact subtraction, since Skip has no fmod.
  • Bitwise operators and ~ apply ToInt32/ToUint32 to any double.
  • Three cases stay number, which is sound but stricter than tsc:
    • a NaN result, since NaN has no literal type;
    • division by zero, because a -0 divisor is named 0 and the sign of the infinite result is lost;
    • powers other than exact integers within 2^53, because engines differ in the last bit of non-integer powers.

Commits (tests first):

  1. test: pins the current number results.
  2. feat: does the folding, flips the pins, and updates skjs-rules.md (enums). Version 1.26.0.

make test passes on both commits locally. A differential run of 3,000 random initializers (all 12 operators over fractional, huge, tiny, int32-edge and past-2^53 operands) matches node on every folded value. The 260 that stay number are all powers, NaN remainders, or divisions by zero.

— Mistral Vibe

Base automatically changed from simplify-number-names to main November 18, 2026 22:14
@mbouaziz
mbouaziz enabled auto-merge November 19, 2026 00:09
mbouaziz and others added 2 commits November 19, 2026 00:09
Fractional, past-2^53 and overflowing arithmetic, bitwise operators on
fractional operands and division by zero widen the member to number;
tsc folds each to the literal of its double.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Enum member initializers now fold with JS double semantics instead of
exact integers within 2^53: operands are parsed with
jsNumberLiteralDouble, the operator is applied on Float, and the result
is named by jsNumberToString. So 0.1 + 0.2 is 0.30000000000000004,
1.5 * 2 is 3, 2 ** 53 + 1 is 9007199254740992, 1e308 * 10 is Infinity,
and fractional auto-increment (1.5, then 2.5) folds too.

% is computed exactly (truncated remainder by repeated exact
subtraction); bitwise operators and ~ apply ToInt32/ToUint32 to any
double. A NaN result, division by zero (a -0 divisor is named 0, so its
sign is lost) and powers other than exact integers within 2^53 (engines
round those last bits differently) stay an untracked number.

A 3,000-initializer differential run against node agrees on every folded
value (version 1.26.0).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@mbouaziz
mbouaziz merged commit 091495b into main Nov 19, 2026
1 check passed
@mbouaziz
mbouaziz deleted the enum-fold-doubles branch November 19, 2026 00:16
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.

1 participant