VOOZH about

URL: https://www.geeksforgeeks.org/cpp/log2-function-in-c-with-examples/

⇱ log2() function in C++ with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

log2() function in C++ with Examples

Last Updated : 12 Jul, 2025
The function log2() of cmath header file in C++ is used to find the logarithmic value with base 2 of the passed argument. Syntax:
log2(x)
Parameters: This function takes a value x, in the range [0, ∞] whose log value is to be found. Return Type: It returns the logarithmic value, as double, float or long double type, based on the following conditions:
  • If x > 1: It returns the positive logarithmic value of x.
  • If x is equals to 1: It returns 0.
  • If 0 < x < 1: It returns the negative logarithmic value of x.
  • If x is equals to 0: It returns the negative infinity(-∞).
  • If x < 0: It returns NaN(Not a Number).
Below examples demonstrate the use of log2() method: Example 1:
Output:
4
1.32193
3.37156
4.67243
Example 2:
Output:
-inf
nan
Reference: https://cplusplus.com/reference/cmath/log2/
Comment
Article Tags: