-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp.java
53 lines (48 loc) · 1.2 KB
/
Exp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/******************************************************************************
* Compilation: javac Exp.java
* Execution: java Exp x
*
* Prints out e^x using Taylor expansion.
*
* e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ..
*
* % java Exp 1.0
* 1.0
* 1.0
*
* % java Exp 1
* 2.7182818284590455
* 2.7182818284590455
*
* % java Exp 10
* 22026.465794806718
* 22026.465794806714
*
* % java Exp -10
* 4.539992976248485E-5
* 4.5399929762484854E-5
*
******************************************************************************/
public class Exp {
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
System.out.println(Math.exp(x));
// for negative argument, use identity e^-x = 1/e^x
boolean isNegative = false;
if (x < 0) {
isNegative = true;
x = -x;
}
// compute e^x assuming x >= 0
double term = 1.0;
double sum = 0.0;
for (int n = 1; sum != sum + term; n++) {
sum += term;
term *= x/n;
}
// print results
if (isNegative)
sum = 1.0 / sum;
System.out.println(sum);
}
}