How the POW() function works in Mariadb?
The POW()
function is a mathematical function in Mariadb that returns the value of a number raised to the power of another number.
The POW()
function is a mathematical function in Mariadb that returns the value of a number raised to the power of another number. The function can also be written as POWER()
, which is a synonym for the POW()
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 POW()
function is as follows:
POW(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 beNULL
.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 beNULL
.
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 POW()
function.
SELECT POW(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 POW()
function.
SELECT POW(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 POW()
function.
SELECT POW(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
.
Related Functions
There are some other functions that are related to the POW()
function, such as:
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 isEXP(number)
, wherenumber
is a numeric expression. The function returns a numeric value that represents the result of the exponentiation. For example,EXP(1)
returns e, andEXP(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 isLOG(number)
, wherenumber
is a positive numeric expression. The function returns a numeric value that represents the result of the logarithm. For example,LOG(e)
returns 1, andLOG(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 isLOG10(number)
, wherenumber
is a positive numeric expression. The function returns a numeric value that represents the result of the logarithm. For example,LOG10(10)
returns 1, andLOG10(1)
returns 0.
Conclusion
The POW()
function is a useful function to return the value of a number raised to the power of another number. The function can also be written as POWER()
, which is a synonym for the POW()
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.