-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathExactMathExamples.sol
84 lines (68 loc) · 2.03 KB
/
ExactMathExamples.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
pragma solidity ^0.4.16;
pragma experimental "v0.5.0";
pragma experimental "ABIEncoderV2";
import {ExactMath} from "../src/math/ExactMath.sol";
contract MathExamples {
using ExactMath for int;
using ExactMath for uint;
// Add exact uints example.
function uintExactAddOverflowExample() public pure {
var n = uint(~0);
n.exactAdd(1);
}
// Subtract exact uints example.
function uintExactSubUnderflowExample() public pure {
var n = uint(0);
n.exactSub(1);
}
// Multiply exact uints example.
function uintExactMulOverflowExample() public pure {
var n = uint(~0);
n.exactMul(2);
}
// Add exact ints example.
function intExactAddOverflowExample() public pure {
int n = 2**255 - 1;
n.exactAdd(1);
}
// Add exact ints example.
function intExactAddUnderflowExample() public pure {
int n = int(2**255);
n.exactAdd(-1);
}
// Subtract exact ints example.
function intExactSubOverflowExample() public pure {
var n = int(2**255 - 1);
n.exactSub(-1);
}
// Subtract exact ints example.
function intExactSubUnderflowExample() public pure {
var n = int(2**255);
n.exactSub(1);
}
// Multiply exact ints example.
function intExactMulOverflowExample() public pure {
var n = int(2**255 - 1);
n.exactMul(2);
}
// Multiply exact ints example.
function intExactMulUnderflowExample() public pure {
var n = int(2**255);
n.exactMul(2);
}
// Multiply exact ints example.
function intExactMulIllegalUseOfIntMinExample() public pure {
var n = int(2**255);
n.exactMul(-1);
}
// Multiply exact ints example.
function intExactMulIllegalUseOfIntMinExample2() public pure {
var n = int(-1);
n.exactMul(int(2**255));
}
// Multiply exact ints example.
function intExactDivIllegalUseOfIntMinExample() public pure {
var n = int(2**255);
n.exactDiv(-1);
}
}