Monday, 12 March 2012

The pow Function in C & C++

More functions from the  <math.h> or <cmath> header files. Today it’s about the pow function.
The pow function is straightforward. Given values for x and n, the function returns x to the nth power.

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 pow Function in C & C++\n\n");
     printf("Given x and n, the function returns x to the nth power.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double x, n;
     // Result.
     double y;

     printf(" y    =    x   ^ n\n\n");
     for (i = 1; i < 10; ++i)
           x = 12.3456 / i;
           n = i;
           y = pow(x, n);
           printf ("%5.2f = %6.3f ^ %1.0f\n", y, x, n);

     // Keep console window open.

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


No comments:

Post a Comment