LOG and EXP functions |
||||||||||||||||||||
The LOG(x) function returns the natural logarithm of the expression x. The returned result is of type Starting with
This query returns the natural and the base-10 logarithms of 2. Besides, there is the function LOG10(x) inherited from previous versions that returns the base-10 logarithm of the expression x. It has been redundant from the very beginning, since instead of it, the well-known change-of-base formula for logarithms can be used: LOGba = LOGca/LOGcb Thus, the following three expressions will return the same result (0.301029995663981):
The function EXP(x), or the exponential function, returns the number e raised to the power of x. The returned result is of type FLOAT. This function is inversely related to the LOG function:
Another useful feature of logarithms - namely, the fact that the logarithm of a product is the sum of the logarithms of the factors - allows us to calculate the product of values in a column, i.e.
We can easily prove the correctness of this equality by the following example:
Among the SQL aggregate functions, there isn't any for multiplying values. However, using the aforementioned feature of logarithms and some elementary transformations, we can reduce this task to calculating a sum of values. Indeed,
Calculate the factorial of the number equal to the quantity of rows in the Laptop table. Solution
|