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? Nov 30, 2020 at 5:03
  • both are uint256
    – ashwin
    Nov 30, 2020 at 5:28
  • And the type of payoutsTo_? Nov 30, 2020 at 6:40
  • mapping(address => int256) payoutsTo_
    – ashwin
    Nov 30, 2020 at 12:33
  • Well then I've already answered your question several hours ago, based on that (as an assumption). Nov 30, 2020 at 12:42

1 Answer 1

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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