PostgreSQL EXP() Function
Summary: in this tutorial, you will learn how to use the PostgreSQLEXP() function to calculate the exponential of a number.
Introduction to the PostgreSQL EXP() function
An exponential of a number is the number e, approximately equal to2.71, raised to a given power (en).
In PostgreSQL, you can use theEXP() function to calculate the exponential of a number.
Here’s the syntax of theEXP() function:
EXP(n)In this syntax:
nis the number that you want to calculate the exponential. It can be a literal number, an expression, or a table column.
TheEXP() function returns the exponential of a number in double precision. TheEXP() function returnsNULL if the n isNULL.
If n is a string, theEXP() function will attempt to convert it to a number before calculating the exponential. If the conversion fails, theEXP() function will raise an error.
PostgreSQL EXP() function examples
Let’s take some examples to practice theEXP() function.
1) Basic EXP() function examples
The following example uses theEXP() function to return the exponential of 1:
SELECT EXP(1) result;Output:
result------------------- 2.718281828459045(1 row)It returns the Euler number (e) because e1 is e.
The following statement uses theEXP() function to return the exponential of zero:
SELECT EXP(0) result;Output:
result-------- 1(1 row)It returns 1 because e0 is 1.
2) Using the EXP() function with numeric strings
The following example uses theEXP() function with a numeric string:
SELECT EXP('10') result;Output:
exp-------------------- 22026.465794806718(1 row)In this example, the EXP() function converts the string ’10’ to the number 10 before calculating the exponential of 10.
The following example raises an error because the function fails to convert the string ’10x’ to a number:
SELECT EXP('10X')result;Error:
ERROR: invalid input syntax for type double precision: "10X"LINE 1: SELECT EXP('10X')result; ^Summary
- Use the
EXP()function to calculate the exponential of a number.
Last updated on