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
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
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.