C++ Logo

std-proposals

Advanced search

Mistake in C++ Standard Arithmetic Conversion makes C++ compiler unreliable

From: Владимир Прокофьев <vov-crao_at_[hidden]>
Date: Tue, 07 Jan 2020 09:46:14 +0300
I have found a mistake in the C++ Standard, https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversion : in the rule:
Otherwise, if the unsigned operand's conversion rank is greater or equal to the conversion rank of the signed operand, the signed operand is converted to the unsigned operand's type.
The NEGATIVE signed value CANNOT be converted to unsigned for any types. But opposite conversion from unsigned to signed is valid with appropriate rank.
To prove, please see wrong results here https://rextester.com/CKKDX46498
Note, that the error is observed for VS, CLang and GCC C++ compilers.

As you can see the error is observer in division operation to unsigned 32 and 64 bits.
The division operation to SIGNED types is VALID (see first table in the example).
Also multiplication of signed and unsigned values is VALID.
The example above shows that the C++ compiler shows unreliable behavior which leads to produce wrong values as a result of division.

The correct rule shall be:
Otherwise, unsigned operand is converted to signed type of same or higher conversion rank of unsigned operand's type and then the operand with lesser conversion rank is converted to the operand with the greater conversion rank

С уважением.
Владимир.

Received on 2020-01-07 00:48:46