![]() |
VOOZH | about |
Solidity is a brand-new programming language created by the Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 led by Christian Reitwiessner. Ethereum is a decentralized open-source platform based on blockchain domain, used to run smart contracts i.e. applications that execute the program exactly as it was programmed without the possibility of any fraud, interference from a third party, censorship, or downtime
Smart contracts are high-level program codes that are compiled to EVM byte code and deployed to the Ethereum blockchain for further execution. It allows us to perform credible transactions without any interference of the third party, these transactions are trackable and irreversible. Mathematical operations in solidity are as simple as we use these operations in real life. There are the following Mathematical Operations in Solidity:
In this article, we will be discussing these operations in detail with examples.
The addition operation involves adding the two numbers and generating a sum.
Example: In the below example, a contract is created with two functions to set the values of the two variables and a third function to add the two variables and return the sum.
Output:
The subtraction operation involves subtracting the two numbers and generating a difference. In Subtraction, we could have used any value of Integer (int16 to int256) but all the operands must be of the same data types (same bits Integers).
Example: In the below example, a contract is created with a function to demonstrate the subtraction of two numbers.
Output:
The multiplication operation deals with generating a product value by multiplying two or more numbers.
Example: In the below example, a contract is created with three functions, where the first two functions are responsible for setting the values of the variables, and the third function generates a product value by multiplying the first two variables.
Output:
The division operation returns the quotient of the division result.
Example: In the below example, a contract is created with three functions, where the first two functions are responsible for setting the values of the variables, and the third function generates a quotient value by dividing the second number by the first number.
Note:
- We can see that we divided two 64-bit Integers and successfully stored the result in a 128-bit Integer.
- This is only possible in the case of Multiplication and Division Operation.
Output:
This operation returns the remainder result of the process of division of two numbers.
Example: In the below example, a contract is created with three functions, where the first two functions are responsible for setting the values of the variables, and the third function generates a remainder value by dividing the second number by the first number. In Modulo Operation all the variables must be Integers of the Same Bits Length.
Output:
The exponentiation operation only works on Unsigned Integers where the lower bits of unsigned Integers can be calculated and stored in a higher-bit unsigned Integer.
Output:
Note: