Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

# What is log2f() in C?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

log2f() in C calculates the base-2 logarithm of a floating point value.

log2f() is defined in the math.h header file and the function declaration is as follows:

float log2f(float arg);


## Parameters and return value

arg: floating-point value

 Input Output Error Raised > 0 and no error occurs base-2 logarithm - 1 +0 - +∞ +∞ - NaN NaN - < 0 NaN Domain error ±0 -∞ Pole error

## Example program

#include <math.h>                                                               #include <stdio.h>                                                                                                                                              int main(void)                                                                  {           printf("log2f(8) = %f\n", log2f(8));    printf("log2f(0.25) = %f\n", log2f(0.25));    /* special cases */    printf("log2f(1) = %f\n", log2f(1));    printf("log2f(0) = %f\n", log2f(0));    printf("log2f(0) = %f\n", log2f(-8));    printf("log2f(+inf) = %f\n", log2f(INFINITY));    printf("log2f(nan) = %f\n", log2f(NAN));    return 0;                     }

NOTE: You need to link the math library with the -lm flag when compiling any program that uses log2f().

## Explanation

First, we import the math.h header file from the C standard library. The program then prints the output of log2f() on possible example values. You can confirm the output using the table above.

RELATED TAGS

c

CONTRIBUTOR

Abdul Monum 