//----------------------------------------------------------------------
//  IMPLEMENTATION FILE (powers.cpp)
//  This module provides exponentiation functions.
//----------------------------------------------------------------------
#include "powers.h"

int PowerOfInt( /* in */ int someInt,
                /* in */ int exp     )
    //..................................................................
    // PRE:  Assigned(someInt)  &&  exp >= 0
    // POST: FCTVAL == someInt raised to the power "exp"
    //       (NOTE: Large exponents may produce overflow)
    //..................................................................
{
    int i;
    int partialVal = 1;

    for (i = 1; i <= exp; i++)  // INV (prior to test):
                                //     partialVal == someInt raised
                                //                   to power (i-1)
                                //  && i <= exp+1
        partialVal *= someInt;
    return partialVal;
}

float PowerOfFloat( /* in */ float someFloat,
                    /* in */ int   exp       )
    //..................................................................
    // PRE:  Assigned(someFloat)  &&  Assigned(exp)
    //    && (exp < 0) --> someFloat != 0.0
    // POST: FCTVAL == someFloat raised to the power "exp"
    //       (NOTE: Large exponents may produce overflow)
    //..................................................................
{
    if (exp < 0) {                  // Negative exponent means
        someFloat = 1.0/someFloat;  // repeated division
        exp = -exp;
    }
    int   i;
    float partialVal = 1.0;

    for (i = 1; i <= exp; i++) // INV (prior to test):
                               //     partialVal == someFloat<entry>
                               //             raised to power (i-1)
                               //  && i <= abs(exp<entry>)+1
        partialVal *= someFloat;
    return partialVal;
}


