|
|
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;
}
//---------------------------------------------------------------------------
|
|
Arithmetic: The Exponential of a Number
|
double exp(double x);
The exp() function calculates the exponential value of a number.
The argument, a double-precision value, represents the number to be
evaluated.
If the value of x is less than -708.395996093
(approximately), the result is reset to 0 and qualifies as underflow. If
the value of the argument x is greater than 709.78222656
(approximately), the result is INF and qualified as overflow:
//---------------------------------------------------------------------------
#include <iostream.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsusedint main(int argc, char* argv[])
{
cout << "\nThe exponential of "
<< 709.78222656 << " is " << exp(709.78222656);
cout << "\n\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------
Therefore, the value of the argument should be between these two extremes. For a larger number, use the
expl() function:
long double expl(long double x);
As opposed to an 8-byte value, this version of the function takes a
10-byte variable, calculates its exponent, and returns a long double.
|
|
Arithmetic: The Mantissa and Exponent
|
double ldexp(double x, int y); long double ldexpl(long double x, int y);
Result = x * 2y
Here is an example:
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double x, Result;
int y;
x = StrToFloat(Edit1->Text);
y = StrToInt(Edit2->Text);
Result = ldexp(x, y);
Edit3->Text = FloatToStr(Result);
}
//---------------------------------------------------------------------------
The ldexp() function works on double-precision numbers while the ldexpl() uses
long doubles.
Extended __fastcall Ldexp(Extended X, int P);
Result = X * 2P //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float Source = 450.04;
float Exp = 10.25;
double Result = Ldexp(Source, Exp);
Edit1->Text = Result;
}
//---------------------------------------------------------------------------
|




0 comments:
Post a Comment