-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMaths.java
More file actions
1606 lines (1385 loc) · 54.5 KB
/
Maths.java
File metadata and controls
1606 lines (1385 loc) · 54.5 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package math;
import parser.STRING;
import static java.lang.Math.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Maths { //3.14159265358979323846;
public static BigDecimal PI =
new BigDecimal("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651");
public static String PI() {
return "3.1415926535897932";
}
@SuppressWarnings("empty-statement")
public static String fact(String p) {
String fact5 = "";
double i = 1;
double prod = 1;
double fact = 0;
double dbVal = Double.parseDouble(p);
double n = Math.floor(dbVal);
double k = dbVal - n;
double d = 160 + k;
if ((dbVal == 0) || (dbVal == 1.0)) {
fact = 1.0;
} else if ((dbVal < 0) && (k == 0)) {
fact = Double.NEGATIVE_INFINITY;
} else if ((dbVal <= d) && (k == 0)) {
while (dbVal > 0) {
prod *= dbVal;
dbVal--;
}//end while
fact = prod;
} else if ((dbVal <= d) && (k != 0)) {
double dSquare = d * d;
double dCube = dSquare * d;
double dFour = dCube * d;
double dFive = dFour * d;
double dSix = dFive * d;
double dSeven = dSix * d;
double dEight = dSeven * d;
double dNine = dEight * d;
double fact1 = Math.pow((d / Math.E), d) * Math.sqrt(2 * d * Math.PI) * (1 + (1 / (12 * d)) + (1 / (288 * dSquare)) - (139 / (51840 * dCube)) -
(59909 / (2.592E8 * dFour)) + (1208137 / (1.492992E9 * dFive)) - (1151957 / (1.875E11 * dSix)) - (101971 / (2.88E8 * dSeven)) +
(189401873 / (2.4E12 * dEight)) + (1293019 / (1.8E10 * dNine)));
while ((n + i) <= 160) {
prod *= (dbVal + i);
i++;
}
fact = fact1 / prod;
} else if ((dbVal > 160)) {
double dbValSquare = dbVal * dbVal;
double dbValCube = dbValSquare * dbVal;
double dbValFour = dbValCube * dbVal;
double dbValFive = dbValFour * dbVal;
double dbValSix = dbValFive * dbVal;
double dbValSeven = dbValSix * dbVal;
double dbValEight = dbValSeven * dbVal;
double dbValNine = dbValEight * dbVal;
double fact1 = Math.pow((dbVal / Math.E), dbVal) * Math.sqrt(2 * dbVal * Math.PI) * (1 + (1 / (12 * dbVal)) + (1 / (288 * dbValSquare)) - (139 / (51840 * dbValCube)) - (59909 / (2.592E8 * dbValFour)) + (1208137 / (1.492992E9 * dbValFive)) - (1151957 / (1.875E11 * dbValSix)) - (101971 / (2.88E8 * dbValSeven)) + (189401873 / (2.4E12 * dbValEight)) + (1293019 / (1.8E10 * dbValNine)));
fact = fact1;
}
fact5 = String.valueOf(fact);
return fact5;
}//end method fact
/**
* method getExponent returns the power to which 10 is raised when the number is written in standard form
* e.g an argument of 34.543 for the method gives a result of 1,since 34.543=3.4543*10^1
*
* @param num the double number argument whose exponent is desire
* @return the base 10 exponent of the number when written in standard form
*/
public static int getExponent(double num) {
double absVal = Math.abs(num);
int count = 0;
if (absVal < 1) {
while (absVal < 1) {
absVal *= 10;
count++;
}
count *= -1;
} else if (absVal > 10) {
while (absVal > 10) {
absVal /= 10;
count++;
}
}
return count;
}//end method getExponent
public static double logToAnyBase(double num, double base) {
return Math.log(num) / Math.log(base);
}
public static double antiLogToAnyBase(double num, double base) {
return Math.pow(base, num);
}
//returns the sign of a number string
private static String sign(String num) {
String sign = "";
if (num.length() > 0) {
if (num.substring(0, 1).equals("+") || num.substring(0, 1).equals("-")) {
sign = num.substring(0, 1);
} else {
sign = "+";
}
}
return sign;
}
private static boolean hasPoint(String num) {
if (num.indexOf(".") >= 0) {
return true;
} else if (num.indexOf(".") < 0) {
return false;
}
return false;
}
private static boolean hasExponent(String num) {
if (num.indexOf("E") >= 0) {
return true;
} else if (num.indexOf("E") < 0) {
return false;
}
return false;
}
private static boolean isNegNumber(String num) {
return (sign(num).equals("-"));
}
//returns true if the character immediately after the E is a -
private static boolean hasNegExponent(String num) {
boolean yes = false;
if (hasExponent(num) && num.substring(num.indexOf("E") + 1, num.indexOf("E") + 2).equals("-")) {
yes = true;
} else {
yes = false;
}
return yes;
}
//the character immediately after the E is a +
private static boolean hasPosExponent(String num) {
boolean yes = false;
if (hasExponent(num) && num.substring(num.indexOf("E") + 1, num.indexOf("E") + 2).equals("+")) {
yes = true;
} else {
yes = false;
}
return yes;
}
//the character immediately after the E is a digit
private static boolean hasUnsignedExp(String num) {
boolean yes = false;
if (hasExponent(num) && STRING.isDigit(num.substring(num.indexOf("E") + 1, num.indexOf("E") + 2))) {
yes = true;
} else {
yes = false;
}
return yes;
}
private static String abs_val(String num) {
if (isNegNumber(num)) {
num = num.substring(1);
} else {
num += "";
}
return num;
}
//assumes that the input has a point
private static String getNumbersBeforePoint(String num) {
String thenumbers = "";
if (hasPoint(num)) {
thenumbers = num.substring(0, num.indexOf("."));
} else if (!hasPoint(num)) {
thenumbers = "";
}
return thenumbers;
}
//assumes that the input has a point
private static String getNumbersAfterPoint(String num) {
String thenumbers = "";
if (hasExponent(num)) {
thenumbers = num.substring(num.indexOf(".") + 1, num.indexOf("E"));
} else if (!hasExponent(num)) {
thenumbers = num.substring(num.indexOf(".") + 1);
} else if (!hasPoint(num)) {
thenumbers = "";
}
return thenumbers;
}
//assumes that the input has a point.It does not return the sign along with the string.
private static String getNumbersAfterExp(String num) {
String thenumbers = "";
if (hasNegExponent(num) || hasPosExponent(num)) {
thenumbers = num.substring(num.indexOf("E") + 2);
} else if (hasUnsignedExp(num)) {
thenumbers = num.substring(num.indexOf("E") + 1);
} else if (!hasExponent(num)) {
thenumbers = "0";
}
return thenumbers;
}
//creates a string of n zeroes
private static String generateZeroes(int n) {
String gen = "";
for (int i = 0; i < n; i++) {
gen += "0";
}
return gen;
}
//returns the number of leading zeroes in a given number string
//utility method for std_form.Helps to standardize numbers after the floating point but before the
//exponent when the numbers before the floating point=0
private static int getNumberOfLeadingZeroes(String num) {
int index = 0;
for (int i = 0; i < num.length(); i++) {
if (num.substring(i, i + 1).equals("0")) {
index += 1;
} else {
break;
}
}
return index;
}
//writes the number in a.bcde...E[+|-]abcde....e.g 1.432E45
protected static String std_form(String num) {
String sign = sign(num);//get the sign of the number
String abs_val = abs_val(num);//get the absolute value of the number
if (!hasPoint(abs_val) && !hasExponent(abs_val)) {
abs_val += ".0E0";
} else if (hasPoint(abs_val) && !hasExponent(abs_val)) {
abs_val += "E0";
} else if (!hasPoint(abs_val) && hasExponent(abs_val)) {
abs_val = abs_val.substring(0, abs_val.indexOf("E")) + ".0" + abs_val.substring(abs_val.indexOf("E"));
} else if (hasPoint(abs_val) && hasExponent(abs_val)) {
abs_val += "";
}
String numbeforepoint = "";
String the_std_form_of_num = "";
long checkmethodgetNumbersBeforePoint = 0;
try {
checkmethodgetNumbersBeforePoint = Long.valueOf(getNumbersBeforePoint(abs_val));
} catch (NumberFormatException numa) {
checkmethodgetNumbersBeforePoint = 0;
}
if (checkmethodgetNumbersBeforePoint != 0) {
//this line gets the number preceding the point and at the same time removes all preceding zeroes in it.
//e.g 0034 becomes 34
numbeforepoint = String.valueOf(checkmethodgetNumbersBeforePoint);
//this line gets the number preceding the E and at the same time removes all preceding zeroes in it.
//e.g 0034 becomes 34
String numafterexp = String.valueOf(Long.valueOf(getNumbersAfterExp(abs_val)));
//rewrite number before point and after point but before the E ( if one exists ) in std_form e.g 4567=4.567E3
String transform_num_before_exp_to_std_form =
numbeforepoint.substring(0, 1) + "." + numbeforepoint.substring(1) + getNumbersAfterPoint(abs_val) + "E" + (numbeforepoint.length() - 1);
if (hasNegExponent(num)) {
the_std_form_of_num = transform_num_before_exp_to_std_form.substring(0, transform_num_before_exp_to_std_form.indexOf("E")) + "E" +
+(-Long.valueOf(numafterexp) + Long.valueOf(transform_num_before_exp_to_std_form.substring(transform_num_before_exp_to_std_form.indexOf("E") + 1)));
} else {
the_std_form_of_num = transform_num_before_exp_to_std_form.substring(0, transform_num_before_exp_to_std_form.indexOf("E")) + "E" +
+(Long.valueOf(numafterexp) + Long.valueOf(transform_num_before_exp_to_std_form.substring(transform_num_before_exp_to_std_form.indexOf("E") + 1)));
}
} else if (checkmethodgetNumbersBeforePoint == 0) {
//this line gets the number preceding the point and at the same time removes all preceding zeroes in it.
//e.g 0034 becomes 34
numbeforepoint = "0";
String numafterpoint = getNumbersAfterPoint(abs_val);
String numafterpointwithoutleadingzeroes = String.valueOf(Long.valueOf(numafterpoint));
//this line gets the number preceding the E and at the same time removes all preceding zeroes in it.
//e.g 0034 becomes 34
String numafterexp = String.valueOf(Long.valueOf(getNumbersAfterExp(abs_val)));
//rewrite number before point and after point but before the E ( if one exists ) in std_form e.g 4567=4.567E3
String transform_num_before_exp_to_std_form =
numafterpointwithoutleadingzeroes.substring(0, 1) + "." + numafterpointwithoutleadingzeroes.substring(1) +
"0E" + "-" + (1 + getNumberOfLeadingZeroes(numafterpoint));
if (hasNegExponent(num)) {
the_std_form_of_num = transform_num_before_exp_to_std_form.substring(0, transform_num_before_exp_to_std_form.indexOf("E")) + "E" +
+(-Long.valueOf(numafterexp) + Long.valueOf(transform_num_before_exp_to_std_form.substring(transform_num_before_exp_to_std_form.indexOf("E") + 1)));
} else {
the_std_form_of_num = transform_num_before_exp_to_std_form.substring(0, transform_num_before_exp_to_std_form.indexOf("E")) + "E" +
+(Long.valueOf(numafterexp) + Long.valueOf(transform_num_before_exp_to_std_form.substring(transform_num_before_exp_to_std_form.indexOf("E") + 1)));
}
}
String standardized = "";
if (sign.equals("+")) {
standardized = the_std_form_of_num;
} else if (sign.equals("-")) {
standardized = sign + the_std_form_of_num;
}
int j = standardized.indexOf("E");
String modify1 = standardized.substring(0, j + 1);
String modify2 = standardized.substring(j + 1);
for (int i = j - 1; i >= 0; i--) {
if (modify1.substring(i, i + 1).equals("0")) {
modify1 = STRING.replace(modify1, "", i, i + 1);
} else {
break;
}
}
return modify1 + modify2;
}
/**
* @param num The number to examine
* @return the number as a non-exponential number.
*/
public static String non_exp_format(String num) {
String result = "";//variable that stores the result
if (hasExponent(num)) {
String sign = sign(num);//get the sign of the number
String abs_val = abs_val(num);//get the absolute value of the number
String std_form = std_form(abs_val);//convert the input to its standard forme.g a.bcde...Eabcde....i.e1.2598E12 e.t.c
String std_form1 = std_form;//store std_form in variable std_form1
std_form1 = STRING.delete(std_form1, ".");//remove the floating point
int L = std_form1.substring(0, std_form1.indexOf("E")).length();
int expsize = Integer.valueOf(getNumbersAfterExp(std_form1));
if (hasNegExponent(num)) {
expsize *= -1;
}
if (expsize >= 0) {
if (L > expsize) {
result = std_form1.substring(0, expsize + 1) +
"." + std_form1.substring(expsize + 1, std_form1.indexOf("E"));
} else if (L <= expsize) {
result = std_form1.substring(0, std_form1.indexOf("E")) + generateZeroes(expsize - L + 1);
}
} else if (expsize < 0) {
result = "0." + generateZeroes(Math.abs(expsize) - 1) + std_form1.substring(0, std_form1.indexOf("E"));
}
if (sign.equals("-")) {
result = sign + result;
} else {
result += "";
}
}//end if
else if (!hasExponent(num)) {
result = num;
}
return result;
}
/**
* method dec_to_other_base takes 2 arguments,the decimal number to be converted,
* and the base to which the number is to be converted.
* This method has the ability to convert the whole part of a decimal number to a specified base.
*
* @param dec_no =the decimal number to be converted,
* @param base_no =the base to which the number is to be converted.
* @return the representation of the input decimal number in the specified base system.
* @throws
*/
private static String whole_dec_to_other_base(String dec_no, String base_no) {
if (!dec_no.contains(".") && !base_no.contains(".")) {
int decimalno = 0;
int baseno = 0;
String h;
try {
decimalno = Integer.parseInt(dec_no);
baseno = Integer.parseInt(base_no);
h = "";
while (decimalno > 0) {
int rem = decimalno % baseno;
h += rem;
if ((rem == 0) && (rem < baseno)) {
decimalno /= baseno;
}//end if
else if ((rem > 0) && (rem < baseno)) {
decimalno = (decimalno - rem) / baseno;
}//end else if
}//end for
return STRING.reverse(h);
} catch (NumberFormatException num) {
System.out.println(dec_no);
System.out.println(base_no);
num.printStackTrace();
throw new NumberFormatException("Only integers are expected here!");
}
}//end if
else {
throw new NumberFormatException("Only integers are expected here!");
}
}
/**
* method frac_dec_to_other_base takes 2 arguments,the decimal number to be converted,
* and the base to which the number is to be converted. It however deals with positive decimals less than 1 and greater than 0.
* This method has the ability to convert a decimal number to a specified base
*
* @param dec_no =the decimal number to be converted,
* @param base_no =the base to which the number is to be converted.
* @return the representation of the input decimal number in the specified base system.
* @throws
*/
private static String frac_dec_to_other_base(String dec_no, String base_no) {
String h = "";
String j = "";
double f = 0;
double f1 = 0;
try {
f = Double.valueOf(dec_no);
f1 = Integer.parseInt(base_no);
while (h.length() <= 18) {
h += String.valueOf(f1 * f).substring(0, String.valueOf(f1 * f).indexOf("."));
if (f1 * f * f1 < f1) {
f = f1 * f;
} else if (f1 * f * f1 >= f1) {
f = Double.valueOf(String.valueOf(f1 * f).substring(String.valueOf(f1 * f).indexOf(".")));
}
}//end while
}//end try
catch (NullPointerException nola) {
h = STRING.reverse("PLEASE ENTER A NUMBER");
} catch (NumberFormatException no) {
h = STRING.reverse("NUMBER TOO LARGE");
} catch (IndexOutOfBoundsException ind) {
}
h = "." + h;
int indexOfPoint = h.indexOf(".");
if (indexOfPoint != -1) {
int len = h.length();
int i = len - 1;
while (h.substring(i, i + 1).equals("0")) {
i--;
}
h = h.substring(0, i + 1);
if (h.endsWith(".")) {
h = h + "0";
}
}
return h;
}
/**
* method dec_to_other_base takes 2 arguments,the decimal number to be converted,
* and the base to which the number is to be converted.
* This method has the ability to convert a decimal number to a specified base
*
* @param dec_no =the decimal number to be converted,
* @param base_no =the base to which the number is to be converted.
* @return the representation of the input decimal number in the specified base system.
*/
public static String dec_to_other_base(String dec_no, String base_no) {
if (dec_no.contains("E") || dec_no.contains("Є")) {
dec_no = Maths.non_exp_format(dec_no);//make the input number devoid of E
}
String v = "";
int kk = 0;
for (int i = 0; i < dec_no.length(); i++) {//record the incidence of floating points
if (dec_no.substring(i, i + 1).equals(".")) {
kk++;
break;
}
}
String nio = "";
if (dec_no.substring(0, 1).equals("-")) {
nio = "-";
v = dec_no.substring(1);
} else if (!dec_no.substring(0, 1).equals("-")) {
nio = "";
v = dec_no;
}
String r = "";
String y = "";
if (kk == 0) {//no point
r = v; //get the whole part of the number
y = ".0";//get the decimal part of the number
} else if (kk != 0) {//a point occurs
r = v.substring(0, v.indexOf("."));//get the whole part of the number
y = v.substring(v.indexOf("."));//get the decimal part of the number
}
String v1 = whole_dec_to_other_base(r, base_no);//the conversion of the whole part of the entry
String v2 = frac_dec_to_other_base(y, base_no);//the conversion of the fractional part of the entry
int u = 0;//variable that when equal to v2.length()-1 implies that v2 is equal to zero
for (int i = 0; i < v2.length(); i++) {
if (!v2.substring(i, i + 1).equals("0") && !v2.substring(i, i + 1).equals(".")) {
v2 += "";
break;
} else {
u++;
}
}
if (u == v2.length()) {
v2 = ".0";
} else {
v2 += "";
}
// return nio+whole_dec_to_other_base(r, base_no)+frac_dec_to_other_base(y, base_no);
return nio + v1 + v2;
}
/**
* method num_to_base_10 takes 2 arguments,the number to be converted to base 10,
* and the base system to which the number currently belongs.
* This method has the ability to convert a number in a specified base back to
* base 10 and so has the effect of reversing the action of method dec_to_other_base.
*
* @param num is the number to be converted to base 10
* @param num_base is the base to which the number currently belongs
* @return the decimal representation of the input.
*/
public static String num_to_base_10(String num, String num_base) {
String h = "";//variable that will store the output of this method
if (Integer.valueOf(num_base) <= 1) {
throw new NumberFormatException("Invalid Number Base.");
} else if (Integer.valueOf(num_base) > 1) {
num = STRING.purifier(num);
num_base = STRING.purifier(num_base);//remove all white spaces
String nio = num;//store the original variable in nio
// so that you can freely manipulate num to handle positive and negative numbers too
int point_watch = -1;//if point_watch>-1 then the input contains a floating point,else
//point_watch=-1 the input contains no floating point
//The first if in the loop below checks if the digits in num are valid digits for the base system
//of operation.If any digit in the input is equal to or greater than the base system,then the entry
//is not a valid number under that base system so it instructs the software
//to generate a NumberFormatException
//The second if specifies the valid components of any number and checks all components of the input
//to see if they are valid components.If any component of the input is not a valid number component,
//The logic instructs the software to generate a NumberFormatException
//The last if is only a floating point watchdog and checks to see if the input
// contains a floating point or not.If a component of the number is a floating
//point,the system documents or remembers this by incrementing variable point_watch
for (int i = 0; i < num.length(); i++) {
if (STRING.isDigit(num.substring(i, i + 1)) && Integer.valueOf(num.substring(i, i + 1)) >= Integer.valueOf(num_base)) {
num = "";
throw new NumberFormatException();
}
if (!num.substring(i, i + 1).equals(".") && !STRING.isDigit(num.substring(i, i + 1)) && !num.substring(i, i + 1).equals("-") && !num.substring(i, i + 1).equals("E")) {
num = "";
throw new NumberFormatException();
}
if (num.substring(i, i + 1).equals(".")) {
point_watch++;
}
}
//The string v created below is a utility string for specifying what happens to our input under
//various circumstances.
//The if-else statement below gets the absolute value of the number and stores it in v.
String v = "";
if (num.substring(0, 1).equals("-")) {
v = num.substring(1);
} else {
v = num;
}
//Two strings r and y for storing the whole and the decimal part of the entry
//under various circumstances
String r = "";
String y = "";
try {
int u = Integer.valueOf(num_base);
if (point_watch == -1) {//no points
r = v;
y = "0";
r = STRING.reverse(r);
} else if (point_watch != -1) {
r = v.substring(0, v.indexOf("."));//get the whole part of the number
y = v.substring(v.indexOf(".") + 1);//get the decimal part of the number
r = STRING.reverse(r);//This reverse of r is done so that we can loop easily later on
//from the beginning of r instead of from its end.
}
// i is a loop incrementer initialized to zero.H is used to accumulate calculations done within the loop.
//The first loop uses H to accumulate its calculations while the second one below uses variable Ha to
//accomplish the same purpose.H accumulates the whole part of the conversion,
//while Ha accumulates the fractional part.
int i = 0;
double H = 0;
while (i < r.length()) {//process the whole part
H += (Double.valueOf(r.substring(i, i + 1)) * Math.pow(u, i));
i++;
}
i = 0;
double Ha = 0;
while (i < y.length()) {//process the decimal part
Ha += (Double.valueOf(y.substring(i, i + 1)) * Math.pow(u, -i - 1));
i++;
}
//Combine the 2 parts of the conversion to get the full conversion
H += Ha;
//Call the original string and check if it was negative or positive
//and reflect this in the conversion.
if (nio.substring(0, 1).equals("-")) {
h = "-" + String.valueOf(H);
} else if (!nio.substring(0, 1).equals("-")) {
h = String.valueOf(H);
}
} catch (NullPointerException nol) {
h = "SYNTAX ERROR at null";
} catch (IndexOutOfBoundsException ind) {
h = "SYNTAX ERROR at ind";
ind.printStackTrace();
} catch (NumberFormatException numa) {
h = "SYNTAX ERROR at numa";
numa.printStackTrace();
}
}
return h;
}
/**
* Method changeBase is designed to give flexibility in converting from one base to another
* method changeBase takes 3 arguments,the number to be converted to base another base,
* the base system to which the number currently belongs,and the base to which the number is to be converted
* This method has the ability to convert a number in a specified base back to
* base 10 and so has the effect of reversing the action of method dec_to_other_base.
*
* @param num is the number to be converted to a new base
* @param num_base is the base to which the number currently belongs
* @param base is the base to which the number is to be converted.
* @return the decimal representation of the input.
*/
public static String changeBase(String num, String num_base, String base) {
String h = num_to_base_10(num, num_base);
String val = dec_to_other_base(h, base);
return val.startsWith(".") ? 0 + val : val;
}
/**
* @param num1 The first number.
* @param base1 The base system of the first number.
* @param num2 The second number.
* @param base2 The base system of the second number.
* @param resultbase The base system of the result.
* @return the sum of the 2 numbers in the
* target base system.
*/
public static String add(String num1, int base1, String num2, int base2, int resultbase) {
String no1_to_base10 = changeBase(num1, String.valueOf(base1), "10");
String no2_to_base10 = changeBase(num2, String.valueOf(base2), "10");
return changeBase(String.valueOf(Double.parseDouble(no1_to_base10) + Double.parseDouble(no2_to_base10)),
"10", String.valueOf(resultbase));
}
/**
* @param num1 The first number.
* @param base1 The base system of the first number.
* @param num2 The second number.
* @param base2 The base system of the second number.
* @param resultbase The base system of the result.
* @return the difference of the 2 numbers in the
* target base system.
*/
public static String subtract(String num1, int base1, String num2, int base2, int resultbase) {
String no1_to_base10 = changeBase(num1, String.valueOf(base1), "10");
String no2_to_base10 = changeBase(num2, String.valueOf(base2), "10");
return changeBase(String.valueOf(Double.parseDouble(no1_to_base10) - Double.parseDouble(no2_to_base10)),
"10", String.valueOf(resultbase));
}
/**
* @param num1 The first number.
* @param base1 The base system of the first number.
* @param num2 The second number.
* @param base2 The base system of the second number.
* @param resultbase The base system of the result.
* @return the division product of the 2 numbers in the
* target base system.
*/
public static String divide(String num1, int base1, String num2, int base2, int resultbase) {
String no1_to_base10 = changeBase(num1, String.valueOf(base1), "10");
String no2_to_base10 = changeBase(num2, String.valueOf(base2), "10");
return changeBase(String.valueOf(Double.parseDouble(no1_to_base10) / Double.parseDouble(no2_to_base10)),
"10", String.valueOf(resultbase));
}
/**
* @param num1 The first number.
* @param base1 The base system of the first number.
* @param num2 The second number.
* @param base2 The base system of the second number.
* @param resultbase The base system of the result.
* @return the product of the 2 numbers in the
* target base system.
*/
public static String multiply(String num1, int base1, String num2, int base2, int resultbase) {
String no1_to_base10 = changeBase(num1, String.valueOf(base1), "10");
String no2_to_base10 = changeBase(num2, String.valueOf(base2), "10");
return changeBase(String.valueOf(Double.parseDouble(no1_to_base10) * Double.parseDouble(no2_to_base10)),
"10", String.valueOf(resultbase));
}
/**
* Method scanintoList is designed to scan a string of numbers separated by commas into a List
* serves to separate the individual number objects in a number string
*
* @param s is the string of numbers separated by commas
* @return the List of scanned numbers
*/
public static List<String> scanintoList(String s) {//A string of numbers separated by commas
s += ",";//append a , to the end so that the scanner will detect the last number in the string too
String acc = "";//read in numbers into this string.
int commacounter = 0;
List<String> input = new ArrayList<String>();
for (int i = 0; i < s.length(); i++) {
acc += s.substring(i, i + 1);
if (s.substring(i, i + 1).equals(",")) {
acc = Maths.non_exp_format(acc);
try {
input.add(STRING.delete(acc, ","));
acc = "";
++commacounter;
} catch (NumberFormatException num) {
}
}
}
return input;
}
public static double degToRad(double deg) {//from degrees to radians
return deg * (Math.PI / 180.0);
}
public static double radToDeg(double rad) {//from rad to degrees
return rad * (180.0 / Math.PI);
}
public static double degToGrad(double deg) {//from rad to degrees
return (10 * deg / 9.0);
}
public static double gradToDeg(double grad) {//from grad to degrees
return 0.9 * grad;
}
public static double radToGrad(double rad) {//from rad to grad
return rad * (200.0 / Math.PI);
}
public static double gradToRad(double grad) {//from rad to degrees
return grad * (Math.PI / 200.0);
}
/**
* @param angRad the angle in rads
* @return the sine of an angle in degrees
*/
public static double sinRadToDeg(double angRad) {
return sin(radToDeg(angRad));
}
/**
* @param angDeg the angle in degs
* @return the sine of an angle in rads
*/
public static double sinDegToRad(double angDeg) {
return sin(degToRad(angDeg));
}
/**
* @param angRad the angle in rads
* @return the sine of an angle in grad
*/
public static double sinRadToGrad(double angRad) {
return sin(radToGrad(angRad));
}
/**
* @param angGrad the angle in grads
* @return the sine of an angle in rad
*/
public static double sinGradToRad(double angGrad) {
return sin(gradToRad(angGrad));
}
/**
* @param angGrad the angle in degs
* @return the sine of an angle in grad
*/
public static double sinDegToGrad(double angGrad) {
return sin(degToGrad(angGrad));
}
/**
* @param angDeg the angle in degs
* @return the sine of an angle in grad
*/
public static double sinGradToDeg(double angDeg) {
return sin(gradToDeg(angDeg));
}
/**
* @param angRad the angle in rads
* @return the cosine of an angle in degrees
*/
public static double cosRadToDeg(double angRad) {
return cos(radToDeg(angRad));
}
/**
* @param angDeg the angle in degs
* @return the cosine of an angle in rads
*/
public static double cosDegToRad(double angDeg) {
return cos(degToRad(angDeg));
}
/**
* @param angRad the angle in rads
* @return the cosine of an angle in grad
*/
public static double cosRadToGrad(double angRad) {
return cos(radToGrad(angRad));
}
/**
* @param angGrad the angle in grads
* @return the cosine of an angle in rad
*/
public static double cosGradToRad(double angGrad) {
return cos(gradToRad(angGrad));
}
/**
* @param angGrad the angle in degs
* @return the cosine of an angle in grad
*/
public static double cosDegToGrad(double angGrad) {
return cos(degToGrad(angGrad));
}
/**
* @param angDeg the angle in degs
* @return the cosine of an angle in grad
*/
public static double cosGradToDeg(double angDeg) {
return cos(gradToDeg(angDeg));
}
/**
* @param angRad the angle in rads
* @return the tangent of an angle in degrees
*/
public static double tanRadToDeg(double angRad) {
return tan(radToDeg(angRad));
}
/**
* @param angDeg the angle in degs
* @return the tangent of an angle in rads
*/
public static double tanDegToRad(double angDeg) {
return tan(degToRad(angDeg));
}
/**
* @param angRad the angle in rads
* @return the tangent of an angle in grad
*/
public static double tanRadToGrad(double angRad) {
return tan(radToGrad(angRad));
}
/**
* @param angGrad the angle in grads
* @return the tangent of an angle in rad
*/
public static double tanGradToRad(double angGrad) {
return tan(gradToRad(angGrad));
}
/**
* @param angGrad the angle in degs
* @return the tangent of an angle in grad
*/
public static double tanDegToGrad(double angGrad) {
return tan(degToGrad(angGrad));
}
/**
* @param angDeg the angle in degs
* @return the tangent of an angle in grad
*/
public static double tanGradToDeg(double angDeg) {
return tan(gradToDeg(angDeg));
}
/**
* @param angRad the angle in rads
* @return the arctan of an angle in degrees
*/
public static double atanRadToDeg(double angRad) {
return radToDeg(atan(angRad));
}
/**
* @param angRad the angle in rads
* @return the arccos of an angle in degrees
*/
public static double acosRadToDeg(double angRad) {
return radToDeg(acos(angRad));
}
/**
* @param angRad the angle in rads
* @return the arcsine of an angle in degrees
*/
public static double asinRadToDeg(double angRad) {
return radToDeg(asin(angRad));
}
/**
* @param angDeg the angle in rads