How the POWER() function works in Mariadb?

The POWER() function is a mathematical function in Mariadb that returns the value of a number raised to the power of another number.

Posted on

The POWER() function is a mathematical function in Mariadb that returns the value of a number raised to the power of another number. The function is equivalent to the POW() function, which is a synonym for the POWER() function. The function can be used to perform various calculations involving exponents, such as finding the square root, the cube root, or the nth root of a number.

Syntax

The syntax of the POWER() function is as follows:

POWER(base, exponent)

The function takes two arguments:

  • base: A numeric expression that represents the base of the exponentiation. The base can be any valid numeric value, such as an integer, a decimal, or a fraction. The base cannot be NULL.
  • exponent: A numeric expression that represents the exponent of the exponentiation. The exponent can be any valid numeric value, such as an integer, a decimal, or a fraction. The exponent cannot be NULL.

The function returns a numeric value that represents the result of the exponentiation, as follows:

  • If the base and the exponent are both positive, the function returns the positive value of the base raised to the power of the exponent.
  • If the base is negative and the exponent is an integer, the function returns the negative value of the base raised to the power of the exponent.
  • If the base is negative and the exponent is not an integer, the function returns NULL, as the result is not a real number.
  • If the base is zero and the exponent is positive, the function returns zero.
  • If the base is zero and the exponent is zero, the function returns one, as per the mathematical convention.
  • If the base is zero and the exponent is negative, the function returns NULL, as the result is not a finite number.
  • If the base is positive and the exponent is zero, the function returns one, as per the mathematical convention.
  • If the base is positive and the exponent is negative, the function returns the reciprocal of the base raised to the power of the absolute value of the exponent.

Examples

Example 1: Finding the square of a number

The following example finds the square of the number 5 using the POWER() function.

SELECT POWER(5, 2) AS square;

The output is:

+--------+
| square |
+--------+
|     25 |
+--------+

The output shows that the square of 5 is 25, as 5^2 = 25.

Example 2: Finding the cube root of a number

The following example finds the cube root of the number 27 using the POWER() function.

SELECT POWER(27, 1/3) AS cube_root;

The output is:

+-----------+
| cube_root |
+-----------+
|         3 |
+-----------+

The output shows that the cube root of 27 is 3, as 27^(1/3) = 3.

Example 3: Finding the nth root of a number

The following example finds the 4th root of the number 16 using the POWER() function.

SELECT POWER(16, 1/4) AS fourth_root;

The output is:

+-------------+
| fourth_root |
+-------------+
|           2 |
+-------------+

The output shows that the 4th root of 16 is 2, as 16^(1/4) = 2.

There are some other functions that are related to the POWER() function, such as:

  • POW(): This function is a synonym for the POWER() function. The syntax of the function is POW(base, exponent), where base and exponent are the same as in the POWER() function. The function returns the same result as the POWER() function.
  • EXP(): This function returns the value of the mathematical constant e (approximately 2.71828) raised to the power of a given number. The syntax of the function is EXP(number), where number is a numeric expression. The function returns a numeric value that represents the result of the exponentiation. For example, EXP(1) returns e, and EXP(0) returns 1.
  • LOG(): This function returns the natural logarithm of a given number. The natural logarithm is the logarithm to the base e. The syntax of the function is LOG(number), where number is a positive numeric expression. The function returns a numeric value that represents the result of the logarithm. For example, LOG(e) returns 1, and LOG(1) returns 0.
  • LOG10(): This function returns the common logarithm of a given number. The common logarithm is the logarithm to the base 10. The syntax of the function is LOG10(number), where number is a positive numeric expression. The function returns a numeric value that represents the result of the logarithm. For example, LOG10(10) returns 1, and LOG10(1) returns 0.

Conclusion

The POWER() function is a useful function to return the value of a number raised to the power of another number. The function is equivalent to the POW() function, which is a synonym for the POWER() function. The function can be used to perform various calculations involving exponents, such as finding the square root, the cube root, or the nth root of a number. The function takes two arguments, which are the base and the exponent of the exponentiation. The function returns a numeric value that represents the result of the exponentiation, depending on the values of the base and the exponent. The function can also be combined with other mathematical functions, such as EXP(), LOG(), LOG10(), etc., to perform more complex operations on numbers.