Home C Math Functions logb() C logb() Function Definition In C, logb() returns the exponent of a floating-point number as a double. Think of x as significand × radixexponent . For binary floating point, logb(16) is 4 because 16 is 1 × 24 in that form.
You pass one double. The result is a double holding the unbiased exponent. Include <math.h>. On GCC, link with -lm. For an int exponent, see ilogb() .
logb looks at the floating-point encoding scale, not a user-chosen log base in the everyday sense. It pairs well with scalbn() and frexp() .
Syntax Pass the floating-point number whose exponent you want.
Call What it does logb(16.0)Returns 4.0 for a typical binary floating-point radix.
Include <math.h>. On GCC, link with -lm. Special values like zero, infinity, and NaN have special results.
Library declarations double logb(double x);
float logbf(float x);
long double logbl(long double x); Parameters Parameter Description x Required. The floating-point value whose exponent you want to read.
Return Value Return Value Description double The unbiased exponent of x. For zero it may be -infinity; for NaN it stays NaN.
C Examples 1. Basic logb 2. Scale check 3. With scalbn Example 1 — Basic Exponent Read Read exponents for a few powers of two.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", logb(1.0));
printf("%.1f\n", logb(8.0));
printf("%.1f\n", logb(0.25));
return 0;
} How It Works logb(1). 1 is 1 × 2^0, so the exponent is 0.logb(8). 8 is 1 × 2^3, so the exponent is 3.logb(0.25). 0.25 is 1 × 2^-2, so the exponent is -2.Sign ignored. The magnitude sets the exponent.Example 2 — Compare Magnitudes Use exponents to see which value is larger in scale.
#include <stdio.h>
#include <math.h>
int main(void) {
double a = 1.5e3;
double b = 9.0e1;
printf("exp a = %.0f\n", logb(a));
printf("exp b = %.0f\n", logb(b));
return 0;
} How It Works Different sizes. a is much larger than b.Read exponents. Larger magnitude usually means a larger exponent.Quick compare. You can sort by scale without full division.Still a double. Remember the return type is floating, not int.Example 3 — Rebuild with scalbn Peel off the exponent, then put a number back on that scale.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 48.0;
double e = logb(x);
double y = scalbn(1.5, (int)e);
printf("logb = %.0f\n", e);
printf("y = %.1f\n", y);
return 0;
} How It Works Read exponent. logb(48) is 5 on a binary system.Pick significand. Here we choose 1.5 on purpose.scalbn rebuilds. 1.5 × 2^5 equals 48.Related tools. frexp and ldexp solve a similar split/join job.Frequently Asked Questions What does logb() do in C? logb(x) returns the floating-point exponent of x as a double.
What is the parameter of logb()? x is required. It is the value whose exponent you want.
What does logb() return? A double holding the unbiased exponent of x.
Which header file does logb() need? Include <math.h>. On GCC, link with -lm.
How is logb() different from log2()? log2() is the mathematical base-2 log. logb() returns the floating-point encoding exponent.
Next: lrint() Round a floating-point value to long using the current rounding mode.
lrint() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion