I'd like to divide integers of different sizes (uint16 and uint256). Do I need to cast the integers to the same size prior to the divide? Or can I just do the the following safely:

    import "../math/SafeMath.sol";
    using SafeMath for uint256; 
    using SafeMath for uint16;

    uint16 public totalValid;
    uint256 public etherValue;

     function _calculatePayout() internal{
        require(this.balance > 0); 
        etherValue = this.balance.div(totalValid); // uint256 this.balance

Thoughts appreciated.

  • 1
    No you don't. Also, you may as well do this.balance / totalValid, since SafeMath.div function is pointless (there's no such thing as "safe div"), and OpenZeppelin will probably realize this sooner or later... May 13 '18 at 18:07

Usually these libraries are written and used for one particular integer type, so in your case you have to use SafeMath for uint256 and typecast totalValid.

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.