[OT] The Usual Arithmetic Confusions

Paul Backus snarwin at gmail.com
Fri Feb 18 05:47:13 UTC 2022


On Friday, 18 February 2022 at 04:33:39 UTC, forkit wrote:
> On the otherhand, implicit conversion of uint to int is 
> inherently unsafe, since the compiler cannot determine whether 
> the coercion 'avoids undefined behaviour'.

The behavior of converting a uint to an int is well-defined in D: 
the uint's bit pattern is re-interpreted as a signed int using 
32-bit two's complement notation. This conversion is valid for 
every possible pattern of 32 bits, and therefore for every 
possible uint. There is absolutely no possibility of undefined 
behavior.

"Undefined behavior" is a technical term with a precise meaning. 
[1] It does not simply mean "undesirable behavior" or 
"error-prone behavior" or even "behavior that violates the rules 
of conventional mathematics."

[1] https://en.wikipedia.org/wiki/Undefined_behavior


More information about the Digitalmars-d mailing list