-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number.java
192 lines (190 loc) · 5.18 KB
/
Number.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
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
public class Number {
public double value;
public int sigFigs;
public int uncertainty;
public Number(String num) {
this.value = Double.parseDouble(num);
this.sigFigs = sigFig(num);
this.uncertainty = findDig(num);
}
public Number(double value, int sigFigs, int uncertainty) {
this.value = value;
this.sigFigs = sigFigs;
this.uncertainty = uncertainty;
}
public Number add(Number num) {
double newValue = value + num.value;
int newUncertainty = Math.max(uncertainty, num.uncertainty);
int newSigFigs = sigFig(numToUncertainty(newValue, newUncertainty));
return new Number(newValue, newSigFigs, newUncertainty);
}
public Number subtract(Number num) {
double newValue = value - num.value;
int newUncertainty = Math.max(uncertainty, num.uncertainty);
int newSigFigs = sigFig(numToUncertainty(newValue, newUncertainty));
return new Number(newValue, newSigFigs, newUncertainty);
}
public Number multiply(Number num) {
double newValue = value * num.value;
int newSigFigs = Math.min(sigFigs, num.sigFigs);
String number = numToString(newValue, newSigFigs);
return new Number(number);
}
public Number divide(Number num) {
double newValue = value / num.value;
int newSigFigs = Math.min(sigFigs, num.sigFigs);
String number = numToString(newValue, newSigFigs);
return new Number(number);
}
public String numToUncertainty(double value, int uncertainty) {
if (uncertainty >= 0) {
value /= Math.pow(10, uncertainty);
value = Math.round(value);
value *= Math.pow(10, uncertainty);
return Integer.toString((int) value);
}
else {
value /= Math.pow(10, uncertainty);
value = Math.round(value);
value *= Math.pow(10, uncertainty);
String format = "#.";
for (int i = 0; i > uncertainty; i--) {
format += "#";
}
return new DecimalFormat(format).format(value);
}
}
public static int sigFig(String str) {
boolean decimalFound = false;
boolean prevNumExists = false;
int count = 0;
int zeroInARow = 0;
int i = 0;
for (; str.charAt(i) == '0' && i < str.length(); i++)
;
for (; i < str.length(); i++) {
char cur = str.charAt(i);
if (cur == '.') {
count += zeroInARow;
zeroInARow = 0;
decimalFound = true;
prevNumExists = false;
} else if (cur == '0') {
zeroInARow++;
} else {
count++;
if (!decimalFound || prevNumExists) {
count += zeroInARow;
}
prevNumExists = true;
zeroInARow = 0;
}
}
if (decimalFound) {
count += zeroInARow;
}
return count;
}
public static int findDig(String str) {
str = additionRefine(str);
boolean whole = str.charAt(str.length()-1)=='.';
for(int i=str.length()-1-(whole?1:0);i>=0;i--){
char cur = str.charAt(i);
if(whole){
if(cur!='0'){
return str.length()-2-i;
}
}else{
if(cur=='.'){
return i-str.length()+1;
}
}
}
return Integer.MAX_VALUE;
}
public static String additionRefine(String str) {
String ans = "";
int i = 0;
boolean foundDecimal = false;
for (; i < str.length() && str.charAt(i) == '0'; i++)
;
for (; i < str.length(); i++) {
char cur = str.charAt(i);
if (cur == '.') {
foundDecimal = true;
}
ans += cur;
if (!foundDecimal) {
ans += ".";
}
}
return ans;
}
public static String numToString(double val, int sigs) {
BigDecimal bd = new BigDecimal(val);
bd = bd.round(new MathContext(sigs));
return bd.toPlainString();
}
public double getValue(){
return value;
}
public int getSigFigs(){
return sigFigs;
}
// public static String numToString(double value, int sigFigs) {
// String raw = "" + value;
// boolean decimalFound = false;
// int count = 0;
// for (int i = raw.charAt(0) == '0' ? 1 : 0; i < raw.length(); i++) {
// char cur = raw.charAt(i);
// if (cur == '.') {
// decimalFound = true;
// } else {
// count++;
// if (count > sigFigs) {
// boolean roundUp = false;
// if (i < raw.length() - 1) {
// char next = raw.charAt(i + 1);
// if (next == '.') {
// System.out.println("xd");
// if (i < raw.length() - 2 && raw.charAt(i + 2) >= '5')
// roundUp = true;
// } else {
// if (next >= '5')
// roundUp = true;
// }
// }
// String refined;
// if (roundUp) {
// System.out.println("xd");
// refined = raw.substring(0, i - 1) + (char) (raw.charAt(i-1) + 1);
// } else {
// refined = raw.substring(0, i);
// }
// if (!decimalFound) {
// for (int j = i; raw.charAt(j) != '.'; j++) {
// refined += '0';
// }
// } else if (refined.charAt(refined.length() - 1) == '.') {
// refined = refined.substring(0, refined.length() - 1);
// }
// return refined;
// }
// }
// }
// return raw;
// }
@Override
public String toString() {
return numToString(value, sigFigs);
}
public static void main(String[] args) {
Number n1 = new Number("420");
Number n2 = new Number("5.5");
Number sub = n1.add(n2);
System.out.println(sub.toString());
}
}