biginteger.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // biginteger.cpp
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #include "biginteger.h"
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. using namespace math;
  13. namespace detail {
  14. using data_type = biginteger::data_type;
  15. // 1 => GREATER, 0 => EQUAL, -1 => LESS
  16. int compare(data_type const & rhs, data_type const & lhs);
  17. data_type add(data_type rhs, data_type const & lhs);
  18. data_type subtract_nounderflow(data_type rhs, data_type const & lhs);
  19. data_type multiply(data_type const & rhs, data_type const & lhs);
  20. std::pair<data_type, data_type> divide(data_type remainder, data_type const & divisor);
  21. }
  22. static void swap(biginteger & rhs, biginteger && lhs) {
  23. swap(rhs, lhs);
  24. }
  25. biginteger::biginteger()
  26. : biginteger(false, 0) {}
  27. biginteger::biginteger(int32_t value)
  28. : biginteger(static_cast<int64_t>(value)) {}
  29. biginteger::biginteger(uint32_t value)
  30. : biginteger(static_cast<uint64_t>(value)) {}
  31. biginteger::biginteger(int64_t value) :
  32. biginteger(value < 0, static_cast<uint64_t>(value < 0 ? -value : value)) {}
  33. biginteger::biginteger(uint64_t value)
  34. : biginteger(false, value) {}
  35. biginteger::biginteger(char const * number)
  36. : is_negative(number[0] == '-') {
  37. // 999,999,996,000,000,005,999,999,996,000,000,001
  38. if (is_negative) ++number;
  39. auto len = strlen(number);
  40. auto elems = len/SEG_DIGITS;
  41. data.resize(elems);
  42. {
  43. auto small = len-(elems*SEG_DIGITS);
  44. if (small > 0) {
  45. char seg[SEG_DIGITS+1] = "";
  46. strncpy(seg, number, small);
  47. data.push_back(atoi(seg));
  48. number += small;
  49. }
  50. }
  51. for (data_type::size_type idx = elems; idx > 0; --idx, number += SEG_DIGITS) {
  52. char seg[SEG_DIGITS+1] = "";
  53. strncpy(seg, number, SEG_DIGITS);
  54. data[idx-1] = atoi(seg);
  55. }
  56. }
  57. biginteger::biginteger(bool neg, uint64_t value) :
  58. is_negative(neg) {
  59. uint64_t next{0};
  60. do {
  61. next = value / OVER_SEG;
  62. data.push_back(static_cast<int32_t>(value - (OVER_SEG * next)));
  63. } while ((value = next) > 0);
  64. }
  65. biginteger::biginteger(bool neg, data_type && dt) :
  66. is_negative(neg), data(std::move(dt)) {}
  67. biginteger & operator+=(biginteger & rhs, biginteger const & lhs) {
  68. swap(rhs, rhs + lhs);
  69. return rhs;
  70. }
  71. biginteger & operator-=(biginteger & rhs, biginteger const & lhs) {
  72. swap(rhs, rhs - lhs);
  73. return rhs;
  74. }
  75. biginteger & operator*=(biginteger & rhs, biginteger const & lhs) {
  76. swap(rhs, rhs * lhs);
  77. return rhs;
  78. }
  79. biginteger & operator/=(biginteger & rhs, biginteger const & lhs) {
  80. swap(rhs, rhs / lhs);
  81. return rhs;
  82. }
  83. biginteger biginteger::operator-() const {
  84. return (*this) * NEGATIVE_ONE;
  85. }
  86. biginteger math::operator+(biginteger const & rhs, biginteger const & lhs) {
  87. if (lhs == biginteger::ZERO) { return rhs; }
  88. else if (rhs == biginteger::ZERO) { return lhs; }
  89. if (rhs.is_negative == lhs.is_negative) {
  90. return {rhs.is_negative, detail::add(rhs.data, lhs.data)};
  91. } else {
  92. auto cmp = detail::compare(rhs.data, lhs.data);
  93. if (cmp == 0) {
  94. return biginteger::ZERO;
  95. } else if (cmp > 0) {
  96. return {rhs.is_negative, detail::subtract_nounderflow(rhs.data, lhs.data)};
  97. } else {
  98. return {lhs.is_negative, detail::subtract_nounderflow(lhs.data, rhs.data)};
  99. }
  100. }
  101. }
  102. biginteger math::operator-(biginteger const & rhs, biginteger const & lhs) {
  103. if (lhs == biginteger::ZERO) { return rhs; }
  104. else if (rhs == biginteger::ZERO) { return -lhs; }
  105. if (rhs.is_negative != lhs.is_negative) {
  106. return {rhs.is_negative, detail::add(rhs.data, lhs.data)};
  107. } else {
  108. auto cmp = detail::compare(rhs.data, lhs.data);
  109. if (cmp == 0) {
  110. return biginteger::ZERO;
  111. } else if (cmp > 0) {
  112. return {rhs.is_negative, detail::subtract_nounderflow(rhs.data, lhs.data)};
  113. } else {
  114. return {!lhs.is_negative, detail::subtract_nounderflow(lhs.data, rhs.data)};
  115. }
  116. }
  117. }
  118. biginteger math::operator*(biginteger const & rhs, biginteger const & lhs) {
  119. if (rhs == biginteger::ZERO || lhs == biginteger::ZERO) {
  120. return biginteger::ZERO;
  121. }
  122. return {rhs.is_negative != lhs.is_negative, detail::multiply(rhs.data, lhs.data)};
  123. }
  124. biginteger math::operator/(biginteger const & rhs, biginteger const & lhs) {
  125. if (lhs == biginteger::ZERO) { throw std::domain_error("cannot divide by 0"); }
  126. else if (rhs == biginteger::ZERO) { return biginteger::ZERO; }
  127. else if (detail::compare(lhs.data, {1}) == 0) {
  128. return {rhs.is_negative != lhs.is_negative, biginteger::data_type{rhs.data}};
  129. } else {
  130. auto cmp = detail::compare(rhs.data, lhs.data);
  131. bool is_neg = rhs.is_negative != lhs.is_negative;
  132. if (cmp < 0) { return biginteger::ZERO; }
  133. else if (cmp == 0) { return {is_neg, {1}}; }
  134. else { return {is_neg, detail::divide(rhs.data, lhs.data)}; }
  135. }
  136. }
  137. namespace detail {
  138. #define IMPL_COMPARE(expr) \
  139. if (rhs expr < lhs expr) return -1; \
  140. else if (lhs expr < rhs expr) return 1
  141. int compare(data_type const & rhs, data_type const & lhs) {
  142. IMPL_COMPARE(.size());
  143. for (size_t i = rhs.size(); i > 0; --i) {
  144. IMPL_COMPARE([i-1]);
  145. }
  146. return 0;
  147. }
  148. #undef IMPL_COMPARE
  149. void add_into(data_type & rhs, data_type const & lhs) {
  150. rhs.resize(std::max(rhs.size(), lhs.size())+1);
  151. auto const lbnd = lhs.size(), ubnd = rhs.size() - 1;
  152. // Add
  153. for (size_t i = 0; i < lbnd; ++i) {
  154. rhs[i] += lhs[i];
  155. }
  156. // Carry
  157. for (size_t i = 0; i < ubnd; ++i) {
  158. if (rhs[i] > biginteger::MAX_SEG) {
  159. rhs[i] -= biginteger::OVER_SEG;
  160. rhs[i+1] += 1;
  161. }
  162. }
  163. if (rhs[ubnd] == 0) { rhs.pop_back(); }
  164. }
  165. data_type add(data_type rhs, data_type const & lhs) {
  166. add_into(rhs, lhs);
  167. return rhs;
  168. }
  169. void subtract_from(data_type & rhs, data_type const & lhs) {
  170. size_t const rbnd = rhs.size(), lbnd = lhs.size();
  171. // Subtract
  172. for (size_t i = 0; i < lbnd; ++i) {
  173. rhs[i] -= lhs[i];
  174. }
  175. // Borrow
  176. for (size_t i = 0; i < rbnd; ++i) {
  177. if (rhs[i] < 0) {
  178. rhs[i] += biginteger::OVER_SEG;
  179. rhs[i+1] -= 1;
  180. }
  181. }
  182. if (rhs[rbnd-1] == 0 && rbnd > 1) { rhs.pop_back(); }
  183. }
  184. data_type subtract_nounderflow(data_type rhs, data_type const & lhs) {
  185. subtract_from(rhs, lhs);
  186. return rhs;
  187. }
  188. data_type multiply(data_type const & rhs, data_type const & lhs) {
  189. if (compare(rhs, {1}) == 0) { return lhs; }
  190. else if (compare(lhs, {1}) == 0) { return rhs; }
  191. size_t const rbnd = rhs.size(), lbnd = lhs.size();
  192. size_t const ubnd = rbnd + lbnd;
  193. data_type rval(ubnd + 1);
  194. // Multiply
  195. for (size_t i = 0; i < rbnd; ++i) {
  196. for (size_t j = 0; j < lbnd; ++j) {
  197. // Max input 999,999,999
  198. // Max output 999,999,998,000,000,001
  199. int64_t product = static_cast<int64_t>(rhs[i]) * static_cast<int64_t>(lhs[j]);
  200. int64_t overflow = product / biginteger::OVER_SEG;
  201. rval[i+j] += static_cast<int32_t>(product - (overflow * biginteger::OVER_SEG));
  202. rval[i+j+1] += static_cast<int32_t>(overflow);
  203. }
  204. }
  205. // Carry
  206. for (size_t i = 0; i < ubnd; ++i) {
  207. if (rval[i] > biginteger::MAX_SEG) {
  208. int32_t overflow = rval[i] / biginteger::OVER_SEG;
  209. rval[i] -= (overflow * biginteger::OVER_SEG);
  210. rval[i+1] += overflow;
  211. }
  212. }
  213. while (rval.back() == 0 && rval.size() > 1) { rval.pop_back(); }
  214. return rval;
  215. }
  216. data_type shift10(data_type const & data, int32_t pow, size_t shift) {
  217. size_t const bnd = data.size();
  218. data_type rval(bnd + shift + 1);
  219. for (size_t i = 0; i < bnd; ++i) {
  220. int64_t product = static_cast<int64_t>(data[i]) * static_cast<int64_t>(pow);
  221. int64_t overflow = product / biginteger::OVER_SEG;
  222. rval[i+shift] += static_cast<int32_t>(product - (overflow * biginteger::OVER_SEG));
  223. rval[i+shift+1] += static_cast<int32_t>(overflow);
  224. }
  225. if (rval.back() == 0 && rval.size() > 1) { rval.pop_back(); }
  226. return rval;
  227. }
  228. size_t digits(int32_t val) {
  229. return val == 0 ? 1 : static_cast<size_t>(floor(log10(val))) + 1;
  230. }
  231. size_t digits(data_type const & data) {
  232. return biginteger::SEG_DIGITS * (data.size()-1) + digits(data.back());
  233. }
  234. int32_t powers[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
  235. data_type divide(data_type remainder, data_type const & divisor) {
  236. data_type accum{0};
  237. auto const dig = digits(divisor);
  238. do {
  239. auto const diff = digits(remainder) - dig - 1;
  240. auto const shift = diff / biginteger::SEG_DIGITS;
  241. auto const ipow = diff - (shift * biginteger::SEG_DIGITS);
  242. data_type step{shift10({1}, powers[ipow], shift)};
  243. data_type value{shift10(divisor, powers[ipow], shift)};
  244. do {
  245. subtract_from(remainder, value);
  246. add_into(accum, step);
  247. } while (detail::compare(remainder, value) >= 0);
  248. } while (detail::compare(remainder, divisor) >= 0);
  249. return accum;
  250. }
  251. }
  252. bool math::operator==(biginteger const & rhs, biginteger const & lhs) {
  253. return rhs.is_negative == lhs.is_negative && detail::compare(rhs.data, lhs.data) == 0;
  254. }
  255. bool math::operator!=(biginteger const & rhs, biginteger const & lhs) {
  256. return !(rhs == lhs);
  257. }
  258. bool math::operator<=(biginteger const & rhs, biginteger const & lhs) {
  259. return !(rhs > lhs);
  260. }
  261. bool math::operator< (biginteger const & rhs, biginteger const & lhs) {
  262. if (rhs.is_negative != lhs.is_negative) { return rhs.is_negative; }
  263. else if (rhs.is_negative) { return detail::compare(rhs.data, lhs.data) > 0; }
  264. else { return detail::compare(rhs.data, lhs.data) < 0; }
  265. }
  266. bool math::operator>=(biginteger const & rhs, biginteger const & lhs) {
  267. return !(rhs < lhs);
  268. }
  269. bool math::operator> (biginteger const & rhs, biginteger const & lhs) {
  270. return lhs < rhs;
  271. }
  272. biginteger const biginteger::ZERO{0}, biginteger::ONE{1}, biginteger::NEGATIVE_ONE{-1};
  273. std::string biginteger::to_string() const {
  274. std::vector<char> output(biginteger::SEG_DIGITS * data.size() + 1, '\0');
  275. std::ptrdiff_t idx = 0;
  276. if (is_negative) { output[0] = '-'; ++idx; }
  277. idx += sprintf(output.data() + idx, "%d", data[data.size()-1]);
  278. for (size_t i = data.size()-1; i > 0; --i, idx+=SEG_DIGITS) {
  279. sprintf(output.data() + idx, "%0.*d", SEG_DIGITS, data[i-1]);
  280. }
  281. return output.data();
  282. }