Skip to content

Commit 0fd569f

Browse files
authored
Add files via upload
0 parents  commit 0fd569f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1-BasicMath.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract BasicMath {
5+
uint256 constant MAX_INT = type(uint256).max;
6+
7+
function adder(uint256 _a, uint256 _b) external pure returns (uint256 sum, bool error) {
8+
if (_b > MAX_INT - _a) {
9+
return (0, true); // Overflow occurred
10+
}
11+
return (_a + _b, false);
12+
}
13+
14+
function subtractor(uint256 _a, uint256 _b) external pure returns (uint256 difference, bool error) {
15+
if (_b > _a) {
16+
return (0, true); // Underflow occurred
17+
}
18+
return (_a - _b, false);
19+
}
20+
21+
function multiplier(uint256 _a, uint256 _b) external pure returns (uint256 product, bool error) {
22+
// Check for overflow during multiplication
23+
if (_a == 0 || _b > MAX_INT / _a) {
24+
return (0, true); // Overflow occurred
25+
}
26+
return (_a * _b, false);
27+
}
28+
29+
function modulo(uint256 _a, uint256 _b) external pure returns (uint256 remainder, bool error) {
30+
// Solidity requires the divisor to be greater than zero for modulo operation
31+
require(_b > 0, "Divisor cannot be zero");
32+
return (_a % _b, false);
33+
}
34+
}

0 commit comments

Comments
 (0)