CISC 2200 Project 1
Functions
proj1.cc File Reference
#include <iostream>
Include dependency graph for proj1.cc:

Functions

template<class T >
power1 (T x, unsigned int n, unsigned int &mults)
 Iterative function to compute a power xn. More...
 
template<class T >
power2 (T x, unsigned int n, unsigned int &mults)
 Naive recursive function to compute a power xn. More...
 
template<class T >
power3 (T x, unsigned int n, unsigned int &mults)
 Divide-and-conquer recursive function to compute a power xn. More...
 
template<class T >
void printReport (T base, unsigned int pow, T result1, T result2, T result3, unsigned int mults1, unsigned int mults2, unsigned int mults3)
 Report the results of three different exponentiation calculations. More...
 
int main ()
 The usual main function. More...
 

Function Documentation

int main ( )

The usual main function.

It computes the powers 2i for i=0,1,2,..,32 via all three methods, reporting the number of multiplications needed for each method. (Note what happens for 232.) It then does likewise for 0.5i, but now with i=0,1,2,...64

template<class T >
T power1 ( x,
unsigned int  n,
unsigned int &  mults 
)

Iterative function to compute a power xn.

Parameters
xthe base
nthe exponent
multsnumber of multiplications used, initially zero
Returns
xn
Precondition
n >= 0
Postcondition
mults is the total number of multiplications used
template<class T >
T power2 ( x,
unsigned int  n,
unsigned int &  mults 
)

Naive recursive function to compute a power xn.

Computes xn as x*xn-1.

Parameters
xthe base
nthe exponent
multstotal of multiplications used, which is assumed to have been correctly set by any previous recursive call
Returns
xn
Precondition
n >= 0
Postcondition
mults is the new number of multiplications used
template<class T >
T power3 ( x,
unsigned int  n,
unsigned int &  mults 
)

Divide-and-conquer recursive function to compute a power xn.

If the exponent is even, the result is simply the square of xn/2; if the exponent is even, then the result is x, multiplied by the square of x(n-1)/2.

Parameters
xthe base
nthe exponent
multstotal of multiplications used, which is assumed to have been correctly set by any previous recursive call
Returns
xn
Precondition
n >= 0
Postcondition
mults is the new number of multiplications used
template<class T >
void printReport ( base,
unsigned int  pow,
result1,
result2,
result3,
unsigned int  mults1,
unsigned int  mults2,
unsigned int  mults3 
)

Report the results of three different exponentiation calculations.

If the three results differ, then all three are printed out, with a warning message. If all three results are the same, the result is printed, along with the number of multiplications used by each of the three algorithms.

Parameters
basethe number whose power we seek
powthe power
result1the result of using power1 to compute basepower
result2the result of using power2 to compute basepower
result3the result of using power3 to compute basepower
mults1the number of multiplications used by power1
mults2the number of multiplications used by power2
mults3the number of multiplications used by power3
Precondition
pow>=0
result1, result2, result3 are as described in the parameter section
Postcondition
none