home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 138 / pascal / power.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-05-13  |  1.5 KB  |  58 lines

  1. {POWER  FUNCTION                        GARY CURTIS NEWPORT
  2.  
  3.     PURPOSE : POWER raises x to the power y, where x, y are real numbers
  4.               or real variables.  Since y can be a fraction, POWER can
  5.               be used to find roots, as well.
  6.  
  7.     ALGORITHM : See any algebra or calculus text for a discussion of
  8.                 logarithms and exponents.
  9.  
  10.     USE :   Assume x has been declared as a real variable.  To find the
  11.             cube of 5,
  12.  
  13.             x := POWER(5.0,3.0);
  14.  
  15.             To find the cube root of 5,
  16.  
  17.             x := POWER(5.0,(1/3));
  18.  
  19.             To find the reciprocal of the cube root of 5,
  20.  
  21.             x := POWER(5.0,-(1/3));
  22.  
  23.             NOTE : If y is zero, POWER correctly returns a value of 1.0.
  24.                    (Any number raised to the zeroth power equals one.)
  25.  
  26.                    Answers are APPROXIMATIONS; for example,
  27.                    the square root of four is 2.0000000029;
  28.                    two cubed is 7.9999999837.
  29.  
  30.  
  31. }
  32.  
  33.    function POWER (x, y : real) : real;
  34.  
  35.       VAR
  36.           recip : boolean;
  37.           temp  : real;
  38.  
  39.    begin {POWER}
  40.       if (y = 0) then
  41.          POWER := 1.0
  42.       else
  43.          begin
  44.          if (y < 0) then
  45.             begin
  46.             y := ABS(y);
  47.             recip := true
  48.             end {if}
  49.          else
  50.             recip := false;
  51.          temp := EXP( y * LN(x));
  52.          if recip then
  53.             POWER := 1/temp
  54.          else
  55.             POWER := temp
  56.          end {else}
  57.    end; {POWER}
  58.