Easy Tutorial
❮ C Exercise Example2 C Exercise Example48 ❯

C Library Function - frexp()

C Standard Library - <math.h>

Description

The C library function double frexp(double x, int *exponent) decomposes the floating-point number x into its mantissa and exponent. The return value is the mantissa, and the exponent is stored in exponent. The resulting value is x = mantissa * 2 ^ exponent.

Declaration

Below is the declaration for the frexp() function.

double frexp(double x, int *exponent)

Parameters

Return Value

The function returns the normalized fraction. If the parameter x is not zero, the normalized fraction is a power of 2 of x, and its absolute value ranges from 1/2 (inclusive) to 1 (exclusive). If x is zero, the normalized fraction is zero, and zero is stored in exp.

Example

The following example demonstrates the usage of the frexp() function.

#include <stdio.h>
#include <math.h>

int main ()
{
   double x = 1024, fraction;
   int e;

   fraction = frexp(x, &e);
   printf("x = %.2lf = %.2lf * 2^%d\n", x, fraction, e);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

x = 1024.00 = 0.50 * 2^11

C Standard Library - <math.h>

❮ C Exercise Example2 C Exercise Example48 ❯