Back to homepage

Math operations in Solidity: the basics

Published

A quick guide describing how to do basic mathematical operations in Solidity


Basic math operations

Math operations in Solidity are pretty straightforward and most operations are the same as in most programming languages. Given two numeric variables (x and y), we could do the following operations:

  • Addition: x + y
  • Subtraction: x - y
  • Multiplication: x * y
  • Division: x / y
  • Modulus / remainder: x % y (for example, 21 % 5 is 1, because if you divide 21 into 5, 1 is the remainder)

Exponential in Solidity

Solidity also supports the exponential operator (i.e. "x to the power of y") which will be represented as x^y.

Advance math operations

For other complex math operations in Solidity like root, logarithm, and exponentiation functions, or basic operations with a higher precission, you can use the ABDK Math Quad library.

TAGS


If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Buy Me A Coffee