0

In the Solidity docs, "if you have int x = type(int).min;, then -x does not fit the positive range.This means that unchecked { assert(-x == x); } works, and the expression -x when used in checked mode will result in a failing assertion."

.min gives the lowest value, so if its a signed int then "int x = type(int).min" will be a very high negative number? In that case shouldn't -x be positive?

0

1 Answer 1

1

type(int256).min is -57896044618658097711785492504343953926634992332820282019728792003956564819968, while type(int256).max is 57896044618658097711785492504343953926634992332820282019728792003956564819967.

If x was equal to type(int256).min, then the theoretic -x will certainly be greater than type(int256).max by 1 and it will not fit the positive number scale, therefore when computed in code it will overflow and the resulting value will be on the exact opposite side on the far end of the negative number scale due to the way numbers are represented digitally and how binary number addition works. That means that the result of -x would be equal to x, given that we're allowing for number overflows, and thus the unchecked math block unchecked { assert(-x == x); } would work. But when used in checked mode, it will result in a failing transaction since that's the default Solidity behavior for handling number overflows after version 0.8.0.

Not the answer you're looking for? Browse other questions tagged or ask your own question.