Skip to content

BigInt (a/d)*d + (a%d) drops the remainder term when operands are for...of bindings #9143

Description

@proggeramlug

Found while benchmarking BigInt division (#9141); reproduces on pristine main 653e886699, unrelated to that change.

Repro

The division identity (a / d) * d + (a % d) === a evaluates to (a / d) * d — the remainder term vanishes — when a and d are bindings from a for...of over a BigInt array:

for (const a of [123456789012345678901234567890n]) {
  for (const d of [1000000007n]) {
    console.log((a / d) * d + (a % d));  // node: a   perry: (a/d)*d
  }
}

Correct when the same operands are compile-time constants or plain locals — so this is about how for...of bindings type/lower in a compound BigInt expression, not about the division itself (division and remainder each produce correct values individually).

Repro files: secret-tests/scratchpad/probe_q5.ts, probe_q6.ts.

Why it matters

Silent wrong arithmetic in the shape most real code uses to iterate big-number collections, and the dropped term makes results look plausible rather than obviously broken.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions