biginteger.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. void add(data_type & rhs, data_type const & lhs);
  18. void subtract_nounderflow(data_type & rhs, data_type const & lhs);
  19. void multiply(data_type & 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 & math::operator+=(biginteger & rhs, biginteger const & lhs) {
  68. if (lhs == biginteger::ZERO) { return rhs; }
  69. else if (rhs == biginteger::ZERO) { rhs=lhs; }
  70. else if (rhs.is_negative == lhs.is_negative) {
  71. detail::add(rhs.data, lhs.data);
  72. } else {
  73. auto cmp = detail::compare(rhs.data, lhs.data);
  74. if (cmp == 0) {
  75. rhs = biginteger::ZERO;
  76. } else if (cmp > 0) {
  77. detail::subtract_nounderflow(rhs.data, lhs.data);
  78. } else {
  79. biginteger tmp{lhs};
  80. swap(rhs, tmp);
  81. detail::subtract_nounderflow(rhs.data, tmp.data);
  82. }
  83. }
  84. return rhs;
  85. }
  86. biginteger & math::operator-=(biginteger & rhs, biginteger const & lhs) {
  87. if (lhs == biginteger::ZERO) { return rhs; }
  88. else if (rhs == biginteger::ZERO) { rhs = -lhs; }
  89. if (rhs.is_negative != lhs.is_negative) {
  90. detail::add(rhs.data, lhs.data);
  91. } else {
  92. auto cmp = detail::compare(rhs.data, lhs.data);
  93. if (cmp == 0) {
  94. rhs = biginteger::ZERO;
  95. } else if (cmp > 0) {
  96. detail::subtract_nounderflow(rhs.data, lhs.data);
  97. } else {
  98. biginteger tmp{-lhs};
  99. swap(rhs, tmp);
  100. detail::subtract_nounderflow(rhs.data, tmp.data);;
  101. }
  102. }
  103. return rhs;
  104. }
  105. biginteger & math::operator*=(biginteger & rhs, biginteger const & lhs) {
  106. bool is_neg = rhs.is_negative != lhs.is_negative;
  107. if (rhs == biginteger::ZERO || lhs == biginteger::ZERO) {
  108. rhs = biginteger::ZERO;
  109. } else if (detail::compare(lhs.data, biginteger::ONE.data) == 0) {
  110. rhs.is_negative = is_neg;
  111. } else if (detail::compare(rhs.data, biginteger::ONE.data) == 0) {
  112. rhs = lhs;
  113. rhs.is_negative = is_neg;
  114. } else {
  115. detail::multiply(rhs.data, lhs.data);
  116. }
  117. return rhs;
  118. }
  119. biginteger & math::operator/=(biginteger & rhs, biginteger const & lhs) {
  120. swap(rhs, rhs / lhs);
  121. return rhs;
  122. }
  123. biginteger biginteger::operator-() const {
  124. return (*this) * NEGATIVE_ONE;
  125. }
  126. biginteger math::operator+(biginteger rhs, biginteger const & lhs) {
  127. return rhs += lhs;
  128. }
  129. biginteger math::operator-(biginteger rhs, biginteger const & lhs) {
  130. return rhs -= lhs;
  131. }
  132. biginteger math::operator*(biginteger rhs, biginteger const & lhs) {
  133. return rhs *= lhs;
  134. }
  135. biginteger math::operator/(biginteger const & rhs, biginteger const & lhs) {
  136. if (lhs == biginteger::ZERO) { throw std::domain_error("cannot divide by 0"); }
  137. else if (rhs == biginteger::ZERO) { return biginteger::ZERO; }
  138. else if (detail::compare(lhs.data, {1}) == 0) {
  139. return {rhs.is_negative != lhs.is_negative, biginteger::data_type{rhs.data}};
  140. } else {
  141. auto cmp = detail::compare(rhs.data, lhs.data);
  142. bool is_neg = rhs.is_negative != lhs.is_negative;
  143. if (cmp < 0) { return biginteger::ZERO; }
  144. else if (cmp == 0) { return {is_neg, {1}}; }
  145. else { return {is_neg, detail::divide(rhs.data, lhs.data).first}; }
  146. }
  147. }
  148. biginteger math::operator%(biginteger const & rhs, biginteger const & lhs) {
  149. if (lhs == biginteger::ZERO) { throw std::domain_error("cannot divide by 0"); }
  150. else if (detail::compare(lhs.data, biginteger::ONE.data) == 0 ||
  151. rhs == biginteger::ZERO) { return biginteger::ZERO; }
  152. else {
  153. auto cmp = detail::compare(rhs.data, lhs.data);
  154. if (cmp < 0) { return rhs; }
  155. else if (cmp == 0) { return biginteger::ZERO; }
  156. else {
  157. auto data = detail::divide(rhs.data, lhs.data).second;
  158. if (detail::compare(data, {0}) == 0) { return biginteger::ZERO; }
  159. else if (rhs.is_negative != lhs.is_negative) {
  160. auto tmp = lhs.data;
  161. swap(data, tmp);
  162. detail::subtract_nounderflow(data, tmp);
  163. }
  164. return {lhs.is_negative, std::move(data)};
  165. }
  166. }
  167. }
  168. namespace detail {
  169. #define IMPL_COMPARE(expr) \
  170. if (rhs expr < lhs expr) return -1; \
  171. else if (lhs expr < rhs expr) return 1
  172. int compare(data_type const & rhs, data_type const & lhs) {
  173. IMPL_COMPARE(.size());
  174. for (size_t i = rhs.size(); i > 0; --i) {
  175. IMPL_COMPARE([i-1]);
  176. }
  177. return 0;
  178. }
  179. #undef IMPL_COMPARE
  180. void add(data_type & rhs, data_type const & lhs) {
  181. rhs.resize(std::max(rhs.size(), lhs.size())+1);
  182. auto const lbnd = lhs.size(), ubnd = rhs.size() - 1;
  183. // Add
  184. for (size_t i = 0; i < lbnd; ++i) {
  185. rhs[i] += lhs[i];
  186. }
  187. // Carry
  188. for (size_t i = 0; i < ubnd; ++i) {
  189. if (rhs[i] > biginteger::MAX_SEG) {
  190. rhs[i] -= biginteger::OVER_SEG;
  191. rhs[i+1] += 1;
  192. }
  193. }
  194. if (rhs[ubnd] == 0) { rhs.pop_back(); }
  195. }
  196. void subtract_nounderflow(data_type & rhs, data_type const & lhs) {
  197. size_t const rbnd = rhs.size(), lbnd = lhs.size();
  198. // Subtract
  199. for (size_t i = 0; i < lbnd; ++i) {
  200. rhs[i] -= lhs[i];
  201. }
  202. // Borrow
  203. for (size_t i = 0; i < rbnd; ++i) {
  204. if (rhs[i] < 0) {
  205. rhs[i] += biginteger::OVER_SEG;
  206. rhs[i+1] -= 1;
  207. }
  208. }
  209. if (rhs[rbnd-1] == 0 && rbnd > 1) { rhs.pop_back(); }
  210. }
  211. void multiply(data_type & rhs, data_type const & lhs) {
  212. size_t const rbnd = rhs.size(), lbnd = lhs.size();
  213. size_t const ubnd = rbnd + lbnd;
  214. rhs.resize(ubnd);
  215. // Multiply
  216. for (size_t i = rbnd; i > 0; --i) {
  217. int32_t const value = rhs[i-1];
  218. for (size_t j = lbnd; j > 0; --j) {
  219. // Max input 999,999,999
  220. // Max output 999,999,998,000,000,001
  221. int64_t product = static_cast<int64_t>(value) * static_cast<int64_t>(lhs[j-1]);
  222. int64_t overflow = product / biginteger::OVER_SEG;
  223. rhs[i+j-2] += static_cast<int32_t>(product - (overflow * biginteger::OVER_SEG));
  224. rhs[i+j-1] += static_cast<int32_t>(overflow);
  225. }
  226. rhs[i-1] -= value;
  227. }
  228. // Carry
  229. for (size_t i = 0; i < ubnd-1; ++i) {
  230. if (rhs[i] > biginteger::MAX_SEG) {
  231. int32_t overflow = rhs[i] / biginteger::OVER_SEG;
  232. rhs[i] -= (overflow * biginteger::OVER_SEG);
  233. rhs[i+1] += overflow;
  234. }
  235. }
  236. while (rhs.back() == 0) { rhs.pop_back(); }
  237. }
  238. data_type shift10(data_type const & data, int32_t pow, size_t shift) {
  239. size_t const bnd = data.size();
  240. data_type rval(bnd + shift + 1);
  241. for (size_t i = 0; i < bnd; ++i) {
  242. int64_t product = static_cast<int64_t>(data[i]) * static_cast<int64_t>(pow);
  243. int64_t overflow = product / biginteger::OVER_SEG;
  244. rval[i+shift] += static_cast<int32_t>(product - (overflow * biginteger::OVER_SEG));
  245. rval[i+shift+1] += static_cast<int32_t>(overflow);
  246. }
  247. if (rval.back() == 0 && rval.size() > 1) { rval.pop_back(); }
  248. return rval;
  249. }
  250. size_t digits(int32_t val) {
  251. return val == 0 ? 1 : static_cast<size_t>(floor(log10(val))) + 1;
  252. }
  253. size_t digits(data_type const & data) {
  254. return biginteger::SEG_DIGITS * (data.size()-1) + digits(data.back());
  255. }
  256. int32_t powers[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
  257. std::pair<data_type, data_type> divide(data_type remainder, data_type const & divisor) {
  258. data_type accum{0};
  259. auto const dig = digits(divisor);
  260. do {
  261. auto const diff = std::max(1UL, digits(remainder) - dig) - 1;
  262. auto const shift = diff / biginteger::SEG_DIGITS;
  263. auto const ipow = diff - (shift * biginteger::SEG_DIGITS);
  264. data_type step{shift10({1}, powers[ipow], shift)};
  265. data_type value{shift10(divisor, powers[ipow], shift)};
  266. do {
  267. subtract_nounderflow(remainder, value);
  268. add(accum, step);
  269. } while (detail::compare(remainder, value) >= 0);
  270. } while (detail::compare(remainder, divisor) >= 0);
  271. return { std::move(accum), std::move(remainder) };
  272. }
  273. }
  274. bool math::operator==(biginteger const & rhs, biginteger const & lhs) {
  275. return rhs.is_negative == lhs.is_negative && detail::compare(rhs.data, lhs.data) == 0;
  276. }
  277. bool math::operator!=(biginteger const & rhs, biginteger const & lhs) {
  278. return !(rhs == lhs);
  279. }
  280. bool math::operator<=(biginteger const & rhs, biginteger const & lhs) {
  281. return !(rhs > lhs);
  282. }
  283. bool math::operator< (biginteger const & rhs, biginteger const & lhs) {
  284. if (rhs.is_negative != lhs.is_negative) { return rhs.is_negative; }
  285. else if (rhs.is_negative) { return detail::compare(rhs.data, lhs.data) > 0; }
  286. else { return detail::compare(rhs.data, lhs.data) < 0; }
  287. }
  288. bool math::operator>=(biginteger const & rhs, biginteger const & lhs) {
  289. return !(rhs < lhs);
  290. }
  291. bool math::operator> (biginteger const & rhs, biginteger const & lhs) {
  292. return lhs < rhs;
  293. }
  294. biginteger const biginteger::ZERO{0}, biginteger::ONE{1}, biginteger::NEGATIVE_ONE{-1};
  295. std::string biginteger::to_string() const {
  296. std::vector<char> output(biginteger::SEG_DIGITS * data.size() + 1, '\0');
  297. std::ptrdiff_t idx = 0;
  298. if (is_negative) { output[0] = '-'; ++idx; }
  299. idx += sprintf(output.data() + idx, "%d", data[data.size()-1]);
  300. for (size_t i = data.size()-1; i > 0; --i, idx+=SEG_DIGITS) {
  301. sprintf(output.data() + idx, "%0.*d", SEG_DIGITS, data[i-1]);
  302. }
  303. return output.data();
  304. }