-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSafeMath.sol
More file actions
33 lines (32 loc) · 845 Bytes
/
SafeMath.sol
File metadata and controls
33 lines (32 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pragma solidity^0.4.11;
/**
*@titleSafeMath
*@devMathoperationswithsafetychecksthatthrowonerror
*/
library SafeMath
{
function mul(uint256 a, uint256 b) internal constant returns (uint256)
{
uint256 c=a*b;
assert(a==0||c/a==b);
return c;
}
function div(uint256 a,uint256 b) internal constant returns(uint256)
{
//assert(b>0);//Solidityautomaticallythrowswhendividingby0
uint256 c=a/b;
//assert(a==b*c+a%b);//Thereisnocaseinwhichthisdoesn'thold
return c;
}
function sub(uint256 a,uint256 b) internal constant returns(uint256)
{
assert(b<=a);
return a-b;
}
function add(uint256 a,uint256 b) internal constant returns(uint256)
{
uint256 c=a+b;
assert(c>=a);
return c;
}
}