Quick Question Regarding Variable Types

This is a discussion on Quick Question Regarding Variable Types within the C++ Programming forums, part of the General Programming Boards category; Hello, I wrote the function: int topower(int base, int pwr) { int topow = 1; long int amt = base; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Unhappy Quick Question Regarding Variable Types

    Hello,

    I wrote the function:

    int topower(int base, int pwr) {
    int topow = 1;
    long int amt = base;
    while ( topow < pwr ) {
    amt = amt * base;
    topow++;
    }
    if ( pwr == 0 ) { amt = 1; }
    printf("\n%d:(%d)--%d--", base, pwr, amt);
    return amt;
    }

    since pow() didn't seem to like me for some reason =)
    problem is, when i try doing 10^10 (10000000000) I get "1410065408" in printf(). I assume that I'm using an incorret variable type?


    Thanks,
    Drek

  2. #2
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    USE

    unsigned long

    NOT

    long int

    AND CHANGE

    amt = amt * base TO amt *= base;

    *All integral types are signed meaning they go both ways

    EXAMPLE............

    For byte, from -128 to 127

    UNSIGNED

    For unsigned byte, 256 //one-way, either positive or neg is now double capacity

    *All the standard math types operate the same way*
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Perhaps this helps a little bit. But note that if you really want to work with huge numbers, you cannot do this. There is a thread in the C-board discussing working with huge numbers.

    Code:
    unsigned long topower (unsigned long base, unsigned long pwr) 
    { 
        unsigned long topow = 1; 
        unsigned long amt = base; 
    
        if (pwr == 0)
            amt = 1;
        else
        {
            while (topow++ < pwr)  
                amt *= base; 
        }
    
        printf("\n%d:(%d)--%d--", base, pwr, amt); 
        return amt; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  2. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  3. Custom variable types in C++
    By Aeroren in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 10:07 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21