ok, I am needing to calculate powers in C++ (ie, 10^6, 10^9, 10^101, etc)

the pow function uses double, and after 10^5, returns a number like (8e3245) ... but i need it for larger powers ...

now, is there a function I can do that will break it down somehow? my teacher said you can, and showed us an example, but the example was modpow (breaking down large numbers to be modded)

??

ive searched net, cant seem to find an asnwer

i have to do a program to do Markoff's Equation, and the limit is a large number .... i can do the markoff's equation already, just cant figure out a bound (ive bounded it in a lower number, but assignment requires a larger number that I cant do)

so any help on calculating the large power would be great ...

Recommended Answers

All 3 Replies

#include <iostream>
#include <climits>

int main()
{
   std::cout << std::numeric_limits<double>::max_exponent10 << std::endl;
   return 0;
}

/* my output
308
*/

With floating point, accuracy and precision have a tradeoff.

For floating point modulus, there is fmod.

Here is my code , i was able to calculate 2 power 1000. you can use the algorithm to calculate any number you want

# include <iostream>
using namespace std;

__int64 sum=0,q=0,o=0,t=0,arr[1000];



void multi(__int64  ,__int64 );
void check (__int64 );
__int64  power (__int64 ,__int64);




int main ()
{
	__int64 x,y;
	x=power(2,50);
	y=power(2,50);
	multi(x,y);
	

	cout<<endl;
	__int64 tot=0;

	for (__int64 i=q-1;i>=0;i--)
	{
	tot+=arr[i];
	cout<<arr[i];
	}
	cout<<endl;
	cout<<"Sum of digits:";

	cout<<tot<<endl;

	
}


void multi(__int64 x ,__int64 y)
{
	__int64 count=0;


	while (x!=0)
	{
		arr[count]=x%10;
		x=x/10;
		count++;
	}

	for (__int64 i=0;i<count;i++)
	{
		__int64 mul=arr[i]*y;
		if (i==count-1)
		{
			mul+=sum;
			while (mul!=0)
			{
				arr[q]=(mul)%10;
				mul=mul/10;
				q++;
			}
		}
		else
		check(mul);
	}
	while (o<18)
	{
		int t=q;
		q=0;
		sum=0;

	for (__int64 i=0;i<t;i++)
	{
		__int64 mul=arr[i]*y;
		if (i==t-1)
		{
			mul+=sum;
			while (mul!=0)
			{
				arr[q]=(mul)%10;
				mul=mul/10;
				q++;
			}
		}
		else
		check(mul);
	}
	o++;
	}
}

void check (__int64 z)
{
	arr[q]=(z+sum)%10;
	q++;
	z=(z+sum)/10;
	sum=z;
}

__int64 power (__int64 x,__int64 y)
{
	__int64 mul=1;
	for (__int64 i=1;i<=y;i++)
	{
		mul*=x;
	}
	return mul;
}
[/B]

You could also use a C++ library called 'apfloat' it's freely available here ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.