Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,800 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,340 people online right now. Registration is fast and FREE... Join Now!




Finding Power of Large Number

 
Reply to this topicStart new topic

Finding Power of Large Number

eudos
4 Oct, 2008 - 04:19 AM
Post #1

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Hi all

I am trying to find the power of large numners (a = b ^ c) ....Here is the function i am using.

CODE

int power(int a, int b)
{
   int c=a;
   int n;
   if (b != 0 ){
      for ( n=b; n>1; n--) c*=a;
   }else{
      c = 1;
   }

return c;
}


the Problem is when the numbers increases the return value is Zero. to be exact when 2^ 16 it give zero.
So my question is how can i deal with large numbers?

Thank you
User is offlineProfile CardPM
+Quote Post

csmanoj
RE: Finding Power Of Large Number
4 Oct, 2008 - 04:58 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Aug, 2007
Posts: 142



Thanked: 3 times
Dream Kudos: 50
My Contributions
You're probably working with a very old compiler. Check the int size of your compiler.

If you want to deal with a larger number, use a bigger data type like long or double. But even they have their limits. If you want no such limits, you have to implement your own data type or use one that someone else developed. They'll have limits too but it'll be very large.

The following code might work for you

CODE

long power(long a, int b)
{
   long c=a;
   int n;
   if (b != 0 ){
      for ( n=b; n>1; n--) c*=a;
   }else{
      c = 1;
   }

return c;
}


and don't forget to write '%ld' instead of '%d' in printf function to output this function's return value.

Look for a file (probably named limits.h or values.h) in your compiler's include folder that says how big the data types are.

This post has been edited by csmanoj: 4 Oct, 2008 - 05:07 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 01:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month