-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber.cpp
More file actions
236 lines (207 loc) · 6.39 KB
/
Number.cpp
File metadata and controls
236 lines (207 loc) · 6.39 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
#include "Number.hpp"
Number::Number(vector<int> &dig, int b, int expo, bool sig) {
digits = dig;
base = b;
exponent = expo;
sign = sig;
// this->removeZeroes();
}
Number::Number(Number* n) {
this->digits = n->digits;
this->base = n->base;
this->exponent = n->exponent;
this->sign = n->sign;
// this->removeZeroes();
}
void Number::removeZeroes(bool maintain_exp, int exp) {
int i = this->digits.size() - 1;
int count = 0;
while(i > 0) {
// remove digit if zero
if(this->digits[i] == 0)
this->digits.pop_back();
else
break;
i--;
count++;
}
if(maintain_exp == true) {
this->addExponent(-1*count);
}
else if(exp != INT_MAX) {
this->exponent = exp;
}
else {
this->exponent = this->digits.size()-1;
}
// if(exp == INT_MAX && maintain_exp == false) this->exponent = this->digits.size() - 1;
// else this->exponent = exp;
}
Number* Number::addExponent(int e) {
this->exponent += e;
return this;
}
void Number::printNumber(int precision) {
// cout << (this->sign == false ? "+ ": "- ");
if(precision == INT_MAX) {
for(int i = this->digits.size() - 1;i >= 0;i--) {
// cout << this->digits[i] << " ";
cout << this->digits[i] << "";
if(i == this->digits.size() - 1 && this->digits.size() > 1)
// cout << ". ";
cout << ".";
}
}
else {
for(int i = this->digits.size() - 1;i >= this->digits.size() - precision - 1;i--) {
// cout << this->digits[i] << " ";
cout << this->digits[i] << "";
if(i == this->digits.size() - 1 && this->digits.size() > 1)
// cout << ". ";
cout << ".";
}
}
// cout << "* (" << this->base << " ^ " << this->exponent << ")";
cout << endl;
// cout << "Digits: " << (this->sign == 1 ? "- " : "+ ");
// for(int i = this->digits.size() - 1;i >= 0;i--) {
// cout << this->digits[i] << " ";
// }
// cout << endl;
// cout << "Exponent: " << this->exponent << endl;
}
void Number::rev() {
reverse(this->digits.begin(), this->digits.end());
}
// will return 1 if n1 > n2, 0 if n1 == n2, -1 if n1 < n2
int Number::compare(Number* n1, Number* n2, bool considerSign) {
// all numbers are in scientific notation, so the following method is followed
// check signs
// compare exponents
// if same, compare the msb
// if same, start comparing from msb - 1
if((n1->digits.size() == 1) && (n1->digits[0] == 0) && (n2->sign == false))
return -1;
if(considerSign) {
if(n1->sign ^ n2->sign != false) {
// if signs are different, then if first number's sign is negative, return -1
return n1->sign == true ? -1 : 1;
}
}
if(n1->exponent == n2->exponent) {
int i = n1->digits.size() - 1;
int j = n2->digits.size() - 1;
while(i >= 0 && j >= 0) {
if(n1->digits[i] == n2->digits[j]){
i--;
j--;
}
else {
return n1->digits[i] > n2->digits[j] ? 1 : -1;
}
}
// if both i and j are zero, numbers are equal
if(i == j) {
return 0;
}
// else return the number with more precision
else {
return i > j ? 1 : -1;
}
}
else {
return n1->exponent > n2->exponent ? 1 : -1;
}
}
Number* Number::retTwo(int b, int expo, bool sig) {
vector<int> two(1, 2);
// adjust for base
while(two[two.size() - 1]/b != 0) {
two.push_back(two[two.size()-1]/b);
two[two.size() - 2] %= b;
}
Number* Two = new Number(two, b, 0);
Two->removeZeroes();
return Two;
}
int Number::padNumbers(Number* A, Number* B) {
// implicit assumption that a.exp > b.exp
// difference in exponents
int diffExp = A->exponent - B->exponent;
// add zeroes to the beginning of second number
for(int i = 0;i < diffExp;i++)
B->digits.push_back(0);
// adjust exponent for the second number by reducing exponent
B->addExponent(-1*diffExp);
int sizeDiff = A->digits.size() - B->digits.size();
if(sizeDiff < 0) {
// a has smaller size, so pad it
for(int i = 0;i < -1*sizeDiff;i++) {
A->digits.insert(A->digits.begin(), 0);
}
}
else {
// b has smaller size, so pad it
for(int i = 0;i < sizeDiff;i++) {
B->digits.insert(B->digits.begin(), 0);
}
}
return diffExp;
}
pair<int, int> Number::QuoRem(int temp, int base) {
int quo, rem;
if(temp >= 0) {
quo = temp/base;
rem = temp%base;
}
else {
// if the number is negative
// // if mod == 0 quo = -temp/base
// // else if mod != 0 quo = -(temp/base + 1)
if(temp%base == 0) {
quo = temp/base;
rem = 0;
}
else {
quo = temp/base - 1;
rem = base + temp%base; // + because temp%base is negativez
}
}
return make_pair(quo, rem);
}
void Number::adjustForPrecision(int precision) {
// goal to make digitsAfterRealDecimal = precision
int digitsAfterRealDecimal = this->digits.size() - this->exponent - 1;
// pad with 0 if size < exponent
if(digitsAfterRealDecimal < 0) {
for(int i = 0;i < -1*digitsAfterRealDecimal;i++) {
this->digits.insert(this->digits.begin(), 0);
}
digitsAfterRealDecimal = 0;
}
// pad with 0 to make digitsAfterRealDecimal = precision
for(int i = 0;i < precision-digitsAfterRealDecimal;i++) {
this->digits.insert(this->digits.begin(), 0);
}
}
// returns the power to be subtracted from the num or denom
int Number::expressWihtoutDecimal() {
// all digits after scientific decimal are made to appear
// so that much power is subtracted
int res = this->digits.size() - 1;
this->addExponent(-1*(this->digits.size() - 1));
return res;
}
void Number::makeDigitsEqual(Number* A, Number* B) {
int sizeDiff = A->digits.size() - B->digits.size();
if(sizeDiff < 0) {
for(int i = 0;i < -1*sizeDiff;i++) {
A->digits.insert(A->digits.begin(), 0);
}
}
else {
for(int i = 0;i < sizeDiff;i++) {
B->digits.insert(B->digits.begin(), 0);
}
}
}