i am trying to not import the math class to use but i am still trying to estimate the constant "e". it is said e= 1+(1/1!)+(1/2!)+(1/3!)+(1/4!)+(1/5!)+.....

these are what i have int at the top

``String userInput;int uIp; // this converts the string into int typedouble e = 2;``

then i ask some questions then i check to see not zero to exit and non negative to continue

``While(uIp >0){final int endTheLoop = 15;int factorialNumber = 1;double e2TheUserInput=0;for(int i = 2; i < endTheLoop; i++){for(int j = 1; j < i; j++){factorialNumber = ((i - 1) * factorialNumber);}e = (1/factorialNumber) + e;e2TheUserInput = Math.pow(e,uIp);}}``

You are doing integer division(but e is a double right?):

``````e = (1/factorialNumber) + e;
``````

Correct that to:

``````e = (1.0/(double)factorialNumber) + e;
``````

It was counting all the loops, but changes are zero according to the integer division. :)

e= 2+(0)+(0)+(0)+(0)+.....

I am not sure what your code is trying to do but if you want to compute exp(x) this is how I would do it.

``````public static void main(String... args) {
for (int i = -4; i <= 4; i++)
System.out.println(i + ": " + exp(i) + " cf " + Math.exp(i));
}

private static double exp(double d) {
if (d < 0)
return 1 / exp(-d);
double e = 1, term = 1;
for (int i = 1; i < 20 || term > e * 1e-16; i++) {
term *= d / i;
e += term;
}
return e;
}
``````

For large exponents, it more efficient to evaluate the integral powers without using a taylor series.

``````public static final double E = 2.7182818284590452354;

private static double exp(double d) {
if (d < 0)
return 1 / exp(-d);
long num = (long) d;
double numE = 1;
double mult = E;
while (num > 0) {
if ((num & 1) != 0)
numE *= mult;
num >>>= 1;
mult *= mult;
}
double fract = d - (long) d;
double fractE = 1, term = 1;
for (int i = 1; i < 20; i++) {
term *= fract / i;
fractE += term;
}
return numE * fractE;
}
``````

Top