|
| 1 | +#include <sourcemeta/core/numeric_decimal.h> |
| 2 | + |
| 3 | +#include <cassert> // assert |
| 4 | +#include <limits> // std::numeric_limits |
| 5 | +#include <utility> // std::move |
| 6 | + |
| 7 | +#include <decimal.hh> // decimal::Decimal |
| 8 | +#include <mpdecimal.h> // MPD_* |
| 9 | + |
| 10 | +namespace sourcemeta::core { |
| 11 | + |
| 12 | +Decimal::Decimal(const char *const value) : data{value} {} |
| 13 | + |
| 14 | +Decimal::Decimal(const std::string &value) : data{value} {} |
| 15 | + |
| 16 | +Decimal::Decimal(const std::string_view value) : data{std::string{value}} {} |
| 17 | + |
| 18 | +auto Decimal::to_scientific_string() const -> std::string { |
| 19 | + return this->data.to_sci(true); |
| 20 | +} |
| 21 | + |
| 22 | +auto Decimal::to_string() const -> std::string { |
| 23 | + const std::string repr{this->data.repr()}; |
| 24 | + return repr.substr(9, repr.size() - 11); |
| 25 | +} |
| 26 | + |
| 27 | +auto Decimal::to_int64() const -> std::int64_t { return this->data.i64(); } |
| 28 | + |
| 29 | +auto Decimal::to_int32() const -> std::int32_t { return this->data.i32(); } |
| 30 | + |
| 31 | +auto Decimal::to_uint64() const -> std::uint64_t { return this->data.u64(); } |
| 32 | + |
| 33 | +auto Decimal::to_uint32() const -> std::uint32_t { return this->data.u32(); } |
| 34 | + |
| 35 | +auto Decimal::is_zero() const -> bool { return this->data.iszero(); } |
| 36 | + |
| 37 | +auto Decimal::is_integer() const -> bool { return this->data.isinteger(); } |
| 38 | + |
| 39 | +auto Decimal::is_finite() const -> bool { return this->data.isfinite(); } |
| 40 | + |
| 41 | +auto Decimal::is_int32() const -> bool { |
| 42 | + assert(this->data.isinteger()); |
| 43 | + return *this >= std::numeric_limits<std::int32_t>::min() && |
| 44 | + *this <= std::numeric_limits<std::int32_t>::max(); |
| 45 | +} |
| 46 | + |
| 47 | +auto Decimal::is_int64() const -> bool { |
| 48 | + assert(this->data.isinteger()); |
| 49 | + return *this >= std::numeric_limits<std::int64_t>::min() && |
| 50 | + *this <= std::numeric_limits<std::int64_t>::max(); |
| 51 | +} |
| 52 | + |
| 53 | +auto Decimal::is_uint32() const -> bool { |
| 54 | + assert(this->data.isinteger()); |
| 55 | + return *this >= 0 && *this <= std::numeric_limits<std::uint32_t>::max(); |
| 56 | +} |
| 57 | + |
| 58 | +auto Decimal::is_uint64() const -> bool { |
| 59 | + assert(this->data.isinteger()); |
| 60 | + return *this >= 0 && *this <= std::numeric_limits<std::uint64_t>::max(); |
| 61 | +} |
| 62 | + |
| 63 | +auto Decimal::operator+=(const Decimal &other) -> Decimal & { |
| 64 | + this->data += other.data; |
| 65 | + return *this; |
| 66 | +} |
| 67 | + |
| 68 | +auto Decimal::operator-=(const Decimal &other) -> Decimal & { |
| 69 | + this->data -= other.data; |
| 70 | + return *this; |
| 71 | +} |
| 72 | + |
| 73 | +auto Decimal::operator*=(const Decimal &other) -> Decimal & { |
| 74 | + this->data *= other.data; |
| 75 | + return *this; |
| 76 | +} |
| 77 | + |
| 78 | +auto Decimal::operator/=(const Decimal &other) -> Decimal & { |
| 79 | + this->data /= other.data; |
| 80 | + return *this; |
| 81 | +} |
| 82 | + |
| 83 | +auto Decimal::operator%=(const Decimal &other) -> Decimal & { |
| 84 | + this->data %= other.data; |
| 85 | + return *this; |
| 86 | +} |
| 87 | + |
| 88 | +auto Decimal::operator+(const Decimal &other) const -> Decimal { |
| 89 | + Decimal result{*this}; |
| 90 | + result.data = this->data + other.data; |
| 91 | + return result; |
| 92 | +} |
| 93 | + |
| 94 | +auto Decimal::operator-(const Decimal &other) const -> Decimal { |
| 95 | + Decimal result{*this}; |
| 96 | + result.data = this->data - other.data; |
| 97 | + return result; |
| 98 | +} |
| 99 | + |
| 100 | +auto Decimal::operator*(const Decimal &other) const -> Decimal { |
| 101 | + Decimal result{*this}; |
| 102 | + result.data = this->data * other.data; |
| 103 | + return result; |
| 104 | +} |
| 105 | + |
| 106 | +auto Decimal::operator/(const Decimal &other) const -> Decimal { |
| 107 | + Decimal result{*this}; |
| 108 | + result.data = this->data / other.data; |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +auto Decimal::operator%(const Decimal &other) const -> Decimal { |
| 113 | + Decimal result{*this}; |
| 114 | + result.data = this->data % other.data; |
| 115 | + return result; |
| 116 | +} |
| 117 | + |
| 118 | +auto Decimal::operator-() const -> Decimal { |
| 119 | + Decimal result{*this}; |
| 120 | + result.data = -this->data; |
| 121 | + return result; |
| 122 | +} |
| 123 | + |
| 124 | +auto Decimal::operator+() const -> Decimal { |
| 125 | + Decimal result{*this}; |
| 126 | + result.data = +this->data; |
| 127 | + return result; |
| 128 | +} |
| 129 | + |
| 130 | +auto Decimal::operator++() -> Decimal & { |
| 131 | + this->data += decimal::Decimal{1}; |
| 132 | + return *this; |
| 133 | +} |
| 134 | + |
| 135 | +auto Decimal::operator++(int) -> Decimal { |
| 136 | + Decimal result{*this}; |
| 137 | + this->data += decimal::Decimal{1}; |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +auto Decimal::operator--() -> Decimal & { |
| 142 | + this->data -= decimal::Decimal{1}; |
| 143 | + return *this; |
| 144 | +} |
| 145 | + |
| 146 | +auto Decimal::operator--(int) -> Decimal { |
| 147 | + Decimal result{*this}; |
| 148 | + this->data -= decimal::Decimal{1}; |
| 149 | + return result; |
| 150 | +} |
| 151 | + |
| 152 | +auto Decimal::operator==(const Decimal &other) const -> bool { |
| 153 | + return this->data == other.data; |
| 154 | +} |
| 155 | + |
| 156 | +auto Decimal::operator!=(const Decimal &other) const -> bool { |
| 157 | + return this->data != other.data; |
| 158 | +} |
| 159 | + |
| 160 | +auto Decimal::operator<(const Decimal &other) const -> bool { |
| 161 | + return this->data < other.data; |
| 162 | +} |
| 163 | + |
| 164 | +auto Decimal::operator<=(const Decimal &other) const -> bool { |
| 165 | + return this->data <= other.data; |
| 166 | +} |
| 167 | + |
| 168 | +auto Decimal::operator>(const Decimal &other) const -> bool { |
| 169 | + return this->data > other.data; |
| 170 | +} |
| 171 | + |
| 172 | +auto Decimal::operator>=(const Decimal &other) const -> bool { |
| 173 | + return this->data >= other.data; |
| 174 | +} |
| 175 | + |
| 176 | +} // namespace sourcemeta::core |
0 commit comments