CE Home
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering
Notices


Advertisements
Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)
Old 2nd August 2008, 12:06 AM
CE - Apprentice
 
Join Date: 25th September 2006
Location: chennai
I'm a Crazy computer science Engineer
Posts: 36
Default 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.
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)
Old 2nd August 2008, 01:26 PM
CE - Addict
 
mahul's Avatar
 
Join Date: 14th November 2007
Location: kolkata
I'm a Crazy Computer Science Engineer
Posts: 275
Send a message via Yahoo to mahul
Default Re: pow() in C

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!!
__________________

An apple changed the world, it fell on Newton's head

My Blog
mahul is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 3rd August 2008, 03:07 AM
CE - Apprentice
 
Join Date: 25th September 2006
Location: chennai
I'm a Crazy computer science Engineer
Posts: 36
Default Re: pow() in C

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.
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 3rd August 2008, 12:02 PM
CE - Addict
 
mahul's Avatar
 
Join Date: 14th November 2007
Location: kolkata
I'm a Crazy Computer Science Engineer
Posts: 275
Send a message via Yahoo to mahul
Default Re: pow() in C

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[i]=a[i]+b[i]+carry;
carry=result[i]/10;
result[i]=result[i]%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.
__________________

An apple changed the world, it fell on Newton's head

My Blog
mahul is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 10th August 2008, 11:46 PM
CE - Apprentice
 
Join Date: 25th September 2006
Location: chennai
I'm a Crazy computer science Engineer
Posts: 36
Default Re: pow() in C

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[i])+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[i]);
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.
sahana is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 25th September 2008, 12:40 PM
CE - Regular Member
 
Join Date: 25th September 2008
I'm a Crazy CSE Engineer
Posts: 65
Default Re: pow() in C

Quote:
Originally Posted by sahana View Post
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.
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

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
sriramchandrk is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 26th September 2008, 11:01 AM
CE - Addict
 
anuragh27crony's Avatar
 
Join Date: 12th January 2006
Location: INDIA
I'm a Crazy Computer Engineer
Posts: 341
Send a message via Yahoo to anuragh27crony
Default Re: pow() in C

Initially there are 2 conditions to satisfy

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

... there's library in C --> GNU MP Bignum Libarary (Site: http://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
anuragh27crony is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +5.5. The time now is 12:22 PM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Member comments are owned by the poster. Copyright © 2005-2008 CrazyEngineers.com. All rights reserved.

Advertisements