-
Notifications
You must be signed in to change notification settings - Fork 2
/
006-arithmetic-operators.sol
69 lines (57 loc) · 1.58 KB
/
006-arithmetic-operators.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Learn about arithmetic operators in solidity
* Intro - Operators tell the compiler to perform specific
* mathematical operation, logical or relational
* operation and produce a suitable response
*/
contract LearnArithmeticOperators {
uint256 public a = 5;
uint256 public b = 10;
function calculatorAdd() public view returns (uint256) {
uint256 result;
result = a + b;
return result;
}
function calculatorMultiply() public view returns (uint256) {
uint256 result;
result = a * b;
return result;
}
function calculatorSubtract() public view returns (uint256) {
uint256 result;
result = a - b;
return result;
}
function calculatorDivide() public view returns (uint256) {
uint256 result;
// division by ZERO
if (b != 0) {
result = a / b;
} else {
result = 0;
}
return result;
}
function calculatorGetRemainder() public view returns (uint256) {
uint256 result;
result = a % b;
return result;
}
// modulo trick
// the goal is to find the remainder
/*
12 % 23
1. divident = 12, divisor = 23
2. q = 12 / 23, q = 0
3. 0 * 23 = 0, newResult = 0
4. 12 - 0, remainder = 0
*/
function calculatorIncrement() public returns (uint256) {
return a++;
}
function calculatorExpression() public view returns (uint256) {
return (a + b) * 3 - 10;
}
}