VOOZH about

URL: https://www.geeksforgeeks.org/solidity/mathematical-operations-in-solidity/

⇱ Mathematical Operations in Solidity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mathematical Operations in Solidity

Last Updated : 10 Nov, 2022

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:

  1. Addition (x + y)
  2. Subtraction: (x - y)
  3. Multiplication (x * y)
  4. Division (x / y)
  5. Modulus (x % y) 
  6. Exponential  (x**y)

In this article, we will be discussing these operations in detail with examples.

Addition

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:

👁 Addition

Subtraction

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:

👁 Subtraction

Multiplication

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:

👁 Multiplication

Division

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: 

  1. We can see that we divided two 64-bit Integers and successfully stored the result in a 128-bit Integer.
  2. This is only possible in the case of Multiplication and Division Operation.

Output:

👁 Division

Modulus Or Remainder

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:

👁 Modulo

Exponentiation

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:

👁 Exponentiation

Note:

  1. In Addition, Subtraction, and Modulo all the operands must be the same size Integer.
  2. In Division and Multiplication, the calculated answer can be stored in the greater bits Integer but the operands must be the same size Integer.
  3. 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.
  4. 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. 
Comment
Article Tags:

Explore