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 following Mathematical Operations in Solidity:
- Addition (x + y)
- Subtraction: (x – y)
- Multiplication (x * y)
- Division (x / y)
- Modulus (x % y)
- Exponential (x**y)
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.
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.
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.
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.
- 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.
Modulus Or Remainder
This operation returns the remainder result of the process of division fo 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.
The exponentiation operation only works on the Unsigned Integers where the lower bits unsigned Integers can be calculated and stored in a higher bit unsigned Integer.
- In Addition, Subtraction, and Modulo all the operands must be the same size Integer.
- In Division and Multiplication, the calculated answer can be stored in the greater bits Integer but the operands must be the same size Integer.
- In the Exponentiation function, the operands must be unsigned Integer. Lower bits Unsigned Integers can be calculated and stored in a higher but Unsigned Integer.
- You need to set the same data type of all the operands in order to apply a mathematical operation on them else they will not execute.
- Solidity - Types
- Solidity - Functions
- What are Events in Solidity?
- Solidity - Inheritance
- Solidity - Polymorphism
- Solidity - View and Pure Functions
- Solidity - Encapsulation
- Solidity - Abstract Contract
- How to Install Solidity in Windows?
- Solidity - While, Do-While, and For Loop
- Solidity - Break and Continue Statements
- Solidity - Variables
- Solidity - Error Handling
- Steps to Execute Solidity Smart Contract using Remix IDE
- Solidity - Arrays
- Solidity - Enums and Structs
- Solidity - Libraries
- Introduction to Solidity
- Solidity - Constructors
- Solidity - Decision Making Statements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.