473,545 Members | 527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pow() with large number??

4 New Member
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.

Anybody know how to solve this problem, please help me.

Thanks a lot.
Mar 13 '08 #1
6 4801
Ganon11
3,652 Recognized Expert Specialist
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
TDBNDVVDSC
4 New Member
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(do uble 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.
So, Please give me some examples about forming the libraries.

Thanks you very much.
Mar 13 '08 #3
Sick0Fant
121 New Member
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 Recognized Expert Contributor
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
TDBNDVVDSC
4 New Member
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
TDBNDVVDSC
4 New Member
Below is my program in Visual C++, please check for me.
void CHJDlg::OnAc()
{
// TODO: Add your control notification handler code here
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(FALS E);

}

// 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(FALS E);
return m_OC;
}

//build the totalfunction
double CHJDlg::TotalFo rmula(int m_From, int m_To, double x, double y)
{
double ResultTF,luythu a,giaithua;
ResultTF=0;
for (int i=m_From;i<=m_T o;i++){
//luythua = pow(x,i);
luythua = exp(i * log(x));
giaithua = Factorial(i);
ResultTF=Result TF+luythua/giaithua*y;
}
return ResultTF;
}

//build the factorial function.
double CHJDlg::Factori al(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(FALS E);
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*(doub le(1)-e_lamda_t1*tota l1) - j*(double(1)-e_lamda_t1*tota l2))*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(FALS E);
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)*total 1 - lamda_t*total2;
UpdateData(FALS E);
return m_BO2E1;
}

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

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

//calculate value of BO
double CHJDlg::BO()
{
m_BO = double(m_P)/m_T * (BO1() + BO2n() + BO2e());
UpdateData(FALS E);
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()
{
// TODO: Add your control notification handler code here
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_delt aXi,m_R,m_S,m_A C);
m_View = str;
UpdateData(fals e);

// 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_delt aXi,m_R,m_S,m_A C);
m_View = m_View + str;
UpdateData(fals e);
// 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_delt aXi,m_R,m_S,m_A C);
m_View = m_View + str;
UpdateData(fals e);
}
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_delt aXi,m_R,m_S,m_A C);
m_View = m_View + str;
UpdateData(fals e);
}
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_delt aXi,m_R,m_S,m_A C);
m_View = m_View + str;
UpdateData(fals e);
}
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_e psilon)// 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

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2697
by: Clueless Moron | last post by:
Why do they act differently with respect to complex numbers? Python 2.2.2 (#1, Feb 24 2003, 19:13:11) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> pow(2, 0+1j) (0.76923890136397211+0.63896127631363475j) >>> >>> import math >>> math.pow(2, 0+1j)
2
1615
by: david.corby | last post by:
I would like to know a few things about the pow() function. 1.) Is it an efficient way to raise a number to a given power for both small and large numbers? 2.) How accurate is pow() when doing integer calculations? I'm basically just going to use it to get powers of 10, but I can't have pow(10, 6) == 999999 or 1000001 when I bring it back...
13
5750
by: Shaobo Hou | last post by:
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of -2? The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack...
52
13123
by: Michel Rouzic | last post by:
I obtain an unwanted behavior from the pow() function : when performing pow(2, 0.5), i obtain 1.414214 when performing pow(2, 1/2), i obtain 1.000000 when performing a=0.5; pow(2, a), i obtain 1.414214 when performing a=1/2; pow(2, a), i obtain 1.000000 how come??? and how can i do a pow(x, y) so my y is the fraction of two other...
11
4412
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)? Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some other (perhaps less efficient) algorithm based on logarithms? Thanks,...
42
2455
by: John Smith | last post by:
In a C program I need to do exponentiation where the base is negative and the exponent is a fraction. In standard C this would be something like t = pow(-11.5, .333), but with this combination of arguments there is a domain error and the result is a percolating NaN. I worked around the problem by putting the standard function in a wrapper:...
2
3303
by: matrim | last post by:
What I'm trying to do: 1. Attempt to open the file. The filename, c:\\windData.txt, should be hardcoded into your program. Note the two slash characters in the file name. If the file cannot be opened, display a message and exit the program. 2. From the file read a temperature and a wind speed. Both values should be stored in variables...
2
2408
by: Olumide | last post by:
Hi , I hope this isn't too trivial to ask, but I'm evaluating a polynomial that involve lots of terms like x^2y^2z^3, and although I can use the power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3). Or is it all the same? Thanks,
5
4381
by: Tzury Bar Yochay | last post by:
What is the reason math.pow yields OverflowError while python itself can calculate these large numbers. e.g: 1e+308 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: math range error 10000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.