Hello to all.
I am trying to get an aproximation of expodential of x, using the power serie expansion which is x to the power n over n factorial, with n approaching to inifinty.
Now I am trying to get the value of expodential of 1 by passing to the command line x = {"1.0"},with n ={"25"}. However, I always get 2 as output, instead of a nice 2.7.......
why is that?
Have a look at the code:
public class Exponent {
public static void main (String[] args) {
double x = Double.parseDouble(args [0]); // command
int n = Integer.parseInt(args [1]); // command
System.out.println(factorial(n));
long result = 0;
for (int i = 0; i <= n; i++) {
result += power(x,i) / factorial(i);
}
System.out.println(result);
}
public static long factorial (int n) { // I used long just becuase the int breaks too soon (@ 15) even with long, 25 is the max number we can compute.
long result = 1;
for (int i = 1; i <= n; i ++) {
result *= i;
}
return result;
}
public static long power (double x, int n) { // also for maximum precision, I used a long for return.
long result = 1;
for (int i = 1; i <= n; i++) {
result *= x;
}
return result;
}
}
Thank you for your time.
---
Oppression.