48 static constexpr int32_t kBitWidth = 128;
49 static constexpr int32_t kByteWidth = kBitWidth / 8;
50 static constexpr int32_t kMaxPrecision = 38;
51 static constexpr int32_t kMaxScale = 38;
57 constexpr
Decimal(int128_t value) noexcept
62 requires(std::is_integral_v<T> && (
sizeof(T) <=
sizeof(uint64_t)))
64 : data_(
static_cast<int128_t
>(value)) {}
72 constexpr Decimal(int64_t high, uint64_t low)
noexcept {
73 data_ = (
static_cast<int128_t
>(high) << 64) | low;
136 constexpr int128_t
value()
const {
return data_; }
139 constexpr int64_t
high()
const {
return static_cast<int64_t
>(data_ >> 64); }
142 constexpr uint64_t
low()
const {
return static_cast<uint64_t
>(data_); }
153 std::string
ToString()
const override {
return ToIntegerString(); }
162 int32_t* scale =
nullptr);
184 if (high() != other.
high()) {
185 return high() <=> other.
high();
187 return low() <=> other.
low();
192 int32_t lhs_scale, int32_t rhs_scale);
194 const uint8_t* native_endian_bytes()
const {
195 return reinterpret_cast<const uint8_t*
>(&data_);
199 std::array<uint8_t, kByteWidth>
ToBytes()
const;
202 int64_t
Sign()
const {
return 1 | (high() >> 63); }
207 explicit operator bool()
const {
return data_ != 0; }
209 friend bool operator==(
const Decimal& lhs,
const Decimal& rhs) {
210 return lhs.data_ == rhs.data_;
213 friend bool operator!=(
const Decimal& lhs,
const Decimal& rhs) {
214 return lhs.data_ != rhs.data_;
217 ICEBERG_EXPORT
friend Decimal operator-(
const Decimal& operand);
218 ICEBERG_EXPORT
friend Decimal operator~(
const Decimal& operand);
220 ICEBERG_EXPORT
friend Decimal operator+(
const Decimal& lhs,
const Decimal& rhs);
221 ICEBERG_EXPORT
friend Decimal operator-(
const Decimal& lhs,
const Decimal& rhs);
222 ICEBERG_EXPORT
friend Decimal operator*(
const Decimal& lhs,
const Decimal& rhs);
223 ICEBERG_EXPORT
friend Decimal operator/(
const Decimal& lhs,
const Decimal& rhs);
224 ICEBERG_EXPORT
friend Decimal operator%(
const Decimal& lhs,
const Decimal& rhs);
230ICEBERG_EXPORT std::ostream& operator<<(std::ostream& os,
const Decimal&
decimal);
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Result< std::string > ToString(int32_t scale) const
Convert the Decimal value to a base 10 decimal string with the given scale.
Decimal & operator<<=(uint32_t shift)
Shift left by the given number of bits (in place).
std::string ToIntegerString() const
Convert the Decimal value to an integer string.
Decimal operator>>(uint32_t shift) const
Shift right by the given number of bits.
Definition decimal.h:129
Decimal & operator&=(const Decimal &other)
Bitwise AND operation.
std::strong_ordering operator<=>(const Decimal &other) const
Spaceship operator for three-way comparison.
Definition decimal.h:183
static Result< Decimal > FromBigEndian(const uint8_t *data, int32_t length)
Convert from a big-endian byte representation. The length must be between 1 and 16.
constexpr uint64_t low() const
Get the low bits of the two's complement representation of the number.
Definition decimal.h:142
Decimal & operator>>=(uint32_t shift)
Shift right by the given number of bits (in place).
Decimal & operator/=(const Decimal &other)
In place division.
Decimal & operator-=(const Decimal &other)
Subtract a number from this one. The result is truncated to 128 bits.
std::vector< uint8_t > ToBigEndian() const
Convert Decimal's unscaled value to two’s-complement big-endian binary, using the minimum number of b...
bool IsNegative() const
Check if the Decimal value is negative.
Definition decimal.h:205
Decimal & Negate()
Negate the current Decimal value (in place)
Decimal & operator*=(const Decimal &other)
Multiply this number by another. The result is truncated to 128 bits.
constexpr Decimal(T value) noexcept
Create a Decimal from any integer not wider than 64 bits.
Definition decimal.h:63
Decimal & operator+=(const Decimal &other)
Add a number to this one. The result is truncated to 128 bits.
constexpr int128_t value() const
Get the underlying 128-bit integer representation of the number.
Definition decimal.h:136
std::array< uint8_t, kByteWidth > ToBytes() const
Returns the raw bytes of the value in native-endian byte order.
bool FitsInPrecision(int32_t precision) const
Whether this number fits in the given precision.
Result< std::pair< Decimal, Decimal > > Divide(const Decimal &divisor) const
Divide this number by another.
Decimal & Abs()
Absolute value of the current Decimal value (in place)
Decimal(std::string_view str)
Parse a Decimal from a string representation.
constexpr int64_t high() const
Get the high bits of the two's complement representation of the number.
Definition decimal.h:139
std::string ToString() const override
Returns an integer string representation of the decimal value.
Definition decimal.h:153
Decimal & operator|=(const Decimal &other)
Bitwise OR operation.
constexpr Decimal(int64_t high, uint64_t low) noexcept
Create a Decimal from two 64-bit integers.
Definition decimal.h:72
Result< Decimal > Rescale(int32_t orig_scale, int32_t new_scale) const
Convert Decimal from one scale to another.
static Decimal Abs(const Decimal &value)
Absolute value of the current Decimal value.
Decimal operator<<(uint32_t shift) const
Shift left by the given number of bits.
Definition decimal.h:119
static std::partial_ordering Compare(const Decimal &lhs, const Decimal &rhs, int32_t lhs_scale, int32_t rhs_scale)
Compare two Decimals with different scales.
constexpr Decimal() noexcept=default
Default constructor initializes to zero.
static Result< Decimal > FromString(std::string_view str, int32_t *precision=nullptr, int32_t *scale=nullptr)
Convert the decimal string to a Decimal value, optionally including precision and scale if they are p...
int64_t Sign() const
Returns 1 if positive or zero, -1 if strictly negative.
Definition decimal.h:202
ICEBERG_EXPORT std::shared_ptr< DecimalType > decimal(int32_t precision, int32_t scale)
Create a DecimalType with the given precision and scale.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.