@@ -7,6 +7,17 @@ public static void main(String[] args) {
77 loop (10 );
88 System .out .println (squareRoot (9.0 ));
99 System .out .println (power (2.0 ,8 ));
10+ System .out .println (factorial (8 ));
11+ System .out .println (myexp (8.0 ,100 ));
12+ check (1 );
13+ for (double i =0.1 ; i <=100.0 ; i *=10 ){
14+ check (i );
15+ }
16+ System .out .println ("The values of exponential e^x for x=-0.1,-1.0,-10.0,-100.0 are:" );
17+ for (double i =-0.1 ; i >=-100.0 ; i *=10 ){
18+ System .out .println (myexp (i ,100 ));
19+ }
20+ System .out .println (gauss (2.0 ,100 ));
1021 }
1122
1223 public static void loop (int n ) {
@@ -22,7 +33,7 @@ public static void loop(int n) {
2233 }
2334
2435 /**
25- * This method calculate an approximation
36+ * This method calculates an approximation
2637 * of the square root of a number
2738 * @param a the number whose root will be found
2839 * @return the approximated result of the square root
@@ -56,7 +67,7 @@ public static double squareRoot(double a){
5667 }
5768
5869 /**
59- * This method calculate
70+ * This method calculates
6071 * the value of the power x^n.
6172 * @param x the base of the power
6273 * @param n the exponent of the power
@@ -80,4 +91,71 @@ public static double power(double x, int n) {
8091 return power ;
8192 }
8293
94+ /**
95+ * This method computes the factorial function
96+ * of a given integer n.
97+ * @param n the argument of the factorial function
98+ * @return the value of the factorial function
99+ */
100+ public static int factorial (int n ){
101+ int result = 1 ;
102+ for (int i =n ;i > 1 ; i --){
103+ result *= i ;
104+ }
105+ return result ;
106+ }
107+
108+ /**
109+ * This method calculates e^x
110+ * using an infinite series expansion,
111+ * whose i^th term is: x^i/i!.
112+ * @param x the exponent of the exponential
113+ * @param n the number of terms of the series
114+ * we want to add
115+ * @return the value of the exponential
116+ */
117+ public static double myexp (double x , int n ){
118+ double result = 1 ;
119+ double temp = 1 ;
120+ for (int i =1 ; i <= n ; i ++){
121+ temp *= x /i ;
122+ result += temp ;
123+ }
124+ return result ;
125+ }
126+
127+ /**
128+ * This method compares the values of the exponential e^x
129+ * computed ith this class' method 'myexp' and
130+ * the ones computed with Math.exp(), showing them
131+ * in different columns.
132+ * @param x the exponent of the function to calculates
133+ */
134+ public static void check (double x ){
135+ System .out .print (x );
136+ System .out .print ("\t " );
137+ System .out .print (myexp (x ,100 ));
138+ System .out .print ("\t " );
139+ System .out .print (Math .exp (x ));
140+ System .out .println ();
141+ }
142+
143+ /**
144+ * This method calculates exp(-x^2),
145+ * given an integer x, using an
146+ * infinite series expansion of Gauss.
147+ * @param x the argument of the exponential
148+ * @param n the number of terms of the series
149+ * @return the value of the exponential
150+ */
151+ public static double gauss (double x , int n ){
152+ double result = 1 ;
153+ double temp = 1 ;
154+ for (int i =1 ; i <=n ; i ++){
155+ temp *= -x *x /i ;
156+ result += temp ;
157+ }
158+ return result ;
159+ }
160+
83161}
0 commit comments