log2()
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
:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours