iceberg-cpp
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
iceberg::Decimal Class Reference

Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represented is 38 significant digits. More...

#include <decimal.h>

Inheritance diagram for iceberg::Decimal:
iceberg::util::Formattable

Public Member Functions

constexpr Decimal () noexcept=default
 Default constructor initializes to zero.
 
constexpr Decimal (int128_t value) noexcept
 Create a Decimal from a 128-bit integer.
 
template<typename T >
requires (std::is_integral_v<T> && (sizeof(T) <= sizeof(uint64_t)))
constexpr Decimal (T value) noexcept
 Create a Decimal from any integer not wider than 64 bits.
 
 Decimal (std::string_view str)
 Parse a Decimal from a string representation.
 
constexpr Decimal (int64_t high, uint64_t low) noexcept
 Create a Decimal from two 64-bit integers.
 
DecimalNegate ()
 Negate the current Decimal value (in place)
 
DecimalAbs ()
 Absolute value of the current Decimal value (in place)
 
Decimaloperator+= (const Decimal &other)
 Add a number to this one. The result is truncated to 128 bits.
 
Decimaloperator-= (const Decimal &other)
 Subtract a number from this one. The result is truncated to 128 bits.
 
Decimaloperator*= (const Decimal &other)
 Multiply this number by another. The result is truncated to 128 bits.
 
Result< std::pair< Decimal, Decimal > > Divide (const Decimal &divisor) const
 Divide this number by another.
 
Decimaloperator/= (const Decimal &other)
 In place division.
 
Decimaloperator|= (const Decimal &other)
 Bitwise OR operation.
 
Decimaloperator&= (const Decimal &other)
 Bitwise AND operation.
 
Decimaloperator<<= (uint32_t shift)
 Shift left by the given number of bits (in place).
 
Decimal operator<< (uint32_t shift) const
 Shift left by the given number of bits.
 
Decimaloperator>>= (uint32_t shift)
 Shift right by the given number of bits (in place).
 
Decimal operator>> (uint32_t shift) const
 Shift right by the given number of bits.
 
constexpr int128_t value () const
 Get the underlying 128-bit integer representation of the number.
 
constexpr int64_t high () const
 Get the high bits of the two's complement representation of the number.
 
constexpr uint64_t low () const
 Get the low bits of the two's complement representation of the number.
 
Result< std::string > ToString (int32_t scale) const
 Convert the Decimal value to a base 10 decimal string with the given scale.
 
std::string ToIntegerString () const
 Convert the Decimal value to an integer string.
 
std::string ToString () const override
 Returns an integer string representation of the decimal value.
 
std::vector< uint8_t > ToBigEndian () const
 Convert Decimal's unscaled value to two’s-complement big-endian binary, using the minimum number of bytes for the value.
 
Result< DecimalRescale (int32_t orig_scale, int32_t new_scale) const
 Convert Decimal from one scale to another.
 
bool FitsInPrecision (int32_t precision) const
 Whether this number fits in the given precision.
 
std::strong_ordering operator<=> (const Decimal &other) const
 Spaceship operator for three-way comparison.
 
const uint8_t * native_endian_bytes () const
 
std::array< uint8_t, kByteWidth > ToBytes () const
 Returns the raw bytes of the value in native-endian byte order.
 
int64_t Sign () const
 Returns 1 if positive or zero, -1 if strictly negative.
 
bool IsNegative () const
 Check if the Decimal value is negative.
 
 operator bool () const
 

Static Public Member Functions

static Decimal Abs (const Decimal &value)
 Absolute value of the current Decimal value.
 
static Result< DecimalFromString (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 provided not null.
 
static Result< DecimalFromBigEndian (const uint8_t *data, int32_t length)
 Convert from a big-endian byte representation. The length must be between 1 and 16.
 
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.
 

Static Public Attributes

static constexpr int32_t kBitWidth = 128
 
static constexpr int32_t kByteWidth = kBitWidth / 8
 
static constexpr int32_t kMaxPrecision = 38
 
static constexpr int32_t kMaxScale = 38
 

Friends

bool operator== (const Decimal &lhs, const Decimal &rhs)
 
bool operator!= (const Decimal &lhs, const Decimal &rhs)
 
ICEBERG_EXPORT friend Decimal operator- (const Decimal &operand)
 
ICEBERG_EXPORT friend Decimal operator~ (const Decimal &operand)
 
ICEBERG_EXPORT friend Decimal operator+ (const Decimal &lhs, const Decimal &rhs)
 
ICEBERG_EXPORT friend Decimal operator- (const Decimal &lhs, const Decimal &rhs)
 
ICEBERG_EXPORT friend Decimal operator* (const Decimal &lhs, const Decimal &rhs)
 
ICEBERG_EXPORT friend Decimal operator/ (const Decimal &lhs, const Decimal &rhs)
 
ICEBERG_EXPORT friend Decimal operator% (const Decimal &lhs, const Decimal &rhs)
 

Detailed Description

Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represented is 38 significant digits.

Constructor & Destructor Documentation

◆ Decimal()

iceberg::Decimal::Decimal ( std::string_view  str)
explicit

Parse a Decimal from a string representation.

Exceptions
Thisconstructor throws an exception if parsing fails. Use Decimal::FromString() if you want to handle errors more gracefully.

Member Function Documentation

◆ Divide()

Result< std::pair< Decimal, Decimal > > iceberg::Decimal::Divide ( const Decimal divisor) const

Divide this number by another.

The operation does not modify the current Decimal value. The answer rounds towards zero. Signs work like: 21 / 5 -> 4, 1 -21 / 5 -> -4, -1 21 / -5 -> -4, 1 -21 / -5 -> 4, -1

Parameters
[in]divisorthe number to divide by
Returns
the pair of the quotient and the remainder

◆ FitsInPrecision()

bool iceberg::Decimal::FitsInPrecision ( int32_t  precision) const

Whether this number fits in the given precision.

Returns true if the number of significant digits is less or equal to precision.

◆ FromBigEndian()

Result< Decimal > iceberg::Decimal::FromBigEndian ( const uint8_t *  data,
int32_t  length 
)
static

Convert from a big-endian byte representation. The length must be between 1 and 16.

Returns
error status if the length is an invalid value

◆ FromString()

Result< Decimal > iceberg::Decimal::FromString ( std::string_view  str,
int32_t *  precision = nullptr,
int32_t *  scale = nullptr 
)
static

Convert the decimal string to a Decimal value, optionally including precision and scale if they are provided not null.

Parameters
strThe string representation of the Decimal value.
[out]precisionOptional pointer to store the precision of the parsed value.
[out]scaleOptional pointer to store the scale of the parsed value.
Returns
The Decimal value.

◆ ToBigEndian()

std::vector< uint8_t > iceberg::Decimal::ToBigEndian ( ) const

Convert Decimal's unscaled value to two’s-complement big-endian binary, using the minimum number of bytes for the value.

Returns
A vector containing the big-endian bytes.

◆ ToString() [1/2]

std::string iceberg::Decimal::ToString ( ) const
inlineoverridevirtual

Returns an integer string representation of the decimal value.

Implements iceberg::util::Formattable.

◆ ToString() [2/2]

Result< std::string > iceberg::Decimal::ToString ( int32_t  scale) const

Convert the Decimal value to a base 10 decimal string with the given scale.

Parameters
scaleThe scale to use for the string representation.
Returns
The string representation of the Decimal value.

The documentation for this class was generated from the following files: