Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>pow() calculate x raised to the yth power</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
pow()                    Calculate X Raised to the Yth Power

 #include   <math.h>

 double     pow(x,y);
 double     x;                           Number to be raised
 double     y;                           Power of x

    pow() calculates the value of 'x' raised to the power of 'y'.

    Returns:    'x' raised to the power of 'y'.  If 'x' is 0.0 and 'y' is
                negative, matherr() is called with a DOMAIN error, a
                DOMAIN error message is printed to 'stderr', and 'errno'
                (defined in <stdlib.h>) is set to ERANGE (defined in
                <math.h>).

    If 'x' is negative and 'y' is not an integer, matherr() is called
    with a DOMAIN error, a DOMAIN error message is printed to 'stderr',
    'errno' is set to EDOM (defined in <math.h>), and 0.0 is returned. On
    overflow, matherr() is called with an OVERFLOW error, 'errno' is set
    to ERANGE, and positive or negative HUGE (defined in <math.h>) is
    returned.  On underflow 0.0 is returned. (matherr() is not called on
    an underflow and 'errno' is not set.)

      Notes:    The value of any 'x' raised to the power of 0.0 is 1.0.

  -------------------------------- Example ---------------------------------

    The following statements calculate x raised to the power of y for
    several pair of values in an array, then report any errors:

         #include <math.h>        /* for pow(), HUGE, ERANGE, and EDOM */
         #include <stdio.h>       /* for printf() */
         #include <stdlib.h>      /* for errno */

         double vals[6][2]={{-3.0, 4.0},   /* 81.0 */
                            {0.0, 0.0},    /*  1.0 */
                            {0.0, -1.0},   /*  0.0 (ERANGE error) */
                            {-1.0, 1.1},   /*  0.0 (EDOM error) */
                            {9.9, 999.9},  /* HUGE (OVERFLOW: ERANGE error)*/
                            {10.0, -444.0}};/*  0.0 (UNDERFLOW: no error) */

         main()
         {
             double x_to_the_y;
             int i;

             for (i = 0;  i < 6;  i++) {
                 errno = 0;
                 x_to_the_y = pow(val[i][0], val[i][1]);
                 /* on any error except an OVER- or UNDERFLOW, an error
                  message is also printed to stderr */
                 if (errno == EDOM)
                     printf("EDOM error in pow()\n");
                 else if (errno == ERANGE)
                     if (x_to_the_y == HUGE)
                         printf("OVERFLOW error in pow()\n");
                     else
                         printf("ERANGE error in pow()\n");
                 printf("%f raised to the %f = %e\n",
                        value[i][0], value[i][1], x_to_the_y);
             }
         }

See Also: exp() log() log10() sqrt()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson