ldexp() Function
The ldexp()
function in C scales a floating-point value by a power of two, effectively generating a new value by multiplying the given number with two raised to an integer power. This operation is particularly useful for manipulating floating-point values without resorting to explicit bit-level operations.
Syntax of ldexp()
double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
Parameters
Parameter | Description |
---|---|
x | A floating point value representing the significand. |
exp | An integer representing the exponent, indicating the power of 2 to multiply by. |
It is worth noting that if the magnitude of the computed result is too large to be represented by the return type, the function returns a special value (such as HUGE_VAL
, HUGE_VALF
, or HUGE_VALL
) and an overflow error is indicated. This error can be detected either by checking the global variable errno
if MATH_ERRNO
is set or by examining the floating-point exception flags if MATH_ERREXCEPT
is used.
Examples for ldexp()
Example 1: Scaling a Number by a Positive Exponent
This example demonstrates how to scale a floating-point number by multiplying it with a positive power of two using ldexp()
.
Program
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.14;
int exponent = 3;
// Compute 3.14 * 2^3
double result = ldexp(value, exponent);
printf("Result of scaling: %f\n", result);
return 0;
}
Explanation:
- A floating-point number is initialized with the value
3.14
. - An integer exponent
3
is specified to scale the number by2^3
. - The
ldexp()
function computes the product of the number and2^3
. - The resulting value is printed to the console.
Program Output:
Result of scaling: 25.120000
Example 2: Scaling a Number by a Negative Exponent
This example shows how ldexp()
can be used with a negative exponent to reduce a floating-point number.
Program
#include <stdio.h>
#include <math.h>
int main() {
double value = 16.0;
int exponent = -2;
// Compute 16.0 * 2^(-2)
double result = ldexp(value, exponent);
printf("Result of scaling with negative exponent: %f\n", result);
return 0;
}
Explanation:
- A floating-point number is initialized with the value
16.0
. - An integer exponent
-2
is specified to scale the number by2^(-2)
. - The
ldexp()
function computes the operation, effectively dividing the number by 4. - The computed result is printed to the console.
Program Output:
Result of scaling with negative exponent: 4.000000
Example 3: Handling Overflow in ldexp() Operation
This example illustrates how an overflow can be detected when the result of scaling a number exceeds the representable range.
Program
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
double value = 1.5;
int exponent = 1024; // A large exponent that may cause overflow
// Compute 1.5 * 2^1024, which is likely to cause an overflow
double result = ldexp(value, exponent);
if (result == HUGE_VAL || result == -HUGE_VAL) {
perror("Overflow occurred");
} else {
printf("Result: %f\n", result);
}
return 0;
}
Explanation:
- A floating-point number is set to
1.5
. - An extremely large exponent is chosen to intentionally cause an overflow.
- The
ldexp()
function computes the result of the scaling operation. - The program checks whether the result equals
HUGE_VAL
(or its negative), indicating an overflow, and prints an appropriate error message.
Program Output:
Overflow occurred: Numerical result out of range