[OT] The Usual Arithmetic Confusions
Steven Schveighoffer
schveiguy at gmail.com
Fri Feb 4 23:19:28 UTC 2022
On 2/4/22 5:22 PM, Paul Backus wrote:
> On Friday, 4 February 2022 at 22:11:10 UTC, Steven Schveighoffer wrote:
>> I don't think anyone is arguing that the result of the operation
>> should be truncated to a byte, even if assigned to an int.
>
> And yet, that's exactly what happens if you use `int` and `long`:
>
> int a = int.max;
> long b = a + 1;
> writeln(b > 0); // false
>
> I think there are reasonable arguments to be made on both sides, but
> having both behaviors in the same language is a bit of a mess, don't you
> think?
Yes, I would prefer for the compiler to determine if the overflow is
needed, and generate appropriate instructions based on that.
The reason this doesn't come up often for int -> long is because a) you
aren't usually converting an int-only operation to a long, and b)
overflow of an int is rare.
-Steve
More information about the Digitalmars-d
mailing list