Skip to content
Related Articles

Related Articles

log2, log2f, log2l Functions in C

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Apr, 2022

log2, log2f, and log2l are functions in C that compute the logarithmic of base 2 of a given number. They are part of the math.h header file.

Syntax:

#include <math.h>

double log2(double x);
float log2f(float x);
long double log2l(long double x);

Parameters:

Function Parameter
log2(x) x => double
log2f(x) x => float
log2l(x) x => long double

Return Values:

Function Return Value
log2(x)  log2(x) => double
log2f(x)  log2(x) => float
log2l(x)  log2(x) => long double

Example 1: Below is the C program to implement log2(x):

C




// C program to implement
// log2(x)
#include <math.h>
#include <stdio.h>
  
// Driver code
int main()
{
    double result, num = 67.9;
    result = log2(num);
    printf("log2(%lf) = %lf"
            num, result);
    return 0;
}


Output

log2(67.900000) = 6.085340

Example 2: Below is the C program to implement log2f(x):

C




// C program to implement 
// log2f(x)
#include <stdio.h>
#include <math.h>
  
// Driver code 
int main()
{
    float result, num = 32.256;
    result = log2f(num);
    printf("log2(%f) = %f"
            num, result);
    return 0;
}


Output

log2(32.256001) = 5.011496

Example 3: Below is the C program to implement log2l(x):

C




// C program to implement
// log2l(x)
#include <math.h>
#include <stdio.h>
  
// Driver code 
int main()
{
    long double result, num = 73.1256;
    result = log2l(num);
    printf("log2(%Lf) = %LF"
            num, result);
    return 0;
}


Output

log2(73.125600) = 6.192305

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!