问题描述:

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 type

double 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;
}
```

您可能感兴趣的文章：

- html - IPB forum post background color
- ios - Crash with received memory warning message utilizing GLKit and ARC
- php - Multiple Magento Attributes on One Product
- r - create new data frame from a function of other data frames
- PHP function that displays time stamps as 1d for 1 day ago, 43s for 43 seconds etc
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial