• sahana

MemberAug 1, 2008

## 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.

Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
• MemberAug 1, 2008

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!!
Are you sure? This action cannot be undone.
• MemberAug 2, 2008

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.
Are you sure? This action cannot be undone.
• MemberAug 2, 2008

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:

int carry=0;
loop till done{
result=a+b+carry;
carry=result/10;
result=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.
Are you sure? This action cannot be undone.
• MemberAug 10, 2008

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<stdio.h>
#include<conio.h>
#include<alloc.h>
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);
getch();
}

i know this is not the best way to solve but trust me this itself shook me.
Are you sure? This action cannot be undone.
• MemberSep 24, 2008

sahana
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.

This is simple using math functions. even pow function may be implemented this way..

#include <stdio.h>
#include <math.h>

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

i.e., 1e+300 + 1e+300
which will be equal to 2e+300 where e is 10

Regards
Sriram
Are you sure? This action cannot be undone.
• MemberSep 25, 2008

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 <a href="https://gmplib.org/" target="_blank" rel="nofollow noopener noreferrer">The GNU MP Bignum Library</a>)

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
Are you sure? This action cannot be undone.