Solidity – Meanings of `(uint256) (a)` and `(int256) (a)` in Solidity

ethereum-classicoverflowsolidity

I came across the following code
payoutsTo_[_customerAddress] += (int256) (_dividends * magnitude);

What is meaning of (int256) in this line, and is there any possibility of overflow here? Or here:
magnitude = 2 ** 64

Best Answer

An educated guess:

Since you did not provide the type of variable payoutsTo_, I believe that it is:

mapping (address => int256) public payoutsTo_;

The following attempt of assigning a uint256 value to an int256 variable would not compile:

payoutsTo_[_customerAddress] += _dividends * magnitude;

Therefore, it is required to cast the value of _dividends * magnitude to int256.


Is there any possibility of overflow here, with magnitude == 2 ** 64?

Yes, the value of int256(_dividends * magnitude) will:

  • Turn negative when 2 ** (255 - 64) <= _dividends < 2 ** (256 - 64)
  • Wrap around when 2 ** (256 - 64) < _dividends
Related Topic