471,897 Members | 1,589 Online

# pow() with large number??

Hi all,

I am programing for my project which include calculating the power function and get stuck there.
The problem is, when number x raised to the power y with x, and y too large (i tested with x=180, y=150) the program can not calculate.

like: double result = pow(180, 150);
=>> It can not calculate.

Thanks a lot.
Mar 13 '08 #1
6 4491
Ganon11
3,652 Expert 2GB
You are probably getting a result too large to store in a double. If you really need to calculate 180^150, there should be libraries designed to handle such massive numbers.
Mar 13 '08 #2
Thanks Ganon,

Exactly, that the problem i got. I has tried many ways to solve this but still get stuck. I changed the way to calculate the pow function like:

double result = pow(x,y); or double result = exp(y*ln(x));

or code them like:
double raiseToPower(double x, int n)
{
int count;
double result;

if (n >= 0)
{ // We're raising the base to a positive exponent
for (count = 1, result = 1.; count <= n; count++)
{
result = result * x;
}
}
else
{ // We're raising the base to a negative exponent
n = n * -1;
for (count = 1, result = 1.; count <= n; count++)
{
result = result * (1. / x);
}
}

return result;
}

but still got stuck.

As your comment, i should form some libraries to handle the massive number but i have just practice C++ in one month and do not know how to form them.

Thanks you very much.
Mar 13 '08 #3
Sick0Fant
121 100+
gcc supports the "double double" type.

If it's still too large, you may consider the following:

x^y = z <=> y * log(x) = log(z)

Then at least you know that when you raise 10 to the log(z) power, you get the true answer.
Mar 13 '08 #4
oler1s
671 Expert 512MB
The number for 180^150 is bigger than what any data type supported by hardware can hold. The best you get is a long long integer or something. Regardless, I think it's still to obig. So you must use a software library that can hold arbitrary precision numbers. GMP is one I know off my head. There are others out there.

As you say, you're still a beginner. Which brings up the question, why are you doing this? If this is for an assignment, perhaps explain to us what you're trying to achieve in the end. If it's your own code, independent of any work, then you're overreaching.
Mar 13 '08 #5
Thanks all,

This is one part of my program for my mathematical model of my thesis, in this mathematical model, it include some part like: sum of power function. To solve my mathematical model, it is need to programing by C++ or Java, because it is very difficult to solve manually.

The problem i got when programing is the power function some time went too big because of two variances inside it increased too much.

The target of program is finding the value of two variances which give the minimum value of model by changing them (increase or decrease). The method i use for searching is Hook and Jeeve method.

So, does anybody know how solve this problem, please tell me.

Thanks a lot.
Mar 14 '08 #6
Below is my program in Visual C++, please check for me.
void CHJDlg::OnAc()
{
UpdateData(TRUE); // update data from control to present variance.
// vice versa update the present variance to control
m_R=m_LD*m_T2;
m_S=m_LD*m_T;
m_AC = AC();
UpdateData(FALSE);

}

// calculate the value of OC
double CHJDlg::OC()
{
int i=int(m_S-m_R-1);
double r,Lamda_T;
Lamda_T = m_LD * m_T;

r=TotalFormula(0,i,Lamda_T,exp(-Lamda_T));
m_OC = double(1)/m_T*((m_K1-m_K2)*r-m_K1/exp(Lamda_T)+m_K2);
UpdateData(FALSE);
return m_OC;
}

//build the totalfunction
double CHJDlg::TotalFormula(int m_From, int m_To, double x, double y)
{
double ResultTF,luythua,giaithua;
ResultTF=0;
for (int i=m_From;i<=m_To;i++){
//luythua = pow(x,i);
luythua = exp(i * log(x));
giaithua = Factorial(i);
ResultTF=ResultTF+luythua/giaithua*y;
}
return ResultTF;
}

//build the factorial function.
double CHJDlg::Factorial(int x)
{
double Result;
Result=1;
for (int i=2;i<=x;i++)
Result=Result*i;
return Result;
}

//calculate the value of BO1
double CHJDlg::BO1()
{
double Lamda_T, e_Lamda_T, total1, total2;

Lamda_T = m_LD * m_T;
e_Lamda_T = exp(-Lamda_T);

total1 = TotalFormula(0,m_S-1,Lamda_T,1);
total2 = TotalFormula(0,m_S,Lamda_T,1);

m_BO1 = Lamda_T - (Lamda_T * e_Lamda_T * total1) - m_S + (m_S * e_Lamda_T * total2);
UpdateData(FALSE);
return m_BO1;
}

//calculate the value of BO2
double CHJDlg::BO2(int From, int To, double m_lamda_t)
{
double lamda_t1, lamda_t, e_lamda_t1, e_lamda_t, Result, total1, total2;
Result = 0;
total1 = 0;
total2 = 0;
lamda_t1 = m_lamda_t;
e_lamda_t1 = exp(-lamda_t1);

lamda_t = m_LD * m_T;
e_lamda_t = exp(-lamda_t);

for(int j=From;j<=To;j++)
{
total1 = TotalFormula(0, j-2, lamda_t1, double(1));
total2 = TotalFormula(0, j-1, lamda_t1, double(1));
Result = Result + (lamda_t1*(double(1)-e_lamda_t1*total1) - j*(double(1)-e_lamda_t1*total2))*pow(lamda_t,m_S-j)/Factorial(m_S-j)*e_lamda_t;
}
return Result;
}

//calculate the value of BO2n
double CHJDlg::BO2n()
{
m_BO2N = BO2(m_R+1, m_S-1, m_LD*m_T1);
UpdateData(FALSE);
return m_BO2N;
}

//calculate the value of BO2e1
double CHJDlg::BO2e1()
{
double lamda_t, lamda_t2, total1, total2;
double Result;
Result = 0;

lamda_t = m_LD* m_T;
lamda_t2 = m_LD*m_T2;

total1 = TotalFormula(0, m_S-1, lamda_t, exp(-lamda_t));
total2 = TotalFormula(0, m_S-2, lamda_t, exp(-lamda_t));
m_BO2E1 = lamda_t2 + lamda_t - m_S + (m_S - lamda_t2)*total1 - lamda_t*total2;
UpdateData(FALSE);
return m_BO2E1;
}

//calculate the value of BO2e2
double CHJDlg::BO2e2()
{
m_BO2E2 = BO2(1,m_R,m_LD* m_T2);
UpdateData(FALSE);
return m_BO2E2;
}

//take sum of BO2e1 and BO2e2 to get BO2e
double CHJDlg::BO2e()
{
m_BO2E = BO2e1() + BO2e2();
UpdateData(FALSE);
return m_BO2E;
}

//calculate value of BO
double CHJDlg::BO()
{
m_BO = double(m_P)/m_T * (BO1() + BO2n() + BO2e());
UpdateData(FALSE);
return m_BO;
}

//calculate value of AC
double CHJDlg::AC()
{
return OC() + BO();
}

//////////////////////STAR USING HOOK AND JEEVE METHOD TO FIND THE VALUE OF R AND S WHICH GIVE THE MINIMUM VALUE OF AC.///
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CHJDlg::OnHj()
{
UpdateData(TRUE); // update date from control to present variance

// define the array value for Xo, X1....

int Xo[2],X10[2],X11[2],X12[2];

CString str;
m_R=m_LD*m_T2;
m_S=m_LD*m_T;

// set the start value for Xo: Xo{x1,x2}={R,S}
Xo[0] = m_R;
Xo[1] = m_S;

// calculate AC
m_AC = AC();

// the program get stuck, then debug from this line to find the problem
//The problem still not solved.
////////////////////////////////////////////////////////////////////////////////////////////////////////
str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m _R,m_S,m_AC);
m_View = str;
UpdateData(false);

// set X10=Xo
X10[0] = Xo[0];
X10[1] = Xo[1];

bool flag=true;

while (flag)
{
// step 1: increase/decrease x1 by deltaX.
//first: to increase x1
m_R = X10[0] + m_deltaXi;
m_S = X10[1];
// calculate AC+ then add to ac_temp
ac_temp = AC();

if (ac_temp<m_AC) // if AC+ less than AC then change the value of X11
{
X11[0] = X10[0] + m_deltaXi;
X11[1] = X10[1];

// keep the new value AC+, update value m_AC by AC+ for using of next step (step 2).
m_AC =ac_temp;
str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m _R,m_S,m_AC);
m_View = m_View + str;
UpdateData(false);
// still do not save the value of m_R, m_S, m_AC and deltaX into file.
}

else// if increase x1 do not give lower value of AC then decrease x1.
{
//second: decrease x1
m_R = X10[0] - m_deltaXi;
// reculculate AC-
ac_temp = AC();

if (ac_temp<m_AC)
{// if AC- < AC => take the new value for X11
X11[0] = X10[0] - m_deltaXi;
X11[1] = X10[1];

// keep the new value AC-, update value m_AC by AC- for using of next step(step 2).
m_AC =ac_temp;

// still do not save the value of m_R, m_S, m_AC and deltaX into file.

str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m _R,m_S,m_AC);
m_View = m_View + str;
UpdateData(false);
}
else// if increase/decrease x1 do not give lower value of AC then take X11=X10
{
X11[0] = X10[0];
X11[1] = X10[1];
}
}

// step 2: increase/decrease x2 by deltaX.
//first: increase x2
m_R = X11[0];
m_S = X11[1] + m_deltaXi;

// calculate AC+
ac_temp = AC();

if (ac_temp<m_AC)// if AC+ < AC then X12 = X11 + delta
{
X12[0] = X11[0];
X12[1] = X11[1] + m_deltaXi;

// update value of m_AC by AC+ for the comparing of next step.

m_AC = ac_temp;

// still do not save the value of m_R, m_S, m_AC and deltaX into file.

str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m _R,m_S,m_AC);
m_View = m_View + str;
UpdateData(false);
}
else // if increase x1 do not give lower value of AC then decrease x1.
{
//decrease x2
m_S = X11[1] - m_deltaXi;
ac_temp = AC();

if (ac_temp<m_AC)
{
X12[0] = X11[0];
X12[1] = X11[1] - m_deltaXi;
// update value of AC by AC- for the next comparing.
m_AC = ac_temp;

// still do not save value of m_R, m_S, m_AC and deltaX into file

str.Format("%d\t\t%d\t\t%d\t\t%15lf\n",m_deltaXi,m _R,m_S,m_AC);
m_View = m_View + str;
UpdateData(false);
}
else// if increase/decrease x2 do not give lower value of AC then take X12=X11
{
X12[0] = X11[0];
X12[1] = X11[1];
}
}

//******************************
// check the value of X12 and X10, they are equal or not.

if (X12[0]==X10[0] && X12[1]==X10[1])// if X12=X10 then check deltaX < epsilon ?
{
if (m_deltaXi<=m_epsilon)// if deltaX < epsilon, then stop.
{
flag=false;//add the flag = false to stop the bool.
break; // exit the bool
}
else // if deltaX > epsilon, then decrease deltaX.
{
m_deltaXi = m_deltaXi/2;
}
}

else // if X12# X10
{
// then find the new value of X10 by X10 = X12 - (X12 - X10).
X10[0] = 2*X12[0] - Xo[0];
X10[1] = 2*X12[1] - Xo[1];

//update the value of R, S, AC.
m_R = X10[0];
m_S = X10[1];
m_AC = AC();
//update the value of Xo.
Xo[0] = X10[0];
Xo[1] = X10[1];
}
}// end while
}
Mar 14 '08 #7