Created
June 3, 2017 20:13
-
-
Save TravisMullen/f97ba27a6f683d675fdf05b2b8d53a2a to your computer and use it in GitHub Desktop.
Safe Math Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.10; | |
/* taking ideas from FirstBlood token */ | |
contract SafeMath { | |
/* function assert(bool assertion) internal { */ | |
/* if (!assertion) { */ | |
/* throw; */ | |
/* } */ | |
/* } // assert no longer needed once solidity is on 0.4.10 */ | |
function safeAdd(uint256 x, uint256 y) internal returns(uint256) { | |
uint256 z = x + y; | |
assert((z >= x) && (z >= y)); | |
return z; | |
} | |
function safeSubtract(uint256 x, uint256 y) internal returns(uint256) { | |
assert(x >= y); | |
uint256 z = x - y; | |
return z; | |
} | |
function safeMult(uint256 x, uint256 y) internal returns(uint256) { | |
uint256 z = x * y; | |
assert((x == 0)||(z/x == y)); | |
return z; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment