Semantic Diff

#9415 Fold float addition of int literals only when the sum is exact

1 commit

45dba2c9137d6be765c1d8d45f287d739e028498..pr-9415

a4f645005 · illusionalsagacity · Nov 23, 2026, 20:27 UTC

Fold float addition of int literals only when the sum is exact

Why

Float.fromInt of an int literal stays an int literal, and +. and -. on two such literals were folded with int32 arithmetic, so Float.fromInt(2147483647) +. Float.fromInt(1) became -2147483648. A float chain x + k + j was also reassociated to x + (k + j), which is inexact in floating point. Unfolding 0 - min_int then exposed that 0 - e printed as -e even when e itself starts with -, giving --a (a decrement) or --5 (a syntax error). Float literals now fold only when the exact result fits an int32, chains are left as written, and 0 - e prints - e when e starts with -, also when e is the payload of an identity Some or a raw negative literal. Int addition and subtraction fold literals with wraparound themselves, so their output stays the same.

modifiedJS float addition and subtraction of int literals

let big = Float.fromInt(2147483647) +. Float.fromInt(1)let chain = x => x +. Float.fromInt(1) +. Float.fromInt(1)let wrap = Float.toInt(Float.fromInt(2147483647)) + 1let min = x => x + -2147483648
js
Before
After
let big = -2147483648;let big = 2147483647 + 1; function chain(x) {function chain(x) { return x + 2; return x + 1 + 1;}} let wrap = -2147483648;let wrap = -2147483648; function min(x) {function min(x) { return x - -2147483648 | 0; return x - -2147483648 | 0;}}

modifiedJS subtraction from zero of an expression starting with a minus

let neg = Float.fromInt(0) -. Float.fromInt(-2147483648)let twice = a => Float.fromInt(0) -. (Float.fromInt(0) -. a)let some = a => Float.fromInt(0) -. Option.getUnsafe(Some(Float.fromInt(0) -. a))let raw = Float.fromInt(0) -. %raw("-5")
js
Before
After
let neg = -2147483648;let neg = - -2147483648; function twice(a) {function twice(a) { return --a; return - -a;}} function some(a) {function some(a) { return --a; return - -a;}} let raw = --5;let raw = - -5;

Review

  • Does int arithmetic still wrap? Yes: + and - on int literals are folded with wraparound, also when a literal only becomes visible in the JS layer, and an unfolded int sum is still wrapped in | 0.
  • Which checked-in outputs change? Only float_add_fold_test.mjs.