|
CISC 2200 Project 1
|
#include <iostream>
Functions | |
| template<class T > | |
| T | power1 (T x, unsigned int n, unsigned int &mults) |
| Iterative function to compute a power xn. More... | |
| template<class T > | |
| T | power2 (T x, unsigned int n, unsigned int &mults) |
| Naive recursive function to compute a power xn. More... | |
| template<class T > | |
| 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... | |
| 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
| T power1 | ( | T | x, |
| unsigned int | n, | ||
| unsigned int & | mults | ||
| ) |
Iterative function to compute a power xn.
| x | the base |
| n | the exponent |
| mults | number of multiplications used, initially zero |
| T power2 | ( | T | x, |
| unsigned int | n, | ||
| unsigned int & | mults | ||
| ) |
Naive recursive function to compute a power xn.
Computes xn as x*xn-1.
| x | the base |
| n | the exponent |
| mults | total of multiplications used, which is assumed to have been correctly set by any previous recursive call |
| T power3 | ( | T | 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.
| x | the base |
| n | the exponent |
| mults | total of multiplications used, which is assumed to have been correctly set by any previous recursive call |
| 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.
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.
| base | the number whose power we seek |
| pow | the power |
| result1 | the result of using power1 to compute basepower |
| result2 | the result of using power2 to compute basepower |
| result3 | the result of using power3 to compute basepower |
| mults1 | the number of multiplications used by power1 |
| mults2 | the number of multiplications used by power2 |
| mults3 | the number of multiplications used by power3 |
result1, result2, result3 are as described in the parameter section
1.8.10