abs and minimum values

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Oct 31 07:54:27 UTC 2021

On Sunday, 31 October 2021 at 05:04:33 UTC, Dom DiSc wrote:
> This should be no surprise. You need to know what the resulting 
> type of int + uint should be. And it is ...... uint!  which is 
> one of the stupit integer-promotion rules inherited from C.

In C++ it is undefined behaviour to take the absolute value of a 
value that has no positive representation. I assume the same is 
true for C? So you can write a compiler that detects it and fails.

You cannot do this in D as int is defined to represent an 
infinite set of numbers (mapped as a circle). So in D, you could 
say that the abs of the most negative value is a positive value 
that is represented as a negative due to circular wrapping.

If this happens in C then it is a bug. If it happens in D, then 
it is a defined feature of the language.

More information about the Digitalmars-d-learn mailing list