Home

Arithmetic: The Power of a Number

 

The C++ pow() Functions

double pow(double Source, double Raise);
long double powl(long double Source, long double Raise);

The pow() function is used to calculate the value of one number or expression raised to the power of another number. This follows the formula:

ReturnValue = xy

The pow() function takes two required arguments. The first argument, x, is used as the base number to be evaluated. The second argument, y, also called the exponent, will raise x to this value. The powl() function performs the same calculation on long double numbers and returns a long double.

In the following example, a form is equipped with a Button control and an Edit control. When the user clicks the button, the constant 205.38 is raised to the power of 4.12. The result displays in the edit box:

//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>

#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	const double Source = 205.38;
	const double Exp = 4.12;

	double Result = pow(Source, Exp);
	Edit1->Text = Result;
}
//---------------------------------------------------------------------------

The IntPower() function

Extended __fastcall IntPower(Extended Base, int Exponent);

The VCL’s IntPower() function is used to raise a number, Base, to the integral Exponent power. The first argument of this function, Base, can be an integer, a float, a double-precision number or a long double. The Exponent argument is the factor about which the Base number will be raised.

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	Extended Number, Base;
	int Exp;

	Base   = StrToFloat(Edit1->Text);
	Exp    = StrToInt(Edit2->Text);
	Number = IntPower(Base, Exp);

	Edit3->Text = FloatToStr(Number);
}
//---------------------------------------------------------------------------

The Power() Function

Extended __fastcall Power(Extended Base, Extended Exponent);

The Power() function takes a number (any number, including integers, floating, double or long double-precision numbers) as the Base argument and raises it to the power of the Exponent argument, which also can be any number (int, float, double, long double).

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	int Source = 205;
	float Exp = 5.25;
	
	double Result = Power(Source, Exp);
	Edit1->Text = Result;
}
//---------------------------------------------------------------------------

 

 
Home Copyright © 2004-2007 FunctionX, Inc.