In MariaDB, LN()
is a built-in function that returns the natural logarithm of its argument. In other words, it returns the base-e logarithm of its argument.
The number e, also known as Euler’s number, is a mathematical constant approximately equal to 2.71828.
LN()
is the inverse of EXP()
, and it’s the same as using LOG()
with the single argument syntax.
Syntax
The syntax goes like this:
LN(X)
Where X
is the value for which to return the natural logarithm from.
Example
Here’s an example:
SELECT LN(3);
Result:
+--------------------+ | LN(3) | +--------------------+ | 1.0986122886681098 | +--------------------+
Here’s what we get when we pass e:
SELECT LN(2.718281828459045);
Result:
+-----------------------+ | LN(2.718281828459045) | +-----------------------+ | 1 | +-----------------------+
Expressions
The argument can include expressions like this:
SELECT LN(2 * 3);
Result:
+-------------------+ | LN(2 * 3) | +-------------------+ | 1.791759469228055 | +-------------------+
Zero Argument
An argument of 0
returns null
with a warning:
SELECT LN(0);
Result:
+-------+ | LN(0) | +-------+ | NULL | +-------+ 1 row in set, 1 warning (0.000 sec)
Show the warning:
SHOW WARNINGS;
Result:
+---------+------+---------------+ | Level | Code | Message | +---------+------+---------------+ | Warning | 1365 | Division by 0 | +---------+------+---------------+
Negative Values
Negative values return null
with a warning:
SELECT LN(-2);
Result:
+--------+ | LN(-2) | +--------+ | NULL | +--------+ 1 row in set, 1 warning (0.002 sec)
Non-Numeric Arguments
Here’s an example of what happens when we provide a non-numeric argument:
SELECT LN('Cat');
Result:
+-----------+ | LN('Cat') | +-----------+ | NULL | +-----------+ 1 row in set, 2 warnings (0.000 sec)
Let’s see the warning:
SHOW WARNINGS;
Result:
+---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: 'Cat' | | Warning | 1365 | Division by 0 | +---------+------+-----------------------------------------+
Null Arguments
LN()
returns null
if the argument is null
:
SELECT LN(null);
Result:
+----------+ | LN(null) | +----------+ | NULL | +----------+
Missing Arguments
Calling LN()
with the wrong number of arguments, or without any arguments results in an error:
SELECT LN();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LN'
And:
SELECT LN(10, 2);
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LN'