In MariaDB, LOG()
is a built-in function that returns the natural logarithm of its argument to a given base.
It can be called with one or two arguments:
- When called with one argument, returns the natural logarithm of the argument.
- When called with two arguments, returns the logarithm of the second argument to the base specified in the first argument.
When called with a single argument, LOG()
is the inverse of EXP()
, and it’s the same as using LN()
.
Syntax
The function can be used in the following two ways:
LOG(X)
LOG(B,X)
When using the first syntax, the function returns the natural logarithm of X
.
When using the second syntax, the function returns the logarithm of X
to the base B
.
Example – Single Argument Syntax
Here’s an example that uses the single argument syntax:
SELECT LOG(3);
Result:
+--------------------+ | LOG(3) | +--------------------+ | 1.0986122886681098 | +--------------------+
Here’s what we get when we pass e:
SELECT LOG(2.718281828459045);
Result:
+------------------------+ | LOG(2.718281828459045) | +------------------------+ | 1 | +------------------------+
The number e, also known as Euler’s number, is a mathematical constant approximately equal to 2.718281828459045… and so on.
Example – Two Argument Syntax
Here’s an example that uses the two argument syntax:
SELECT LOG(10, 3);
Result:
+--------------------+ | LOG(3) | +--------------------+ | 1.0986122886681098 | +--------------------+
Here’s the same value using different bases:
SELECT
LOG(8, 3),
LOG(16, 3),
LOG(2, 3);
Result:
+--------------------+--------------------+--------------------+ | LOG(8, 3) | LOG(16, 3) | LOG(2, 3) | +--------------------+--------------------+--------------------+ | 0.5283208335737188 | 0.3962406251802891 | 1.5849625007211563 | +--------------------+--------------------+--------------------+
Argument Ranges
If X
is less than or equal to 0
, then NULL
is returned with a warning.
SELECT
LOG(0),
LOG(-1),
LOG(16, -3),
LOG(2, -3);
Result:
+--------+---------+-------------+------------+ | LOG(0) | LOG(-1) | LOG(16, -3) | LOG(2, -3) | +--------+---------+-------------+------------+ | NULL | NULL | NULL | NULL | +--------+---------+-------------+------------+ 1 row in set, 4 warnings (0.001 sec)
Let’s check the warnings:
SHOW WARNINGS;
Result:
+---------+------+---------------+ | Level | Code | Message | +---------+------+---------------+ | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | +---------+------+---------------+
Also, the base must be greater than 1
. If not, NULL
is returned:
SELECT
LOG(0, 3),
LOG(1, 3),
LOG(-1, 3);
Result:
+-----------+-----------+------------+ | LOG(0, 3) | LOG(1, 3) | LOG(-1, 3) | +-----------+-----------+------------+ | NULL | NULL | NULL | +-----------+-----------+------------+ 1 row in set, 3 warnings (0.000 sec)
Check the warnings:
SHOW WARNINGS;
Result:
+---------+------+---------------+ | Level | Code | Message | +---------+------+---------------+ | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | +---------+------+---------------+
Non-Numeric Arguments
Here’s an example of what happens when we provide non-numeric arguments:
SELECT LOG('Homer', 'Simpson');
Result:
+-------------------------+ | LOG('Homer', 'Simpson') | +-------------------------+ | 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: 'Homer' | | Warning | 1365 | Division by 0 | +---------+------+-------------------------------------------+
Null Arguments
LOG()
returns null
if any argument is null
:
SELECT
LOG(null),
LOG(null, 3),
LOG(16, null),
LOG(null, null);
Result:
+-----------+--------------+---------------+-----------------+ | LOG(null) | LOG(null, 3) | LOG(16, null) | LOG(null, null) | +-----------+--------------+---------------+-----------------+ | NULL | NULL | NULL | NULL | +-----------+--------------+---------------+-----------------+
Missing Arguments
Calling LOG()
with the wrong number of arguments, or without any arguments results in an error:
SELECT LOG();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG'
And:
SELECT LOG(10, 2, 3);
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG'