biginteger.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // biginteger.h
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #pragma once
  8. #include <string>
  9. #include <vector>
  10. #include "number_format.h"
  11. namespace math {
  12. class biginteger {
  13. private:
  14. template <typename Int>
  15. using is_signed_t =
  16. typename std::enable_if<std::numeric_limits<Int>::is_integer &&
  17. std::numeric_limits<Int>::is_signed,
  18. int>::type;
  19. template <typename Int>
  20. using is_unsigned_t =
  21. typename std::enable_if<std::numeric_limits<Int>::is_integer &&
  22. !std::numeric_limits<Int>::is_signed,
  23. int>::type;
  24. public:
  25. using data_type = std::vector<int32_t>;
  26. static biginteger const ZERO, ONE, NEGATIVE_ONE;
  27. static constexpr int32_t const MAX_SEG{999999999};
  28. static constexpr int32_t const OVER_SEG{1000000000};
  29. static constexpr int32_t const SEG_DIGITS{9};
  30. public:
  31. // Constructors
  32. biginteger();
  33. template <typename Int>
  34. biginteger(Int value, is_signed_t<Int> = 0)
  35. : biginteger(value < 0,
  36. static_cast<uint64_t>(value < 0 ? -value : value)) {}
  37. template <typename Int>
  38. biginteger(Int value, is_unsigned_t<Int> = 0)
  39. : biginteger(false, static_cast<uint64_t>(value)) {}
  40. biginteger(char const *);
  41. // Unary operators
  42. biginteger operator-() const;
  43. // Binary operators
  44. friend biginteger operator+(biginteger, biginteger const &);
  45. friend biginteger operator-(biginteger, biginteger const &);
  46. friend biginteger operator*(biginteger, biginteger const &);
  47. friend biginteger operator/(biginteger, biginteger const &);
  48. friend biginteger operator%(biginteger, biginteger const &);
  49. friend biginteger & operator+=(biginteger &, biginteger const &);
  50. friend biginteger & operator-=(biginteger &, biginteger const &);
  51. friend biginteger & operator*=(biginteger &, biginteger const &);
  52. friend biginteger & operator/=(biginteger &, biginteger const &);
  53. // Output
  54. std::string to_string(number_format fmt = {}) const;
  55. // Comparison
  56. friend bool operator==(biginteger const &, biginteger const &);
  57. friend bool operator!=(biginteger const &, biginteger const &);
  58. friend bool operator<=(biginteger const &, biginteger const &);
  59. friend bool operator<(biginteger const &, biginteger const &);
  60. friend bool operator>=(biginteger const &, biginteger const &);
  61. friend bool operator>(biginteger const &, biginteger const &);
  62. private:
  63. biginteger(bool, uint64_t);
  64. void subtract_impl(biginteger const & lhs, bool is_sub);
  65. friend void swap(biginteger & rhs, biginteger & lhs) {
  66. using std::swap;
  67. swap(rhs.is_negative, lhs.is_negative);
  68. swap(rhs.data, lhs.data);
  69. }
  70. bool is_negative;
  71. data_type data{};
  72. };
  73. }