Unpopular opinion: i64, int64_t, Int64 and similar types should be named according to their actual meaning, Ring64.

Even better, all programming languages should have a Ring[N] type that provides unit, zero addition and multiplication over a domain of N-bit strings, with the compiler applying proper optimizations when available (and requested).

@Shamar int64_t is not allowed to overflow, so it is not a ring. If you rearrange/transform formulas according to rules of a ring you might break something that was carefully crafted to not overflow.
uint64_t however is a finite field.

Ring could be a useful concept (type requirement), but so many things can be rings that as a specific type it's meaningless.

@namark

What do you mean by "is not allowed to overflow"? Afaik (~0LL)+1 actually overflows...

Follow

@Shamar
Assuming 2's complement ~(0LL) will be -1, and adding 1 will bring it back to 0, which is not a signed integer overflow (curiously this sequence of ~ +1 corresponds to negation in 2's complement).
Something like this would do it ~(1LL << 63) + 1, overflowing from maximum value to minimum.

This kind of overflow, from max to min (or back) is undefined behavior in C/C++ (the only languages that matter :P), which means if you do that your compiler may "optimize" your entire program away or do something else sinister. A more subtle side effect is that you're not allowed to negate the minimum value, as that produces a value that is one more than maximum, so even that simple unary operation is not safe. Historically this was to account for different implementations of signed integers, but it's maintained on latest standards as well, as such a type has proven to be useful for certain optimizations, in contexts where you can otherwise guarantee no overflow will occur.

Up until the latest standards you couldn't legally do 2's complement arithmetic in these languages. With the latest you can, but not directly, only by casting to unsigned type and then casting back.

That said, nobody but the nerdiest care about such things. Normal people most likely break the standards left and right in this regard, there are probably even compiler flags/settings to accommodate that.

· · SubwayTooter · 0 · 0 · 2
Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.