log2()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Nov 28, 2022
Contribute to Docs

The log2() function returns the base-2 logarithm of the argument.

Syntax

log2(x)

If the x parameter is negative (less than zero), then a domain error will occur. If x is equal to zero, a pole error may occur where the result has reached the point of infinity and cannot be returned.

Example

The following example uses the log2() function to find the base-2 log of 1024:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 1024;
double result;
result = log2(x);
cout << "The log to the base of 2 of " << x << " is " << result << "\n";
// Output: The log to the base of 2 of 1024 is 10
}

Codebyte Example

The following example is runnable and returns the base-2 log of 8:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy