Friday, December 20, 2024

MySQL LN() Function

 The LN() function in MySQL is used to calculate the natural logarithm (base 

ee) of a given positive numeric value. The base ee is approximately equal to 2.718281828459045.

Syntax:

LN(number)

Parameters:

  • number: A positive numeric value for which the natural logarithm is calculated. It must be greater than zero.

Return Value:

  • The natural logarithm of the provided number.
  • Returns NULL if the number is less than or equal to zero or if it is NULL.

Example Usage:

Basic Usage

SELECT LN(1);  -- Returns 0, because ln(1) = 0
SELECT LN(2.718281828459045);  -- Returns approximately 1, because ln(e) = 1
SELECT LN(10);  -- Returns approximately 2.302585

With a Table

CREATE TABLE numbers (value DOUBLE);
INSERT INTO numbers (value) VALUES (1), (2.718), (10), (100);

SELECT value, LN(value) AS natural_log FROM numbers;

Output:

value natural_log
1 0
2.718 ~1.000
10 ~2.302
100 ~4.605

Notes:

  • If you need logarithms with a different base, use the LOG(base, number) function or the formula: logb(x)=ln(x)ln(b)\text{log}_{b}(x) = \frac{\ln(x)}{\ln(b)}
  • Example for base-10 logarithm:
    SELECT LOG(10, 100);  -- Returns 2
    

Let me know if you need more clarification!

No comments:

Post a Comment