On Fri, 23 Jan 2026 19:50:49 GMT, Joe Darcy <[email protected]> wrote:
> First cut at adding modeling interfaces for the algebraic structures of > rings, groups, and fields. > Sorry for digression. > > > Note 32-bit int arithmetic is a ring -- Integer.MIN_VALUE is its own > > additive inverse. > > While I may not convince anyone all at once, I feel solidly that this is not > the right way to think of this. int does not model Z_2^32 all that > successfully. Looked at that way, it has no business having a division > operator, or being comparable, etc. But more to the point, if we center > _typical real-world_ use cases in our minds, it should be clear that the > "wraparound" behavior is a _failure_ mode, where the representation is > _failing_ to model the intended value. (Cases where wraparound is actually > _desired_ by the user exist but are just too rare to focus on here.) > > In general, it's muddying whenever we try to talk about whether a Java type > "is" a ring/field/etc.. I'd suggest we can only speak fruitfully when we draw > a clean distinction between (a) what values the type is trying to model and > (b) what is its (almost always imperfect) _representation_ for those values. > As much as possible the result of an operation should still qualify as a > decent representation of the correct result; but in overflow cases it just > won't be. The wrapping around of 32-bit two's complement arithmetic has well-defined operational semantics and optimizers are justified in making transformations on int expressions taking advantage of all the ring properties (commutativity, associativity, etc.) because all the ring properties actually do hold. For int expressions, 32-bit wrapping around expressions may not be what was wanted, but it is what was asked for ;-) ------------- PR Comment: https://git.openjdk.org/valhalla/pull/1957#issuecomment-3792833226
