0

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

5
  • What are the types of _dividends and magnitude? – goodvibration Nov 30 '20 at 5:03
  • both are uint256 – ashwin Nov 30 '20 at 5:28
  • And the type of payoutsTo_? – goodvibration Nov 30 '20 at 6:40
  • mapping(address => int256) payoutsTo_ – ashwin Nov 30 '20 at 12:33
  • Well then I've already answered your question several hours ago, based on that (as an assumption). – goodvibration Nov 30 '20 at 12:42
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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