C library function -ldexp()

C standard library - <math.h>

Description

C Library Functionsdouble ldexp(double x, int exponent)returnxtimes 2 to theexponentpower.

ldexp()is part of the C standard library<math.h>One of the functions used to computex * 2^exp. It is usually withfrexp()When used together, the significand and exponent parts of a floating-point number are recombined into a floating-point number. This is very useful in numerical computation and advanced operations on floating-point numbers.

Declaration

Below is the declaration of the ldexp() function.

#include <math.h>

double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);

Parameter

  • xThe floating-point number to be combined with the exponent.
  • expThe base-2 exponent.

Return Value

  • returnx * 2^exp, with the same type as the inputxconsistent.

Example

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

Example

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

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 us compile and run the above program, which will produce the following result:

0.650000 * 2^3 = 5.200000

andfrexp()used in combination

The following example shows how to usefrexp()andldexp()to decompose and reconstruct a floating-point number:

Example

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

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 us 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 explanation

  • define a floating-point numberx, the value is 123.456.
  • Usagefrexp()willxDecompose into a significand and an exponent.
  • Usageldexp()Reconstruct the original floating-point number from the significand and exponent.
  • Print the original value, significand, exponent, and the 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, positive infinity, negative infinity, or zero may be returned. Using theisinf()andisnan()function can detect these conditions.

The following example shows how to handle possible overflow and underflow conditions:

Example

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

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 us compile and run the above program, which will produce the following result:

Overflow: ldexp(1.000000, 1024) results in infinity.

Code explanation

  • define a floating-point numberxand a very large integerexponent。
  • Callldexp(x, exponent)Computex * 2^exponent, the result is stored inresultIn.
  • Usageisinf()Check whether the result is infinity, and handle overflow cases.
  • Check whether the result is zero, and handle underflow cases.
  • Print the calculation result.

Summary

ldexp()The function is used to calculatex * 2^expis an important tool for handling floating-point operations. Through reasonable useldexp()can achieve efficient handling of floating-point numbers in numerical analysis, scientific computing, and engineering applications, especially when it is necessary tofrexp()when used in combination with the function.

C standard library - <math.h>

other extensions