-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicMath
More file actions
43 lines (40 loc) · 1.41 KB
/
BasicMath
File metadata and controls
43 lines (40 loc) · 1.41 KB
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;
// @title BasicMath
// @notice This contract is used to test the overflow detection
// @author NelsonRodMar
contract BasicMath {
constructor() {}
// @notice This function return the sum of the two number and a boold true if overflow otherwise false
// @param _a The first number
// @param _b The second number
// @return sum The sum of the two number
// @return error True if overflow otherwise false
function adder(uint256 _a, uint256 _b) public pure returns (uint256 sum, bool error) {
unchecked {
if (_a + _b >= _a) {
sum = _a + _b;
error = false;
} else {
sum = 0;
error = true;
}
}
}
// @notice This function return the difference of the two number and a boold true if overflow otherwise false
// @param _a The first number
// @param _b The second number
// @return difference The difference of the two number
// @return error True if overflow otherwise false
function subtractor(uint256 _a, uint256 _b) public pure returns (uint256 difference, bool error) {
unchecked {
if (_a - _b <= _a) {
difference = _a - _b;
error = false;
} else {
difference = 0;
error = true;
}
}
}
}