A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/log2-function-in-c-with-examples/ below:

log2() function in C++ with Examples

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:

Below examples demonstrate the use of log2() method:

Example 1: CPP
// C++ program to illustrate log2() function

#include <bits/stdc++.h>
using namespace std;

// Driver Code
int main()
{
    long b = 16;
    float c = 2.5;
    double d = 10.35;
    long double e = 25.5;

    // Logarithmic value of long datatype
    cout << log2(b) << "\n";

    // Logarithmic value of float datatype
    cout << log2(c) << "\n";

    // Logarithmic value of double datatype
    cout << log2(d) << "\n";

    // Logarithmic value of long double datatype
    cout << log2(e) << "\n";

    return 0;
}
Output:
4
1.32193
3.37156
4.67243
Example 2: CPP
// C++ program to illustrate log2() function

#include <bits/stdc++.h>
using namespace std;

// Driver Code
int main()
{
    // To show extreme cases
    int a = 0;
    int b = -16;

    // Logarithmic value of 0
    cout << log2(a) << "\n";

    // Logarithmic value of negative value
    cout << log2(b) << "\n";

    return 0;
}
Reference: https://cplusplus.com/reference/cmath/log2/

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4