w3resource

C ldexp() function

C ldexp() function - Load exponent of a floating-point number

Syntax:

double ldexp(double x, int exponent)

The ldexp() function is used to calculate the value of x * (2aexp).

Parameters:

Name Description Required /Optional
x Floating-point value. Required
exponent Integer exponent. Required

Return value from ldexp()

  • returns the value of x * (2aexp).

Example: ldexp() function

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

#include <math.h>
#include <stdio.h>
 
int main(void)
{
   double x, y;
   int p;
 
   x = 3;
   p = 2;
   y = ldexp(x,p); 
   printf("%lf times 2 to the power of %d is %lf\n", x, p, y);
  
   x = 3;
   p = 3;
   y = ldexp(x,p); 
   printf("\n%lf times 2 to the power of %d is %lf\n", x, p, y);
   
   x = 1.5;
   p = 5;
   y = ldexp(x,p);
 
   printf("\n%lf times 2 to the power of %d is %lf\n", x, p, y);
}

Output:

3.000000 times 2 to the power of 2 is 12.000000

3.000000 times 2 to the power of 3 is 24.000000

1.500000 times 2 to the power of 5 is 48.000000

C Programming Code Editor:

Previous C Programming: C frexp()
Next C Programming: C log()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
{
    ptr = 0;// oops, I meant *ptr = 0
}

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q