-
Notifications
You must be signed in to change notification settings - Fork 130
/
binary_arithmetic.hpp
547 lines (433 loc) · 13.5 KB
/
binary_arithmetic.hpp
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
/*
===========================================================================
Binary arithmetic operators
===========================================================================
*/
#ifndef BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
#define BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP
#include <climits>
#include <cmath>
#include <string>
#include "BigInt.hpp"
#include "constructors/constructors.hpp"
#include "functions/math.hpp"
#include "functions/utility.hpp"
#include "operators/arithmetic_assignment.hpp"
#include "operators/assignment.hpp"
#include "operators/increment_decrement.hpp"
#include "operators/relational.hpp"
#include "operators/unary_arithmetic.hpp"
const long long FLOOR_SQRT_LLONG_MAX = 3037000499;
/*
BigInt + BigInt
---------------
The operand on the RHS of the addition is `num`.
*/
BigInt BigInt::operator+(const BigInt& num) const {
// if the operands are of opposite signs, perform subtraction
if (this->sign == '+' and num.sign == '-') {
BigInt rhs = num;
rhs.sign = '+';
return *this - rhs;
}
else if (this->sign == '-' and num.sign == '+') {
BigInt lhs = *this;
lhs.sign = '+';
return -(lhs - num);
}
// identify the numbers as `larger` and `smaller`
std::string larger, smaller;
std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
BigInt result; // the resultant sum
result.value = ""; // the value is cleared as the digits will be appended
short carry = 0, sum;
// add the two values
for (long i = larger.size() - 1; i >= 0; i--) {
sum = larger[i] - '0' + smaller[i] - '0' + carry;
result.value = std::to_string(sum % 10) + result.value;
carry = sum / (short) 10;
}
if (carry)
result.value = std::to_string(carry) + result.value;
// if the operands are negative, the result is negative
if (this->sign == '-' and result.value != "0")
result.sign = '-';
return result;
}
/*
BigInt - BigInt
---------------
The operand on the RHS of the subtraction is `num`.
*/
BigInt BigInt::operator-(const BigInt& num) const {
// if the operands are of opposite signs, perform addition
if (this->sign == '+' and num.sign == '-') {
BigInt rhs = num;
rhs.sign = '+';
return *this + rhs;
}
else if (this->sign == '-' and num.sign == '+') {
BigInt lhs = *this;
lhs.sign = '+';
return -(lhs + num);
}
BigInt result; // the resultant difference
// identify the numbers as `larger` and `smaller`
std::string larger, smaller;
if (abs(*this) > abs(num)) {
larger = this->value;
smaller = num.value;
if (this->sign == '-') // -larger - -smaller = -result
result.sign = '-';
}
else {
larger = num.value;
smaller = this->value;
if (num.sign == '+') // smaller - larger = -result
result.sign = '-';
}
// pad the smaller number with zeroes
add_leading_zeroes(smaller, larger.size() - smaller.size());
result.value = ""; // the value is cleared as the digits will be appended
short difference;
long i, j;
// subtract the two values
for (i = larger.size() - 1; i >= 0; i--) {
difference = larger[i] - smaller[i];
if (difference < 0) {
for (j = i - 1; j >= 0; j--) {
if (larger[j] != '0') {
larger[j]--; // borrow from the j-th digit
break;
}
}
j++;
while (j != i) {
larger[j] = '9'; // add the borrow and take away 1
j++;
}
difference += 10; // add the borrow
}
result.value = std::to_string(difference) + result.value;
}
strip_leading_zeroes(result.value);
// if the result is 0, set its sign as +
if (result.value == "0")
result.sign = '+';
return result;
}
/*
BigInt * BigInt
---------------
Computes the product of two BigInts using Karatsuba's algorithm.
The operand on the RHS of the product is `num`.
*/
BigInt BigInt::operator*(const BigInt& num) const {
if (*this == 0 or num == 0)
return BigInt(0);
if (*this == 1)
return num;
if (num == 1)
return *this;
BigInt product;
if (abs(*this) <= FLOOR_SQRT_LLONG_MAX and abs(num) <= FLOOR_SQRT_LLONG_MAX)
product = std::stoll(this->value) * std::stoll(num.value);
else if (is_power_of_10(this->value)){ // if LHS is a power of 10 do optimised operation
product.value = num.value;
product.value.append(this->value.begin() + 1, this->value.end());
}
else if (is_power_of_10(num.value)){ // if RHS is a power of 10 do optimised operation
product.value = this->value;
product.value.append(num.value.begin() + 1, num.value.end());
}
else {
// identify the numbers as `larger` and `smaller`
std::string larger, smaller;
std::tie(larger, smaller) = get_larger_and_smaller(this->value, num.value);
size_t half_length = larger.size() / 2;
auto half_length_ceil = (size_t) ceil(larger.size() / 2.0);
BigInt num1_high, num1_low;
num1_high = larger.substr(0, half_length);
num1_low = larger.substr(half_length);
BigInt num2_high, num2_low;
num2_high = smaller.substr(0, half_length);
num2_low = smaller.substr(half_length);
strip_leading_zeroes(num1_high.value);
strip_leading_zeroes(num1_low.value);
strip_leading_zeroes(num2_high.value);
strip_leading_zeroes(num2_low.value);
BigInt prod_high, prod_mid, prod_low;
prod_high = num1_high * num2_high;
prod_low = num1_low * num2_low;
prod_mid = (num1_high + num1_low) * (num2_high + num2_low)
- prod_high - prod_low;
add_trailing_zeroes(prod_high.value, 2 * half_length_ceil);
add_trailing_zeroes(prod_mid.value, half_length_ceil);
strip_leading_zeroes(prod_high.value);
strip_leading_zeroes(prod_mid.value);
strip_leading_zeroes(prod_low.value);
product = prod_high + prod_mid + prod_low;
}
strip_leading_zeroes(product.value);
if (this->sign == num.sign)
product.sign = '+';
else
product.sign = '-';
return product;
}
/*
divide
------
Helper function that returns the quotient and remainder on dividing the
dividend by the divisor, when the divisor is 1 to 10 times the dividend.
*/
std::tuple<BigInt, BigInt> divide(const BigInt& dividend, const BigInt& divisor) {
BigInt quotient, remainder, temp;
temp = divisor;
quotient = 1;
while (temp < dividend) {
quotient++;
temp += divisor;
}
if (temp > dividend) {
quotient--;
remainder = dividend - (temp - divisor);
}
return std::make_tuple(quotient, remainder);
}
/*
BigInt / BigInt
---------------
Computes the quotient of two BigInts using the long-division method.
The operand on the RHS of the division (the divisor) is `num`.
*/
BigInt BigInt::operator/(const BigInt& num) const {
BigInt abs_dividend = abs(*this);
BigInt abs_divisor = abs(num);
if (num == 0)
throw std::logic_error("Attempted division by zero");
if (abs_dividend < abs_divisor)
return BigInt(0);
if (num == 1)
return *this;
if (num == -1)
return -(*this);
BigInt quotient;
if (abs_dividend <= LLONG_MAX and abs_divisor <= LLONG_MAX)
quotient = std::stoll(abs_dividend.value) / std::stoll(abs_divisor.value);
else if (abs_dividend == abs_divisor)
quotient = 1;
else if (is_power_of_10(abs_divisor.value)) { // if divisor is a power of 10 do optimised calculation
size_t digits_in_quotient = abs_dividend.value.size() - abs_divisor.value.size() + 1;
quotient.value = abs_dividend.value.substr(0, digits_in_quotient);
}
else {
quotient.value = ""; // the value is cleared as digits will be appended
BigInt chunk, chunk_quotient, chunk_remainder;
size_t chunk_index = 0;
chunk_remainder.value = abs_dividend.value.substr(chunk_index, abs_divisor.value.size() - 1);
chunk_index = abs_divisor.value.size() - 1;
while (chunk_index < abs_dividend.value.size()) {
chunk.value = chunk_remainder.value.append(1, abs_dividend.value[chunk_index]);
chunk_index++;
while (chunk < abs_divisor) {
quotient.value += "0";
if (chunk_index < abs_dividend.value.size()) {
chunk.value.append(1, abs_dividend.value[chunk_index]);
chunk_index++;
}
else
break;
}
if (chunk == abs_divisor) {
quotient.value += "1";
chunk_remainder = 0;
}
else if (chunk > abs_divisor) {
strip_leading_zeroes(chunk.value);
std::tie(chunk_quotient, chunk_remainder) = divide(chunk, abs_divisor);
quotient.value += chunk_quotient.value;
}
}
}
strip_leading_zeroes(quotient.value);
if (this->sign == num.sign)
quotient.sign = '+';
else
quotient.sign = '-';
return quotient;
}
/*
BigInt % BigInt
---------------
Computes the modulo (remainder on division) of two BigInts.
The operand on the RHS of the modulo (the divisor) is `num`.
*/
BigInt BigInt::operator%(const BigInt& num) const {
BigInt abs_dividend = abs(*this);
BigInt abs_divisor = abs(num);
if (abs_divisor == 0)
throw std::logic_error("Attempted division by zero");
if (abs_divisor == 1 or abs_divisor == abs_dividend)
return BigInt(0);
BigInt remainder;
if (abs_dividend <= LLONG_MAX and abs_divisor <= LLONG_MAX)
remainder = std::stoll(abs_dividend.value) % std::stoll(abs_divisor.value);
else if (abs_dividend < abs_divisor)
remainder = abs_dividend;
else if (is_power_of_10(num.value)){ // if num is a power of 10 use optimised calculation
size_t no_of_zeroes = num.value.size() - 1;
remainder.value = abs_dividend.value.substr(abs_dividend.value.size() - no_of_zeroes);
}
else {
BigInt quotient = abs_dividend / abs_divisor;
remainder = abs_dividend - quotient * abs_divisor;
}
strip_leading_zeroes(remainder.value);
// remainder has the same sign as that of the dividend
remainder.sign = this->sign;
if (remainder.value == "0") // except if its zero
remainder.sign = '+';
return remainder;
}
/*
BigInt + Integer
----------------
*/
BigInt BigInt::operator+(const long long& num) const {
return *this + BigInt(num);
}
/*
Integer + BigInt
----------------
*/
BigInt operator+(const long long& lhs, const BigInt& rhs) {
return BigInt(lhs) + rhs;
}
/*
BigInt - Integer
----------------
*/
BigInt BigInt::operator-(const long long& num) const {
return *this - BigInt(num);
}
/*
Integer - BigInt
----------------
*/
BigInt operator-(const long long& lhs, const BigInt& rhs) {
return BigInt(lhs) - rhs;
}
/*
BigInt * Integer
----------------
*/
BigInt BigInt::operator*(const long long& num) const {
return *this * BigInt(num);
}
/*
Integer * BigInt
----------------
*/
BigInt operator*(const long long& lhs, const BigInt& rhs) {
return BigInt(lhs) * rhs;
}
/*
BigInt / Integer
----------------
*/
BigInt BigInt::operator/(const long long& num) const {
return *this / BigInt(num);
}
/*
Integer / BigInt
----------------
*/
BigInt operator/(const long long& lhs, const BigInt& rhs) {
return BigInt(lhs) / rhs;
}
/*
BigInt % Integer
----------------
*/
BigInt BigInt::operator%(const long long& num) const {
return *this % BigInt(num);
}
/*
Integer % BigInt
----------------
*/
BigInt operator%(const long long& lhs, const BigInt& rhs) {
return BigInt(lhs) % rhs;
}
/*
BigInt + String
---------------
*/
BigInt BigInt::operator+(const std::string& num) const {
return *this + BigInt(num);
}
/*
String + BigInt
---------------
*/
BigInt operator+(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) + rhs;
}
/*
BigInt - String
---------------
*/
BigInt BigInt::operator-(const std::string& num) const {
return *this - BigInt(num);
}
/*
String - BigInt
---------------
*/
BigInt operator-(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) - rhs;
}
/*
BigInt * String
---------------
*/
BigInt BigInt::operator*(const std::string& num) const {
return *this * BigInt(num);
}
/*
String * BigInt
---------------
*/
BigInt operator*(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) * rhs;
}
/*
BigInt / String
---------------
*/
BigInt BigInt::operator/(const std::string& num) const {
return *this / BigInt(num);
}
/*
String / BigInt
---------------
*/
BigInt operator/(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) / rhs;
}
/*
BigInt % String
---------------
*/
BigInt BigInt::operator%(const std::string& num) const {
return *this % BigInt(num);
}
/*
String % BigInt
---------------
*/
BigInt operator%(const std::string& lhs, const BigInt& rhs) {
return BigInt(lhs) % rhs;
}
#endif // BIG_INT_BINARY_ARITHMETIC_OPERATORS_HPP