Friday, 9 March 2012

The ldexp Function in C & C++

More functions from the  <math.h> or <cmath> header files. Today it’s about the ldexp function.

Given two values for a and n, the function returns y such that y = a x 2n.

From C11 standard.

Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
     // Header.
     printf("The ldexp Function in C & C++\n\n");
     printf("Given a and n, the function returns\n");
     printf("a x 2 to the nth power.\n\n");

     int i;

     printf(" a x 2 ^  n\n\n");
     for (i = 0; i < 16; ++i)
           printf("10 x 2 ^ %2d = %8.2f\n", i, ldexp(10.0, i));

     // Keep console window open.

     // Return some value.
     return 0;
} // end main


No comments:

Post a Comment