C Math frexp() Function
Example
Split the number 12 into a fraction and a power of 2:
int n;
double m = frexp(12.0, &n);
printf("%f\n", m);
printf("%d", n);
Definition and Usage
The frexp() function splits a number x into a fraction m and a power of 2 n, so that x is m times 2n. The fraction is from 0.5 up to (but not including) 1.0. For example, 12 is 0.75 times 24 (16).
The function returns m, and writes n to the int that y points to. y is a pointer (it stores the memory address of a variable). In the example, &n gives the memory address of n, so the function can change n.
The frexp() function is defined in the <math.h> header file.
Tip: ldexp() does the opposite: ldexp(0.75, 4) returns 12.
Syntax
double frexp(double x, int *y);
There are also versions for float and long double values: frexpf() and frexpl().
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to split. |
| y | Required. A pointer to an int. The power of 2 is written to this int. |
Technical Details
| Returns: | A double value with the fraction. If x is 0, both the fraction and the power are 0. |
|---|
Related Pages
Read more about math in our C Math Tutorial.