Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Created June 3, 2017 20:13
Show Gist options
  • Save TravisMullen/f97ba27a6f683d675fdf05b2b8d53a2a to your computer and use it in GitHub Desktop.
Save TravisMullen/f97ba27a6f683d675fdf05b2b8d53a2a to your computer and use it in GitHub Desktop.
Safe Math Functions
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