Hacker News new | ask | show | jobs
by someonebaggy 67 days ago
One reason is that the language didn't originally intend for UB to be so broad. They probably expected that each value would be loaded into an int-sized register, and the calculation would then be performed on two int-sized registers with overflow handled in a platform-dependent but generally sane manner (not time travel).
1 comments

Yeah, reading the rationale in the standard itself, it's is kinda obvious that they didn't consider potential UB at all: when talking about "questionable signedness" they only mention division/remainder and comparisons as being possibly affected, but not addition/multiplication (due to overflow), and comparisons behaving mostly expectedly (for some value of "expectedly") is the main selling point. But comparing signed and unsigned values without actual thought and explicit cast is almost always a bug anyway! And we had the warnings for that for, like, forever.

Makes me wonder how ergonomic would a language with actually correct signatures for the arithmetic operators be.

    +<S, M, N>: int<S, M> × int<S, N> → int<S, max(M, N)+1> // alternatively int<S, max(M, N)> × bool
    -<S, M, N>: int<S, M> × int<S, N> → int<signed, max(M, N)+1> // alternatively int<signed, max(M, N)> × bool
    *<S, M, N>: int<S, M> × int<S, N> → int<S, M+N>
    /<S, M, N>: int<S, M> × int<S, N> → int<S, M>
    %<S, M, N>: int<S, M> × int<S, N> → int<S, N>