In MariaDB, LOG2() è una funzione incorporata che restituisce il logaritmo in base 2 del suo argomento.
Sintassi
La sintassi è questa:
LOG2(X)
Dove X è il valore per il quale restituire il logaritmo in base 2.
Esempio
Ecco un esempio da dimostrare:
SELECT LOG2(3); Risultato:
+--------------------+ | LOG2(3) | +--------------------+ | 1.5849625007211563 | +--------------------+
Eccone altri:
SELECT
LOG2(2),
LOG2(4),
LOG2(8),
LOG2(16); Risultato:
+---------+---------+---------+----------+ | LOG2(2) | LOG2(4) | LOG2(8) | LOG2(16) | +---------+---------+---------+----------+ | 1 | 2 | 3 | 4 | +---------+---------+---------+----------+
Intervalli di argomenti
Se X è minore o uguale a 0 , quindi NULL viene restituito con un avviso.
SELECT
LOG2(0),
LOG2(-1); Risultato:
+---------+----------+ | LOG2(0) | LOG2(-1) | +---------+----------+ | NULL | NULL | +---------+----------+ 1 row in set, 2 warnings (0.000 sec)
Controlliamo gli avvisi:
SHOW WARNINGS; Risultato:
+---------+------+---------------+ | Level | Code | Message | +---------+------+---------------+ | Warning | 1365 | Division by 0 | | Warning | 1365 | Division by 0 | +---------+------+---------------+
Argomenti non numerici
Ecco un esempio di cosa succede quando forniamo argomenti non numerici:
SELECT LOG2('Dog'); Risultato:
+-------------+
| LOG2('Dog') |
+-------------+
| NULL |
+-------------+
1 row in set, 2 warnings (0.000 sec) Vediamo l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: 'Dog' | | Warning | 1365 | Division by 0 | +---------+------+-----------------------------------------+
Argomenti nulli
LOG2() restituisce null se il suo argomento è null :
SELECT LOG2(null); Risultato:
+------------+ | LOG2(null) | +------------+ | NULL | +------------+
Argomenti mancanti
Chiamando LOG2() con il numero errato di argomenti o senza argomenti genera un errore:
SELECT LOG2(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG2'
E:
SELECT LOG2(10, 2); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG2'