Coffee Room

Discuss anything here - everything that you wish to discuss with fellow engineers.

12793 Members

Join this group to post and comment.

# pow() in C

how can we compute the pow of real huge numbers like pow(1000,1000) or even more in c?

i guess even long double can't be used in this case as it results in overflow.

i guess the bit representations is a way but still pretty clueless.

It must be implemented in C and not any other language.

Please share your ideas for this problem.

i guess even long double can't be used in this case as it results in overflow.

i guess the bit representations is a way but still pretty clueless.

It must be implemented in C and not any other language.

Please share your ideas for this problem.

Yeah, you were right, this cannot be implemented using long double.

The solution to this problem is to use an character/integer array. The number can be stored in the array one digit at each location. Now all you need to do is to raise a array to power of another. To do this you can make use of numerical methods. Store the results in a 3rd array( use dynamic allocation ) and there you are!!

The solution to this problem is to use an character/integer array. The number can be stored in the array one digit at each location. Now all you need to do is to raise a array to power of another. To do this you can make use of numerical methods. Store the results in a 3rd array( use dynamic allocation ) and there you are!!

hi mahul.

i am sorry i didnt really catch you.the number is to be stored as an array with each digit in each location . so i am assuming i have to compute pow(1234,1234).so a[0]=1,a[1]=2,a[2]=3,a[3]=4.

now i dont understand array to the power of array. how am i to implement that? any hint please ,like is there any particular algorithm for that.

besides this will expect me to eventually store the number right?

won't it again over flow.

please correct me if i have understood it wrong.

thanks for the reply.

i am sorry i didnt really catch you.the number is to be stored as an array with each digit in each location . so i am assuming i have to compute pow(1234,1234).so a[0]=1,a[1]=2,a[2]=3,a[3]=4.

now i dont understand array to the power of array. how am i to implement that? any hint please ,like is there any particular algorithm for that.

besides this will expect me to eventually store the number right?

won't it again over flow.

please correct me if i have understood it wrong.

thanks for the reply.

I would like you to work out the solution for yourself. As for overflowing, using dynamic memory allocation, i doubt if they would overflow( the results are to be stored in another array, remember ). Even if there is a chance of that happening, do not store the result, rather output it to, say, a file. As for the logic for computation, I would give you a few hints:

1. Addition-

int carry=0;

loop till done{

result

1. Addition-

int carry=0;

loop till done{

result

*=a**+b**+carry;*

carry=resultcarry=result

*/10;*

resultresult

*=result**%10;*

}

see this can add and store the result in 'result' array.

Similarly you can multiply as well.

For that you need to multiply the first array by each element of the second array and add the results of each. Try that, it isn't very tough.

Once you know how to add and multiply integer arrays, all you would need is to pick up an numerical computation algorithm and implement it to compute power. If you are not aware of any, post the same and I would definitely help you out. I hope I've been clear this time.}

see this can add and store the result in 'result' array.

Similarly you can multiply as well.

For that you need to multiply the first array by each element of the second array and add the results of each. Try that, it isn't very tough.

Once you know how to add and multiply integer arrays, all you would need is to pick up an numerical computation algorithm and implement it to compute power. If you are not aware of any, post the same and I would definitely help you out. I hope I've been clear this time.

hello mahul. i am very sorry for the late response.

i tried to solve it, and i guess i got the answer. i checked it for 1024^1024, got it right and even for 1000^1000.

#include

#include

#include

void main()

{

int *a,b=1024,i=0,carry=0,k=0,alen=4,p=999;

clrscr();

a[0]=4;

a[1]=2;a[2]=0;a[3]=1;

while(p!=0)

{

while(alen!=0)

{

a[k]=b*(a

i tried to solve it, and i guess i got the answer. i checked it for 1024^1024, got it right and even for 1000^1000.

#include

#include

#include

void main()

{

int *a,b=1024,i=0,carry=0,k=0,alen=4,p=999;

clrscr();

a[0]=4;

a[1]=2;a[2]=0;a[3]=1;

while(p!=0)

{

while(alen!=0)

{

a[k]=b*(a

*)+carry;*

carry=a[k]/10;

alen--;

a[k]=a[k]%10;

if(alen==0)

{

while(carry!=0)

{ k++;

a[k]=carry%10;

carry=carry/10;

}

}

i++;

k++;

}

carry=0;

alen=k;

i=0;

k=0;

p--;

}

for(i=alen-1;i>=0;i--)

printf("%d",a

carry=a[k]/10;

alen--;

a[k]=a[k]%10;

if(alen==0)

{

while(carry!=0)

{ k++;

a[k]=carry%10;

carry=carry/10;

}

}

i++;

k++;

}

carry=0;

alen=k;

i=0;

k=0;

p--;

}

for(i=alen-1;i>=0;i--)

printf("%d",a

*);*

getch();

}

i know this is not the best way to solve but trust me this itself shook me.

thanks a ton. please help me ways in which i could have improved my code.getch();

}

i know this is not the best way to solve but trust me this itself shook me.

thanks a ton. please help me ways in which i could have improved my code.

This is simple using math functions. even pow function may be implemented this way..sahanahow can we compute the pow of real huge numbers like pow(1000,1000) or even more in c?

i guess even long double can't be used in this case as it results in overflow.

i guess the bit representations is a way but still pretty clueless.

It must be implemented in C and not any other language.

Please share your ideas for this problem.

#include

#include

double pow(double a, double b)

{

return(exp(b*log(a)));

}

int main()

{

double a, b;

printf("Enter a, b: ");

scanf("%lf%lf", &a, &b);

printf("%lf ^ %lf = %lf\n", a, b, pow(a, b));

}

you need to device a funcion which calls pow in breakups for example

1000^200

pow(1000,100) + pow(1000,100)

and sub results should be added and stored in string format

add using formulas for addition of such numbers.

i.e., 1e+300 + 1e+300

which will be equal to 2e+300 where e is 10

Regards

Sriram

Initially there are 2 conditions to satisfy :smile:

1) If you are using it some where in the project

... there's library in C --> GNU MP Bignum Libarary (Site: https://gmplib.org/)

there are no limitations to number of digits it can support..

2) If it's for fun..... then continue discussing don't use this library

1) If you are using it some where in the project

... there's library in C --> GNU MP Bignum Libarary (Site: https://gmplib.org/)

there are no limitations to number of digits it can support..

2) If it's for fun..... then continue discussing don't use this library