Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.
Example 8
double pow(double a, double b)
{
// true if base is greater than 1
boolean gt1 = (Math.sqrt((a-1)*(a-1)) <= 1)? false:true;
int oc = -1; // used to alternate math symbol (+,-)
int iter = 20; // number of iterations
double p, x, x2, sumX, sumY;
// is exponent a whole number?
if( (b-Math.floor(b)) == 0 )
{
// return base^exponent
p = a;
for( int i = 1; i < b; i++ )p *= a;
return p;
}
x = (gt1)?
(a /(a-1)): // base is greater than 1
(a-1); // base is 1 or less
sumX = (gt1)?
(1/x): // base is greater than 1
x; // base is 1 or less
for( int i = 2; i < iter; i++ )
{
// find x^iteration
p = x;
for( int j = 1; j < i; j++)p *= x;
double xTemp = (gt1)?
(1/(i*p)): // base is greater than 1
(p/i); // base is 1 or less
sumX = (gt1)?
(sumX+xTemp): // base is greater than 1
(sumX+(xTemp*oc)); // base is 1 or less
oc *= -1; // change math symbol (+,-)
}
x2 = b * sumX;
sumY = 1+x2; // our estimate
for( int i = 2; i <= iter; i++ )
{
// find x2^iteration
p = x2;
for( int j = 1; j < i; j++)p *= x2;
// multiply iterations (ex: 3 iterations = 3*2*1)
int yTemp = 2;
for( int j = i; j > 2; j-- )yTemp *= j;
// add to estimate (ex: 3rd iteration => (x2^3)/(3*2*1) )
sumY += p/yTemp;
}
return sumY; // return our estimate
}
public static int pow(int arg, int times){
int ret = 1;
for ( int i = 1 ; i <= times ; i++ ) {
ret = ret * arg;
}
return ret;
}
public static double log(double x, int base, int decimalplace) {
int integer_value = 0;
while (x < 1) {
integer_value--;
x = x * base;
}
while (x >= base) {
integer_value++;
x = x / base;
}
double decimal_fraction = 0.0;
double partial = 1.0;
x = pow(x, 10);
while (decimalplace > 0) {
partial = partial / 10;
int digit = 0;
while (x >= base) {
digit++;
x = x / base;
}
decimal_fraction = decimal_fraction + digit * partial;
x = pow(x, 10);
decimalplace--;
}
return integer_value + decimal_fraction;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
netstat -ano | find "8000" and taskkill /f /pid 2984 | 3 | 55 | |
Should localization be done inside spring controller | 5 | 25 | |
couple of eclipse | 5 | 35 | |
Tomcat: Unable to run tomcat service. | 2 | 18 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
13 Experts available now in Live!