home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / POWER / PowerTest.pas < prev   
Pascal/Delphi Source File  |  1998-05-11  |  3KB  |  96 lines

  1. {$APPTYPE CONSOLE}
  2.  
  3. (*
  4.  * This program is not a Delphi project. It is a stand-alone
  5.  * CRT application, which must be run from a DOS prompt. Follow
  6.  * these steps to load the program into Delphi, compile,
  7.  * and run:
  8.  *
  9.  * 1. Close any files now open.
  10.  * 2. Use File|Open..., select *.pas files, open PowerTest.pas.
  11.  * 3. Press Ctrl+F9 (Project|Compile) to create PowerTest.exe.
  12.  * 4. Open a DOS-prompt window.
  13.  * 5. Change to the Source\Power directory.
  14.  * 6. Type PowerTest to run the test program.
  15.  *
  16.  * Note 1: If you run the CRT application using Explorer, the
  17.  * DOS window closes as soon as the program ends. To prevent
  18.  * this, run the program *after* opening a DOS prompt window.
  19.  *
  20.  * Note 2: The program intentionally displays four error
  21.  * messages to demonstrate the Power function's exceptions.
  22.  *)
  23.  
  24. program PowerTest;
  25.  
  26. uses SysUtils;
  27.  
  28. const
  29.   sFmt = 'Exception raised in Power function. Base=%f Exp=%f';
  30.  
  31. type
  32. { Declare exception class }
  33.   EPower = class(EMathError);
  34.  
  35. { Return Base raised to Exponent }
  36. function Power(Base, Exponent: Double): Double;
  37.   function F(B, E: Double): Double;
  38.   begin
  39.     Result := Exp(E * Ln(B));
  40.   end;
  41. begin
  42.   if Base = 0.0 then
  43.     if Exponent = 0.0 then
  44.       Result := 1.0
  45.     else if Exponent < 1.0 then
  46.       raise EPower.CreateFmt(sFmt, [Base, Exponent])
  47.     else
  48.       Result := 0.0
  49.   else if Base > 0.0 then
  50.     Result := F(Base, Exponent)
  51.   else if Frac(Exponent) = 0.0 then
  52.     if Odd(Trunc(Exponent)) then
  53.       Result := -F(-Base, Exponent)
  54.     else
  55.       Result :=  F(-Base, Exponent )
  56.   else raise EPower.CreateFmt(sFmt, [Base, Exponent]);
  57. end; { Power }
  58.  
  59. { Test procedure }
  60. procedure Test(Base, Exponent: Double);
  61. begin
  62.   try
  63.     Writeln(Base:8:3, ' ^ ', Exponent:8:3, ' = ',
  64.       Power(Base, Exponent));
  65.   except
  66.     on E: EPower do
  67.       Writeln(E.Message);
  68.   end;
  69. end;
  70.  
  71. { The following is the CRT application's main body. It
  72.   merely calls the test procedure with various values. The
  73.   final four values intentionally test the Power function's
  74.   exceptions. }
  75. begin
  76.   test( 7,    3  );
  77.   test( 7,   -3  );
  78.   test(-7,    3  );
  79.   test(-7,   -3  );
  80.   test( 7,    3.5);
  81.   test( 7,   -3.5);
  82.   test( 7.2,  3  );
  83.   test( 7.2, -3  );
  84.   test(-7.2,  3  );
  85.   test(-7.2, -3  );
  86.   test( 7.2,  3.5);
  87.   test( 7.2, -3.5);
  88. { These four tests produce *expected* exceptions }
  89.   test(-7,    3.5);
  90.   test(-7,   -3.5);
  91.   test(-7.2,  3.5);
  92.   test(-7.2, -3.5);
  93.   test( 0,    0.5);
  94. end.
  95.  
  96.