| |
double hexponent(double i, double j)
{
/*
Programmer: Howard Bandy
Project Name: Lab3
Program Name: Lab3.cpp
Last Modified: 2/19/2000
This Function is designed to:
Raise i to the j power using the binomial theorem:
(P + PQ)^(m/n) = P^(m/n) + (m/n)AQ + ((m-n)/2n)BQ + ((m-2n)/3n)CQ + ((m-3n)/4n)DQ + ...
Funcion dictionary:
hIntegerDivision: Returns the integer part of division.
Data dictionary:
ans: holds the answer ((P + PQ)^(m/n))
A: holds each previous term (A, B, C, D, ...)
precision: sets the accuracy of the calculation. higher accuracy = more steps.
*/
double ans = 1, A = 1;
const int precision = 100;
if(j - hIntegerDivision(j, 1) == 0 || (j < 1 && j > -1))
{
//if the exponent is not composite
if(j >= 0)
//if the exponent is positive
if(i <= 2 || j - hIntegerDivision(j, 1) == 0)
{
//if the number is less than or equal to 2, or the exponent is an integer, then
//the answer will converge by the binomial theorem and P is defined to 1
--i;
for(int k = 1; k < precision; k++)
{
if(j - k + 1 == 0)
break;
A *= (j - (k - 1)) / k * i;
ans += A;
}
}
else
{
//if the number is greater than 2 and the exponent is not an integer, then the answer
//will diverge. to account for this, Q is set to 1 and P is calculated.
A = hexponent(i/2, j);
ans = A;
for(int k = 1; k < precision; k++)
{
if(j - k + 1 == 0)
break;
A *= (j - (k - 1)) / k;
ans += A;
}
}
else
//if the exponent is negative, then make it positive and recalculate
ans = 1 / hexponent(i, -j);
}
else
//if the exponent is composite, then do the integer and fraction parts seperate
ans = hexponent(i, hIntegerDivision(j, 1)) * hexponent(i, j - hIntegerDivision(j, 1));
return ans;
} |