biginteger.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. namespace math {
  11. class biginteger {
  12. public:
  13. using data_type = std::vector<int32_t>;
  14. static biginteger const ZERO, ONE, NEGATIVE_ONE;
  15. static constexpr int32_t const MAX_SEG { 999999999};
  16. static constexpr int32_t const OVER_SEG {1000000000};
  17. static constexpr int32_t const SEG_DIGITS{ 9};
  18. public:
  19. // Constructors
  20. biginteger();
  21. biginteger(int32_t);
  22. biginteger(uint32_t);
  23. biginteger(int64_t);
  24. biginteger(uint64_t);
  25. biginteger(char const *);
  26. // Unary operators
  27. biginteger operator-() const;
  28. // Binary operators
  29. friend biginteger operator+(biginteger const &, biginteger const &);
  30. friend biginteger operator-(biginteger const &, biginteger const &);
  31. friend biginteger operator*(biginteger const &, biginteger const &);
  32. friend biginteger operator/(biginteger const &, biginteger const &);
  33. friend biginteger & operator+=(biginteger &, biginteger const &);
  34. friend biginteger & operator-=(biginteger &, biginteger const &);
  35. friend biginteger & operator*=(biginteger &, biginteger const &);
  36. friend biginteger & operator/=(biginteger &, biginteger const &);
  37. // Output
  38. std::string to_string() const;
  39. // Comparison
  40. friend bool operator==(biginteger const &, biginteger const &);
  41. friend bool operator!=(biginteger const &, biginteger const &);
  42. friend bool operator<=(biginteger const &, biginteger const &);
  43. friend bool operator< (biginteger const &, biginteger const &);
  44. friend bool operator>=(biginteger const &, biginteger const &);
  45. friend bool operator> (biginteger const &, biginteger const &);
  46. private:
  47. biginteger(bool, uint64_t);
  48. biginteger(bool, data_type &&);
  49. friend void swap(biginteger & rhs, biginteger & lhs) {
  50. using std::swap;
  51. swap(rhs.is_negative, lhs.is_negative);
  52. swap(rhs.data, lhs.data);
  53. }
  54. bool is_negative;
  55. data_type data{};
  56. };
  57. }