-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
282 lines (201 loc) · 4.93 KB
/
Calculator.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Calculator
*
* version 1
*
* August 3, 2019
*
* Copyright © 2019 Sabrina Kim, Martial Pastor, Keven Presseau-St-Laurent, Marco Tropiano, Diana Zitting-Rioux.
* All rights reserved.
*/
/**
* Calculator class that has all the required functions for the calculator.
*
* @version 1
* @author Sabrina Kim, Martial Pastor, Keven Presseau-St-Laurent, Marco Tropiano, Diana Zitting-Rioux
*
*/
public class Calculator {
/**
* Constants
*/
final static double PICONST = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;
//-------------- MAIN FUNCTIONS ----------------
/**
* Exponent (e^x)
* @param n
* @param x
* @return result
*/
public static float calculateExponent(int n, float x){
float result = 1;
for (int i = 0 ; i < n ; ++i) {
result = 1 + x / ( n - i ) * result ;
}
return result;
}
/**
* 10^x
* @param n
* @return result
*/
public static float calculateTenPower(float n){
float result = 1;
if (n == 0)
return result;
boolean isNegative = false;
if (n < 0){
isNegative = true;
n *= -1;
}
String text = Float.toString(n);
int numbWhole = text.indexOf('.');
int numbDeci = text.length() - numbWhole - 1;
//System.out.println(text + "\tnumbDeci: " + numbDeci);
int maxDigits = 1;
for (int i = 0 ; i < numbDeci; ++i)
maxDigits *= 10;
//System.out.println("maxDigits: " + maxDigits);
int numerator = (int) (n * maxDigits);
int denominator = maxDigits;
//System.out.println("Fraction: " + numerator + "/" + denominator + "\tDecimal: " + (float) numerator/denominator);
int whole = 0;
while (numerator >= denominator){
whole++;
numerator -= denominator;
}
int decimal = numerator;
//System.out.println("Whole: " + whole + "\tDecimal: " + decimal);
result = (float) calculatePower(10, whole);
//System.out.println("\nRemaining Fraction:" + decimal + "/" + denominator);
double nRoot = findNthRoot(10, denominator);
float nextPart = (float) calculatePower(nRoot, decimal);
//System.out.println("\nnRoot: " + nRoot);
//nextPart = nRoot;
//System.out.println("Decimal: " + decimal);
//System.out.println("nextPart: " + nextPart);
result *= nextPart;
if (isNegative)
result = 1 / result;
return result;
}
/**
* cosh(x)
* @param x
* @return total
*/
public static double calculateCosh(double x){
//Known integer value of cosh
if (x == 0){
return 1;
}
//Taylor series ((x^2n)/(2n)!) summation from n = 0 to 149
//n = 0
double total = 1;
//n = 1 to 149
for(int i = 1; i < 150; i++){
total = total + calculatePower(x, (2 * i)) / calculateFactorial((2 * i));
}
return total;
}
/**
* Square root of x (x^(1/2))
* @param number
* @return
*/
public static double calculateSquareRoot(double number) {
if (number<0){ // Check that value is nonnegative
throw new IllegalArgumentException("Number must be greater than zero."); // TODO handle better
}
if (number == 0){ // If passed number is 0, return value 0
return number;
}
double t;
double squareRoot = number;
// Do guesses using Newthon's method until t is equal to squareRoot
do {
t = squareRoot;
squareRoot = ((number / t + t)) / 2;
} while ((t - squareRoot) != 0);
return squareRoot;
}
/**
* Sin(x) using rads
* @param inp
* @return result
*/
public static double calculateSine(double inp){
double inpMod = inp % (2.0*PICONST);
double result = inpMod;
for(int j = 3; j < 150; j+=4){
result -= (calculatePower(inpMod, j) / calculateFactorial(j));
result += (calculatePower(inpMod, j+2) / calculateFactorial(j+2));
}
return result;
}
//-------------- SECONDARY FUNCTIONS ----------------
/**
* Factorial
* @param val
* @return total
*/
public static double calculateFactorial(int val){
if (val == 0){
return 1;
}
else{
double total = 1;
for(int i = 0; i < val; i++){
total = total * (val - i);
}
return total;
}
}
/**
* Power with positive integer exponent
* @param base
* @param expo
* @return x
*/
public static double calculatePower(double base, int expo) {
if(expo == 0)
return 1;
return base * calculatePower(base, expo-1);
}
/**
* N root
* @param base
* @param expo
* @return x
*/
private static double findNthRoot(double base, int expo) {
double x = 1;
boolean accurate = false;
while (!accurate) {
double y = (1 / (double) expo) * ((expo - 1) * x + base / calculatePower(x, expo - 1));
accurate = isAccurate(x, y);
x = y;
}
return x;
}
/**
* Accuracy for n root
* @param x
* @param y
* @return boolean result
*/
private static boolean isAccurate(double x, double y) {
double result = y - x;
if (result < 0)
result *= -1;
return result < 0.000001;
}
/**
* Deg to rad
* @param inp
* @return inp
*/
public static double DegToRad(double inp){
return inp * PICONST / 180.0;
}
}