How the EXP() function works in Mariadb?

The EXP() function is a numeric function that returns the value of e (the base of natural logarithms) raised to the power of the argument.

Posted on

The EXP() function is a numeric function that returns the value of e (the base of natural logarithms) raised to the power of the argument. The number e, also known as Euler’s number, is a mathematical constant approximately equal to 2.71828. The EXP() function is useful for calculating exponential growth, compound interest, or other applications involving exponential functions. In this article, we will learn how to use the EXP() function in Mariadb with some examples.

Syntax

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

EXP(X)

The EXP() function takes one argument:

  • X is a numeric expression that represents the exponent.

The EXP() function returns a double value of e raised to the power of X. If X is NULL, this function returns NULL. If the computed value does not fit in a double size, an error is returned.

Examples

Let’s see some examples of using the EXP() function in Mariadb.

Example 1: Calculating e to the power of 2

In this example, we will calculate e to the power of 2 using the EXP() function.

SELECT EXP(2);

The output is:

+------------------+
| EXP(2)           |
+------------------+
| 7.38905609893065 |
+------------------+

The EXP() function returns the value of e raised to the power of 2, which is approximately 7.38905609893065.

Example 2: Calculating e to the power of -2

In this example, we will calculate e to the power of -2 using the EXP() function.

SELECT EXP(-2);

The output is:

+--------------------+
| EXP(-2)            |
+--------------------+
| 0.1353352832366127 |
+--------------------+

The EXP() function returns the value of e raised to the power of -2, which is approximately 0.1353352832366127.

Example 3: Calculating e to the power of 0

In this example, we will calculate e to the power of 0 using the EXP() function.

SELECT EXP(0);

The output is:

+--------+
| EXP(0) |
+--------+
|      1 |
+--------+

The EXP() function returns the value of e raised to the power of 0, which is 1. This is because any number raised to the power of 0 is 1.

Example 4: Calculating e to the power of NULL

In this example, we will calculate e to the power of NULL using the EXP() function.

SELECT EXP(NULL);

The output is:

+-----------+
| EXP(NULL) |
+-----------+
| NULL      |
+-----------+

The EXP() function returns NULL when the argument is NULL.

Example 5: Calculating e to the power of a column value

In this example, we will calculate e to the power of a column value using the EXP() function. We will use the following table named scores for this example.

DROP TABLE IF EXISTS scores;
CREATE TABLE scores (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  score INT
);

INSERT INTO scores VALUES
(1, 'Alice', 10),
(2, 'Bob', 8),
(3, 'Charlie', 9),
(4, 'David', 7),
(5, 'Eve', 6);

The table looks like this:

+----+---------+-------+
| id | name    | score |
+----+---------+-------+
|  1 | Alice   |    10 |
|  2 | Bob     |     8 |
|  3 | Charlie |     9 |
|  4 | David   |     7 |
|  5 | Eve     |     6 |
+----+---------+-------+

We will calculate e to the power of the score column using the EXP() function and display the result along with the name column.

SELECT name, EXP(score) FROM scores;

The output is:

+---------+--------------------+
| name    | EXP(score)         |
+---------+--------------------+
| Alice   | 22026.465794806718 |
| Bob     | 2980.9579870417283 |
| Charlie |  8103.083927575384 |
| David   | 1096.6331584284585 |
| Eve     |  403.4287934927351 |
+---------+--------------------+

The EXP() function returns the value of e raised to the power of the score column for each row in the table.

There are some other functions that are related to the EXP() function in Mariadb. Here are some of them:

  • The LN() function returns the natural logarithm of the argument. The natural logarithm is the inverse of the EXP() function. For example, LN(EXP(X)) is equal to X.
  • The LOG() function returns the logarithm of the argument to a specified base. The base can be specified as a second argument or default to e if omitted. For example, LOG(EXP(X)) is equal to X, and LOG(X, 10) is equal to the base-10 logarithm of X.
  • The LOG10() function returns the base-10 logarithm of the argument. This is equivalent to LOG(X, 10). For example, LOG10(100) is equal to 2.
  • The LOG2() function returns the base-2 logarithm of the argument. This is equivalent to LOG(X, 2). For example, LOG2(8) is equal to 3.
  • The POW() function returns the value of the first argument raised to the power of the second argument. This is equivalent to EXP(Y * LN(X)). For example, POW(2, 3) is equal to 8.

Conclusion

In this article, we learned how to use the EXP() function in Mariadb to calculate the value of e (the base of natural logarithms) raised to the power of the argument. We also saw some examples of using the EXP() function with different types of arguments and some related functions. The EXP() function is a handy function for performing exponential calculations in Mariadb.