|
33 | 33 | #include <iomanip> |
34 | 34 | #include <iostream> |
35 | 35 | #include <limits> |
| 36 | +#include <ostream> |
36 | 37 | #include <sstream> |
37 | 38 | #include <string> |
38 | 39 | #include <string_view> |
@@ -183,7 +184,7 @@ static Status BuildFromArray(Decimal* result, const uint32_t* array, int64_t len |
183 | 184 | std::array<uint64_t, 2> result_array = {0, 0}; |
184 | 185 | for (int64_t i = length - 2 * 2 - 1; i >= 0; i--) { |
185 | 186 | if (array[i] != 0) { |
186 | | - return Overflow("Decimal division overflow"); |
| 187 | + return Invalid("Decimal division overflow"); |
187 | 188 | } |
188 | 189 | } |
189 | 190 |
|
@@ -250,7 +251,7 @@ static inline Status DecimalDivide(const Decimal& dividend, const Decimal& divis |
250 | 251 | } |
251 | 252 |
|
252 | 253 | if (divisor_length == 0) { |
253 | | - return DivideByZero("Cannot divide by zero in DecimalDivide"); |
| 254 | + return Invalid("Cannot divide by zero in DecimalDivide"); |
254 | 255 | } |
255 | 256 |
|
256 | 257 | if (divisor_length == 1) { |
@@ -1013,9 +1014,8 @@ struct DecimalRealConversion { |
1013 | 1014 | const auto x = std::nearbyint(real * PowerOfTen<double>(scale)); |
1014 | 1015 | const auto max_abs = PowerOfTen<double>(precision); |
1015 | 1016 | if (x <= -max_abs || x >= max_abs) { |
1016 | | - return Overflow( |
1017 | | - "Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", real, |
1018 | | - precision, scale); |
| 1017 | + return Invalid("Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", |
| 1018 | + real, precision, scale); |
1019 | 1019 | } |
1020 | 1020 |
|
1021 | 1021 | // Extract high and low bits |
@@ -1048,9 +1048,8 @@ struct DecimalRealConversion { |
1048 | 1048 | // overflow. |
1049 | 1049 | // NOTE: `limit` is allowed here as rounding can make it smaller than |
1050 | 1050 | // the theoretical limit (for example, 1.0e23 < 10^23). |
1051 | | - return Overflow( |
1052 | | - "Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", real, |
1053 | | - precision, scale); |
| 1051 | + return Invalid("Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", |
| 1052 | + real, precision, scale); |
1054 | 1053 | } |
1055 | 1054 |
|
1056 | 1055 | // 2. Losslessly convert `real` to `mant * 2**k` |
@@ -1140,9 +1139,8 @@ struct DecimalRealConversion { |
1140 | 1139 |
|
1141 | 1140 | // Rounding might have pushed `x` just above the max precision, check again |
1142 | 1141 | if (!x.FitsInPrecision(precision)) { |
1143 | | - return Overflow( |
1144 | | - "Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", real, |
1145 | | - precision, scale); |
| 1142 | + return Invalid("Cannot convert {} to Decimal(precision = {}, scale = {}): overflow", |
| 1143 | + real, precision, scale); |
1146 | 1144 | } |
1147 | 1145 | return x; |
1148 | 1146 | } |
@@ -1346,6 +1344,11 @@ std::array<uint8_t, Decimal::kByteWidth> Decimal::ToBytes() const { |
1346 | 1344 | return out; |
1347 | 1345 | } |
1348 | 1346 |
|
| 1347 | +ICEBERG_EXPORT std::ostream& operator<<(std::ostream& os, const Decimal& decimal) { |
| 1348 | + os << decimal.ToIntegerString(); |
| 1349 | + return os; |
| 1350 | +} |
| 1351 | + |
1349 | 1352 | // Unary operators |
1350 | 1353 | ICEBERG_EXPORT Decimal operator-(const Decimal& operand) { |
1351 | 1354 | Decimal result(operand.high(), operand.low()); |
|
0 commit comments