Thread: Exponent calculator not functioning properly....

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Exponent calculator not functioning properly....

    Here is the source I wrote.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    
    
       
    int main()
    {
     int x;
     int y; 
     int total;
     int i; 
     
    
    
     
     
     i=0;
     total=1;
     
     
     printf("Enter a base limit for x : \n");
     scanf("%d",&x);
     
     printf("Enter an exponent limit for y: \n");
     scanf("%d",&y);
     
     while( i < y)
     {
      
      total = total * x ;
        
      i++;
            }
            
            printf("The total of %d raised to %d = %d", x,y,total);
    getch();
    getch();
    return 0;
    }
    this calculator works for a majority of the math except :

    100 ^ 100 = 0 in some cases

    in another case the calculator is just plain wrong. EX:

    19^9 = 322687697779

    my calculator says:

    19^9 = 565 150 579

    Any advice on why this is happening so i can figure out how to fix it would be much appreciated.

    Thank you for your time.


  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are experiencing integer overflows. Import limits.h and print the value for the INT_MAX constant. 100 multiplied by itself 100 times is going to be a huge number for example.

    Edit: to add some more to this. Most likely an int is 32 bits on your system which means the highest value you can ever store in it is going to be 2^32 -1. Since you are using a signed integer half of it's range is used for negative numbers, so in your case: 2^31 -1. When you go above the capacity you get a domino brick effect on the bits and you start over at 0.
    Last edited by Subsonics; 09-29-2011 at 08:43 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @subsonics ... Given the inclusion of conio.h I'm betting it's Turbo C which might even be 16 bits... meaning his integer range is -32768 -> 32767. Not entirely condusive to storing 1.0e+200

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Please be patient with me I'm new to this.
    I understand implementing a missing definition limits.h

    But I'm not sure how to implement INT_MAX in my code.

    I looked around the site some and all i found was this:
    how to handle integer overflow in C

    This is probably what I need I just cant make any sense of it in relation to my code.

    As far as the conio.h (that was just me fumbling threw the dark adding unnecessary information)


    Thanks again all your help has been very useful.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are not supposed to implement INT_MAX it's defined in limits.h and it will tell you how large an integer is on your system (or the largest value you can store in an integer actually). But there is an easier way: (sizeof(int) * 8) but I told you mostly as a way to understand the limits of what you can do, using your current approach.

    For example (100 ^ 100): for a reference 10 ^ 100 is 1 googol, or (as I have heard) approximately the amount of molecules in the known universe, in other words, don't sweat it if you can not represent it in an integer.

    The easiest way to solve this is probably to just assume 1 googol, then do: 10 ^ 2 = 100 googol.


    Edit: Actually, I did some searching and it appears that: The amount of elementary particles in the known universe have been approximated to 10^80 i.e 100 trillion times fewer than 1 googol.
    Last edited by Subsonics; 09-29-2011 at 10:28 AM. Reason: 10 ^ 2 :*(

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by working_with_c View Post
    Please be patient with me I'm new to this.
    I understand implementing a missing definition limits.h

    But I'm not sure how to implement INT_MAX in my code.
    Here is limits.h. You are going to need to change your variable type if you want to handle big numbers past INT_MAX. Something like a long or long long.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    More like double or long double. ( If that dinosaur supports them, that is... )
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. max number program not functioning properly
    By MaaaTtY in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2009, 11:53 AM
  2. if / else if / else statement not functioning properly
    By Beowolf in forum C Programming
    Replies: 8
    Last Post: 09-12-2007, 02:09 AM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM