C Library Function - frexp()
Description
C library function *double frexp(double x, int exponent) Break the floating-point number x into a mantissa and an exponent. The return value is the mantissa, and the exponent is stored in exponent . The resulting value is x = mantissa * 2 ^ exponent.
frexp() is a function in the C standard library is a function in that splits a floating-point number into a significand and an exponent with base 2. It is often used for advanced operations on floating-point numbers, such as normalization and numerical analysis.
Declaration
Below is the declaration of the frexp() function.
#include
double frexp(double x, int *exp);
float frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);
Parameters
x: the floating-point number to be split.exp: a pointer to an integer where the base-2 exponent is stored.
Return Value
- Return
xthe significand part of , such thatx = mantissa * 2^exp, wheremantissais the return value, with a valid range of 0.5 (inclusive) to 1.0 (exclusive), or -0.5 (inclusive) to -1.0 (exclusive),expis the exponent.
Example
The following example demonstrates the use of the frexp() function.
Example
#include
#include
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
Handling multiple floating-point numbers
The following example shows how to handle multiple floating-point numbers:
Example
#include
#include
int
main
(
)
{
double
values
[
]
=
{
0.5
,
1.0
,
4.0
,
10.0
,
16.0
}
;
int
num_values
=
sizeof
(
values
)
/
sizeof
(
values
[
0
]
)
;
for
(
int
i
=
0
;
i
<
num_values
;
i
++
)
{
double
x
=
values
[
i
]
;
int
exponent
;
double
mantissa
=
frexp
(
x
,
&
exponent
)
;
printf
(
"x = %f, mantissa = %f, exponent = %d\n"
,
x
,
mantissa
,
exponent
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
x = 0.500000, mantissa = 0.500000, exponent = 0
x = 1.000000, mantissa = 0.500000, exponent = 1
x = 4.000000, mantissa = 0.500000, exponent = 3
x = 10.000000, mantissa = 0.625000, exponent = 4
x = 16.000000, mantissa = 0.500000, exponent = 5
Code Analysis
- Define an array containing multiple floating-point numbers
values. - Use
for, and loop through each value, calling thefrexp()to split them and print the results.
Use frexp() Practical applications of
frexp() is commonly used for normalized representations of floating-point numbers and various algorithms in numerical analysis. It helps avoid overflow and underflow problems when manipulating floating-point numbers directly.
The following example shows how to use frexp() and ldexp()(corresponding to frexp() related functions) to split and reconstruct floating-point numbers:
Example
#include
#include
int
main
(
)
{
double
x
=
123.456
;
int
exponent
;
double
mantissa
=
frexp
(
x
,
&
exponent
)
;
// Reconstruct the floating-point number
double
reconstructed_x
=
ldexp
(
mantissa
,
exponent
)
;
printf
(
"Original x = %f\n"
,
x
)
;
printf
(
"Mantissa = %f, Exponent = %d\n"
,
mantissa
,
exponent
)
;
printf
(
"Reconstructed x = %f\n"
,
reconstructed_x
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Original x = 123.456000
Mantissa = 0.964500, Exponent = 7
Reconstructed x = 123.456000
Code Analysis
- Define a floating-point number
xwith the value 123.456. - Use
frexp()data to the child component viaxSplit it into a significand and an exponent. - Use
ldexp()Reconstruct the significand and exponent into the original floating-point number. - Print the original value, significand, exponent, and reconstructed floating-point number.
Summary
frexp() The function is used to split a floating-point number into a significand and an exponent, and is an important tool for handling floating-point operations. By properly using frexp(), efficient operations on floating-point numbers can be achieved in numerical analysis, scientific computing, and engineering applications.
C Standard Library -