-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMath.sol
95 lines (79 loc) · 2.21 KB
/
Math.sol
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
file: Math.sol
ver: 0.2.3
updated:20-Apr-2018
author: Darryl Morris
contributors: terraflops
email: o0ragman0o AT gmail.com
An inheritable contract containing math functions and comparitors.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU lesser General Public License for more details.
<http://www.gnu.org/licenses/>.
*/
pragma solidity ^0.4.18;
contract Math
{
/* Constants */
string constant public VERSION = "Math 0.2.3";
uint constant NULL = 0;
bool constant LT = false;
bool constant GT = true;
// No type bool <-> int type conversion in solidity :~(
uint constant iTRUE = 1;
uint constant iFALSE = 0;
uint constant iPOS = 1;
uint constant iZERO = 0;
uint constant iNEG = uint(-1);
/* Modifiers */
/* Functions */
// @dev Parametric comparator for > or <
// !_sym returns a < b
// _sym returns a > b
function cmp (uint a, uint b, bool _sym) internal pure returns (bool)
{
return (a!=b) && ((a < b) != _sym);
}
/// @dev Parametric comparator for >= or <=
/// !_sym returns a <= b
/// _sym returns a >= b
function cmpEq (uint a, uint b, bool _sym) internal pure returns (bool)
{
return (a==b) || ((a < b) != _sym);
}
/// Trichotomous comparator
/// a < b returns -1
/// a == b returns 0
/// a > b returns 1
/* function triCmp(uint a, uint b) internal pure returns (bool)
{
uint c = a - b;
return c & c & (0 - 1);
}
function nSign(uint a) internal pure returns (uint)
{
return a & 2^255;
}
function neg(uint a) internal pure returns (uint) {
return 0 - a;
}
*/
function safeMul(uint a, uint b) internal pure returns (uint)
{
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function safeSub(uint a, uint b) internal pure returns (uint)
{
assert(b <= a);
return a - b;
}
function safeAdd(uint a, uint b) internal pure returns (uint)
{
uint c = a + b;
assert(c>=a && c>=b);
return c;
}
}