VB icon

Power of a Number in Delphi

Email
Submitted on: 9/12/2016 11:41:31 PM
By: Jake R. Pomperada  
Level: Beginner
User Rating: Unrated
Compatibility: Delphi 4
Views: 1928
author picture
 
     Here is a sample program that I wrote in Delphi as my programming language that will ask the user to give the base and exponent value and then our program will compute the power of the number. I hope you will find my work useful I used the power function written by Jack Lyle in this program. The version of Delphi I used in this sample program is Delphi 4 Professional Edition. Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
!**************************************
! Name: Power of a Number in Delphi
! Description:Here is a sample program that I wrote in Delphi as my programming language that will ask the user to give the base and exponent value and then our program will compute the power of the number. I hope you will find my work useful I used the power function written by Jack Lyle in this program. The version of Delphi I used in this sample program is Delphi 4 Professional Edition.
Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
 
My mobile number here in the Philippines is 09173084360.
! By: Jake R. Pomperada
!**************************************

unit power;
interface
uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls;
type
 TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Label3: TLabel;
Edit2: TEdit;
Button1: TButton;
Label4: TLabel;
Button2: TButton;
Label5: TLabel;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
 private
{ Private declarations }
 public
{ Public declarations }
 end;
var
 Form1: TForm1;
implementation
{$R *.DFM}
{** A power function from Jack Lyle. Said to be more powerful than the
Pow function that comes with Delphi. }
function Power2(Base, Exponent : Double) : Double;
{ raises the base to the exponent }
 CONST
cTiny = 1e-15;
 VAR
Power : Double; { Value before sign correction }
 BEGIN
Power := 0;
{ Deal with the near zero special cases }
IF (Abs(Base) < cTiny) THEN BEGIN
 Base := 0.0;
END; { IF }
IF (Abs(Exponent) < cTiny) THEN BEGIN
 Exponent := 0.0;
END; { IF }
{ Deal with the exactly zero cases }
IF (Base = 0.0) THEN BEGIN
 Power := 0.0;
END; { IF }
IF (Exponent = 0.0) THEN BEGIN
 Power := 1.0;
END; { IF }
{ Cover everything else }
IF ((Base < 0) AND (Exponent < 0)) THEN
Power := 1/Exp(-Exponent*Ln(-Base))
ELSE IF ((Base < 0) AND (Exponent >= 0)) THEN
Power := Exp(Exponent*Ln(-Base))
ELSE IF ((Base > 0) AND (Exponent < 0)) THEN
Power := 1/Exp(-Exponent*Ln(Base))
ELSE IF ((Base > 0) AND (Exponent >= 0)) THEN
Power := Exp(Exponent*Ln(Base));
{ Correct the sign }
IF ((Base < 0) AND (Frac(Exponent/2.0) <> 0.0)) THEN
 Result := -Power
ELSE
 Result := Power;
 END; { FUNCTION Pow }
var base,exponent: integer;
var solve : double;
procedure TForm1.Button1Click(Sender: TObject);
begin
base := strtoint(Edit1.Text);
exponent := strtoint(Edit2.Text);
solve :=Power2(base,exponent);
 label4.caption := 'The result is ' + floattostr(solve)+'.';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.text :='';
edit2.text :='';
label4.caption := '';
edit1.setfocus;
end;
end.


Other 22 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.