biginteger.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // biginteger.cpp
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #include "biginteger.h"
  8. #include "bignum_helper.h"
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. using namespace math;
  13. static void read(int32_t & dst, char const * & str, size_t len) {
  14. char seg[biginteger::SEG_DIGITS+1] = "";
  15. strncpy(seg, str, len);
  16. dst = atoi(seg);
  17. str += len;
  18. }
  19. biginteger::biginteger()
  20. : is_negative(false), data({0}) {}
  21. biginteger::biginteger(char const * number)
  22. : is_negative(number[0] == '-') {
  23. if (is_negative) ++number;
  24. auto len = strlen(number);
  25. auto elems = len/SEG_DIGITS;
  26. data.resize(elems);
  27. if (auto small = len-(elems*SEG_DIGITS)) {
  28. read(*data.insert(data.end(), 0), number, small);
  29. }
  30. for (data_type::size_type idx = elems; idx > 0; --idx) {
  31. read(data[idx-1], number, SEG_DIGITS);
  32. }
  33. }
  34. biginteger::biginteger(bool neg, uint64_t value) :
  35. is_negative(neg) {
  36. uint64_t next{0};
  37. do {
  38. next = value / OVER_SEG;
  39. data.push_back(static_cast<int32_t>(value - (OVER_SEG * next)));
  40. } while ((value = next) > 0);
  41. }
  42. void biginteger::subtract_impl(biginteger const & lhs, bool is_sub) {
  43. auto cmp = detail::compare(data, lhs.data);
  44. if (cmp == 0) {
  45. *this = biginteger::ZERO;
  46. } else if (cmp > 0) {
  47. detail::subtract_nounderflow(data, lhs.data);
  48. } else {
  49. data_type tmp{lhs.data};
  50. is_negative = lhs.is_negative ^ is_sub;
  51. swap(data, tmp);
  52. detail::subtract_nounderflow(data, tmp);
  53. }
  54. }
  55. biginteger & math::operator+=(biginteger & rhs, biginteger const & lhs) {
  56. if (lhs == biginteger::ZERO) { return rhs; }
  57. else if (rhs == biginteger::ZERO) { rhs=lhs; }
  58. else if (rhs.is_negative == lhs.is_negative) {
  59. detail::add(rhs.data, lhs.data);
  60. } else {
  61. rhs.subtract_impl(lhs, false);
  62. }
  63. return rhs;
  64. }
  65. biginteger & math::operator-=(biginteger & rhs, biginteger const & lhs) {
  66. if (lhs == biginteger::ZERO) { return rhs; }
  67. else if (rhs == biginteger::ZERO) { rhs = -lhs; }
  68. else if (rhs.is_negative != lhs.is_negative) {
  69. detail::add(rhs.data, lhs.data);
  70. } else {
  71. rhs.subtract_impl(lhs, true);
  72. }
  73. return rhs;
  74. }
  75. biginteger & math::operator*=(biginteger & rhs, biginteger const & lhs) {
  76. bool is_neg = rhs.is_negative != lhs.is_negative;
  77. if (rhs == biginteger::ZERO || lhs == biginteger::ZERO) {
  78. rhs = biginteger::ZERO;
  79. } else if (detail::compare(lhs.data, biginteger::ONE.data) == 0) {
  80. rhs.is_negative = is_neg;
  81. } else if (detail::compare(rhs.data, biginteger::ONE.data) == 0) {
  82. rhs = lhs;
  83. rhs.is_negative = is_neg;
  84. } else {
  85. detail::multiply(rhs.data, lhs.data);
  86. }
  87. return rhs;
  88. }
  89. biginteger & math::operator/=(biginteger & rhs, biginteger const & lhs) {
  90. rhs.is_negative ^= lhs.is_negative;
  91. if (lhs == biginteger::ZERO) { throw std::domain_error("cannot divide by 0"); }
  92. else if (rhs == biginteger::ZERO) { rhs = biginteger::ZERO; }
  93. else if (detail::compare(lhs.data, biginteger::ONE.data) != 0) {
  94. auto cmp = detail::compare(rhs.data, lhs.data);
  95. if (cmp < 0) { rhs = biginteger::ZERO; }
  96. else if (cmp == 0) { rhs.data = {1}; }
  97. else { rhs.data = detail::divide(rhs.data, lhs.data); }
  98. }
  99. return rhs;
  100. }
  101. biginteger biginteger::operator-() const {
  102. return (*this) * NEGATIVE_ONE;
  103. }
  104. biginteger math::operator+(biginteger rhs, biginteger const & lhs) {
  105. return rhs += lhs;
  106. }
  107. biginteger math::operator-(biginteger rhs, biginteger const & lhs) {
  108. return rhs -= lhs;
  109. }
  110. biginteger math::operator*(biginteger rhs, biginteger const & lhs) {
  111. return rhs *= lhs;
  112. }
  113. biginteger math::operator/(biginteger rhs, biginteger const & lhs) {
  114. return rhs /= lhs;
  115. }
  116. biginteger math::operator%(biginteger rhs, biginteger const & lhs) {
  117. if (lhs == biginteger::ZERO) { throw std::domain_error("cannot divide by 0"); }
  118. else if (lhs == biginteger::ONE ||
  119. rhs == biginteger::ZERO) { return biginteger::ZERO; }
  120. else {
  121. auto cmp = detail::compare(rhs.data, lhs.data);
  122. if (cmp == 0) { return biginteger::ZERO; }
  123. else if (cmp > 0) {
  124. detail::divide(rhs.data, lhs.data);
  125. if (detail::compare(rhs.data, biginteger::ZERO.data) == 0) { return biginteger::ZERO; }
  126. }
  127. if (rhs.is_negative != lhs.is_negative) {
  128. rhs.is_negative = lhs.is_negative;
  129. auto tmp = lhs.data;
  130. swap(rhs.data, tmp);
  131. detail::subtract_nounderflow(rhs.data, tmp);
  132. }
  133. return rhs;
  134. }
  135. }
  136. bool math::operator==(biginteger const & rhs, biginteger const & lhs) {
  137. return rhs.is_negative == lhs.is_negative && detail::compare(rhs.data, lhs.data) == 0;
  138. }
  139. bool math::operator!=(biginteger const & rhs, biginteger const & lhs) {
  140. return !(rhs == lhs);
  141. }
  142. bool math::operator<=(biginteger const & rhs, biginteger const & lhs) {
  143. return !(rhs > lhs);
  144. }
  145. bool math::operator< (biginteger const & rhs, biginteger const & lhs) {
  146. if (rhs.is_negative != lhs.is_negative) { return rhs.is_negative; }
  147. else if (rhs.is_negative) { return detail::compare(rhs.data, lhs.data) > 0; }
  148. else { return detail::compare(rhs.data, lhs.data) < 0; }
  149. }
  150. bool math::operator>=(biginteger const & rhs, biginteger const & lhs) {
  151. return !(rhs < lhs);
  152. }
  153. bool math::operator> (biginteger const & rhs, biginteger const & lhs) {
  154. return lhs < rhs;
  155. }
  156. biginteger const biginteger::ZERO{0}, biginteger::ONE{1}, biginteger::NEGATIVE_ONE{-1};
  157. std::string biginteger::to_string() const {
  158. std::vector<char> output(biginteger::SEG_DIGITS * data.size() + 2, '\0');
  159. std::ptrdiff_t idx = 0;
  160. if (is_negative) { output[0] = '-'; ++idx; }
  161. idx += sprintf(output.data() + idx, "%d", data[data.size()-1]);
  162. for (size_t i = data.size()-1; i > 0; --i, idx+=SEG_DIGITS) {
  163. sprintf(output.data() + idx, "%0.*d", SEG_DIGITS, data[i-1]);
  164. }
  165. return output.data();
  166. }