| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // biginteger.h
- // bigdecimal
- //
- // Created by Sam Jaffe on 6/30/17.
- //
- #pragma once
- #include <string>
- #include <vector>
- #include "number_format.h"
- namespace math {
- class biginteger {
- private:
- template <typename Int>
- using is_signed_t =
- typename std::enable_if<std::numeric_limits<Int>::is_integer &&
- std::numeric_limits<Int>::is_signed,
- int>::type;
- template <typename Int>
- using is_unsigned_t =
- typename std::enable_if<std::numeric_limits<Int>::is_integer &&
- !std::numeric_limits<Int>::is_signed,
- int>::type;
- public:
- using data_type = std::vector<int32_t>;
- static biginteger const ZERO, ONE, NEGATIVE_ONE;
- static constexpr int32_t const MAX_SEG{999999999};
- static constexpr int32_t const OVER_SEG{1000000000};
- static constexpr int32_t const SEG_DIGITS{9};
- public:
- // Constructors
- biginteger();
- template <typename Int>
- biginteger(Int value, is_signed_t<Int> = 0)
- : biginteger(value < 0,
- static_cast<uint64_t>(value < 0 ? -value : value)) {}
- template <typename Int>
- biginteger(Int value, is_unsigned_t<Int> = 0)
- : biginteger(false, static_cast<uint64_t>(value)) {}
- biginteger(char const *);
- // Unary operators
- biginteger operator-() const;
- // Binary operators
- friend biginteger operator+(biginteger, biginteger const &);
- friend biginteger operator-(biginteger, biginteger const &);
- friend biginteger operator*(biginteger, biginteger const &);
- friend biginteger operator/(biginteger, biginteger const &);
- friend biginteger operator%(biginteger, biginteger const &);
- friend biginteger & operator+=(biginteger &, biginteger const &);
- friend biginteger & operator-=(biginteger &, biginteger const &);
- friend biginteger & operator*=(biginteger &, biginteger const &);
- friend biginteger & operator/=(biginteger &, biginteger const &);
- // Output
- std::string to_string(number_format const & fmt = default_fmt) const;
- // Comparison
- friend bool operator==(biginteger const &, biginteger const &);
- friend bool operator!=(biginteger const &, biginteger const &);
- friend bool operator<=(biginteger const &, biginteger const &);
- friend bool operator<(biginteger const &, biginteger const &);
- friend bool operator>=(biginteger const &, biginteger const &);
- friend bool operator>(biginteger const &, biginteger const &);
- private:
- biginteger(bool, uint64_t);
- void subtract_impl(biginteger const & lhs, bool is_sub);
- friend void swap(biginteger & rhs, biginteger & lhs) {
- using std::swap;
- swap(rhs.is_negative, lhs.is_negative);
- swap(rhs.data, lhs.data);
- }
- bool is_negative;
- data_type data{};
- };
- }
|