biginteger.cpp 5.7 KB

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