The LOG2() function in MySQL calculates and returns the logarithm of a number to base-2.
Syntax
LOG2(number)
Definition and Usage
- The
LOG2()function is used to compute the logarithm of a specified number using base-2. - The input
numbermust be greater than 0. - Commonly used in mathematical and computational scenarios, especially when working with binary systems.
Parameter Values
| Parameter | Description |
|---|---|
number |
Required. A positive numeric value. |
Technical Details
- Introduced in: MySQL 4.0
- Return Value: A floating-point value representing the base-2 logarithm of the input.
Examples
Example 1: Calculate the base-2 logarithm of 6
SELECT LOG2(6);
Output:
2.584963
Example 2: Base-2 logarithm of a power of 2
SELECT LOG2(64);
Output:
6
Explanation: 2^6 = 64.
Example 3: Base-2 logarithm of another number
SELECT LOG2(256);
Output:
8
Explanation: 2^8 = 256.
Related Functions
LOG(): Calculates the logarithm of a number to a specified base or natural logarithm if the base is omitted.LOG10(): Computes the base-10 logarithm of a number.LN(): Returns the natural logarithm (basee) of a number.EXP(): Calculateseraised to the power of the specified number.
Notes
- The
LOG2()function is especially useful in computer science and information theory for applications involving binary logarithms, such as analyzing algorithms or understanding data structures.