home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / FUNC.PAS < prev    next >
Pascal/Delphi Source File  |  1993-06-03  |  2KB  |  63 lines

  1. program PowerFunction;
  2.  
  3. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  4. { Program to illustrate the use of a function, in this case to evaluate  }
  5. { a number raised to a power ( x ^ y ). The number is referred to as the }
  6. { base value and the power is the exponent. Negative base values with    }
  7. { fractional exponents are excluded in this simple program as they would }
  8. { require the use of complex numbers. A unit POWERU.TPU is available to  }
  9. { handle complex solutions.                                              }
  10. {                                                                        }
  11. { FUNC.PAS  -> FUNC.EXE       R Shaw            13.1.93                  }
  12. {________________________________________________________________________}
  13.  
  14. uses Crt;       {The Crt unit contains the procedure to clear the screen}
  15.  
  16. var
  17.   x,y   : real;
  18.   sign  : integer;
  19.   reply : char;
  20.  
  21. function Power(Base, Exponent : real) : real;
  22. begin
  23.   if Base > 0.0 then
  24.     Power := exp(Exponent * ln(Base))
  25.   else if Base = 0.0 then
  26.     Power := 0.0
  27.   else if ((Base < 0.0) and (round(Exponent) = Exponent)) then
  28.     begin
  29.       if abs(round(Exponent) mod 2) = 1 then sign := -1 else sign := +1;
  30.       Power := sign * exp(Exponent * ln(Abs(Base)));
  31.     end
  32.   else if ((Base < 0.0) and (round(Exponent) <> Exponent)) then exit
  33. end;
  34.  
  35. begin
  36.    ClrScr;
  37.    writeln('      FUNCTION FOR EVALUATING POWERS OF NUMBERS.');
  38.    writeln;
  39.    writeln('Please enter the base and exponent for x^y when requested. ');
  40.    writeln('In each case press the ENTER key after typing the values.');
  41.    writeln;
  42.    writeln('High values for x and particularly y may cause floating point overflow.');
  43.    writeln('Please restart program if this happens.');
  44.    Repeat
  45.      writeln;
  46.      writeln;
  47.      write('Base value x: ');
  48.      readln(x);
  49.      write('Exponent y  : ');
  50.      readln(y);
  51.      writeln;
  52.      If ((x < 0.0) and (round(y) <> y)) then
  53.         begin
  54.           writeln('   Result not possible without use of complex numbers');
  55.           writeln('   A unit POWERU.TPU is available for this purpose.');
  56.         end
  57.      else writeln('   ',x:6:3,' ^ ',y:6:3,' = ',Power(x,y):6:3);
  58.      writeln;
  59.      write('Press C to continue or Q to quit ');
  60.      Reply := readkey;
  61.    Until UpCase(Reply) = 'Q';
  62. end.
  63.