Repository navigation
Expand file tree
/
Copy pathExercise7.java
More file actions
161 lines (150 loc) · 4.43 KB
/
Copy pathExercise7.java
File metadata and controls
161 lines (150 loc) · 4.43 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
loop(10);
System.out.println(squareRoot(9.0));
System.out.println(power(2.0,8));
System.out.println(factorial(8));
System.out.println(myexp(8.0,100));
check(1);
for(double i=0.1; i<=100.0; i*=10){
check(i);
}
System.out.println("The values of exponential e^x for x=-0.1,-1.0,-10.0,-100.0 are:");
for(double i=-0.1; i>=-100.0; i*=10){
System.out.println(myexp(i,100));
}
System.out.println(gauss(2.0,100));
}
public static void loop(int n) {
int i = n;
while (i > 1) {
System.out.println(i);
if (i % 2 == 0) {
i = i / 2;
} else {
i = i + 1;
}
}
}
/**
* This method calculates an approximation
* of the square root of a number
* @param a the number whose root will be found
* @return the approximated result of the square root
*/
public static double squareRoot(double a){
Scanner in = new Scanner(System.in);
boolean ok;
System.out.println("Type a number: ");
do{ //number input check
ok = true;
if(!in.hasNextDouble()){
ok = false;
String word = in.next();
System.err.println(word + " is not a number!");
}
}while(!ok);
double x;
while(true){ //positive input check
x = in.nextDouble();
if(x<=0){
System.out.println("number is non-positive");
continue;
} else{break;}
};
double x1 = x;
do{ //compute and return the result of the square
x = x1;
x1 = (x + a/x) / 2;
}while(Math.abs(x-x1)>0.0001);
return x1;
}
/**
* This method calculates
* the value of the power x^n.
* @param x the base of the power
* @param n the exponent of the power
* @return the result of the power
*/
public static double power(double x, int n) {
while (n < 0) {
System.err.println("Exponent must be non-negative!");
break;
}
double power = x;
while (n > 0) {
if (n % 2 == 0) {
power *= Math.pow(x, n / 2);
n = n/2;
} else {
power *= Math.pow(x, n - 1);
n--;
}
}
return power;
}
/**
* This method computes the factorial function
* of a given integer n.
* @param n the argument of the factorial function
* @return the value of the factorial function
*/
public static int factorial(int n){
int result = 1;
for(int i=n;i > 1; i--){
result *= i;
}
return result;
}
/**
* This method calculates e^x
* using an infinite series expansion,
* whose i^th term is: x^i/i!.
* @param x the exponent of the exponential
* @param n the number of terms of the series
* we want to add
* @return the value of the exponential
*/
public static double myexp(double x, int n){
double result = 1;
double temp = 1;
for(int i=1; i <= n; i++){
temp *= x/i;
result += temp;
}
return result;
}
/**
* This method compares the values of the exponential e^x
* computed ith this class' method 'myexp' and
* the ones computed with Math.exp(), showing them
* in different columns.
* @param x the exponent of the function to calculates
*/
public static void check(double x){
System.out.print(x);
System.out.print("\t");
System.out.print(myexp(x,100));
System.out.print("\t");
System.out.print(Math.exp(x));
System.out.println();
}
/**
* This method calculates exp(-x^2),
* given an integer x, using an
* infinite series expansion of Gauss.
* @param x the argument of the exponential
* @param n the number of terms of the series
* @return the value of the exponential
*/
public static double gauss(double x, int n){
double result = 1;
double temp = 1;
for(int i=1; i<=n; i++){
temp *= -x*x/i;
result += temp;
}
return result;
}
}