C library function - ldexp()
Description
C Library Function double ldexp(double x, int exponent) Return x Multiplies by 2 raised to the exponent power.
ldexp() is a function in the C standard library a function in the C standard library used to compute x * 2^expIt is usually used together with frexp() to recombine the significand and exponent parts of a floating-point number into a floating-point value. This is very useful in numerical computation and advanced floating-point operations.
Declaration
Here is the declaration of the ldexp() function.
#include
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
Parameters
x: the floating-point number to combine with the exponent.exp: the exponent with base 2.
Return Value
- Return
x * 2^exp, with the same type as the inputxvalue.
Example
The following example demonstrates the use of the ldexp() function.
Example
#include
#include
int
main
(
)
{
double
x
,
ret
;
int
n
;
x
=
0.65
;
n
=
3
;
ret
=
ldexp
(
x
,
n
)
;
printf
(
"%f * 2^%d = %f\n"
,
x
,
n
,
ret
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
0.650000 * 2^3 = 5.200000
The difference between frexp() Used together with the functions above
The following example shows how to use frexp() and ldexp() to decompose 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.
Error handling
ldexp() The function does not return an error status, but in some cases the input may cause overflow or underflow. For results that are too large or too small, it may return positive infinity, negative infinity, or zero. Use the isinf() and isnan() function to detect these conditions.
The following example shows how to handle possible overflow and underflow situations:
Example
#include
#include
#include
int
main
(
)
{
double
x
=
1.0
;
int
exponent
=
1024
;
double
result
=
ldexp
(
x
,
exponent
)
;
if
(
isinf
(
result
)
)
{
printf
(
"Overflow: ldexp(%f, %d) results in infinity.\n"
,
x
,
exponent
)
;
}
else
if
(
result
==
0.0
&&
errno
==
ERANGE
)
{
printf
(
"Underflow: ldexp(%f, %d) results in zero.\n"
,
x
,
exponent
)
;
}
else
{
printf
(
"ldexp(%f, %d) = %f\n"
,
x
,
exponent
,
result
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Overflow: ldexp(1.000000, 1024) results in infinity.
Code Analysis
- Define a floating-point number
xand a very large integerexponent. - called
ldexp(x, exponent)Computex * 2^exponent, with the result stored inresultit. - Use
isinf()Check whether the result is infinite and handle the overflow condition. - Check whether the result is zero and handle the underflow condition.
- Print the computation result.
Summary
ldexp() The function is used to compute x * 2^exp, and is an important tool for handling floating-point operations. By making proper use of ldexp(), efficient operations on floating-point numbers can be achieved in numerical analysis, scientific computing, and engineering applications, especially when combined with the frexp() function.
C Standard Library -