Utilities

Decimal Numbers

class Decimal128 : public arrow::BasicDecimal128

Represents a signed 128-bit integer in two’s complement.

Calculations wrap around and overflow is ignored.

For a discussion of the algorithms, look at Knuth’s volume 2, Semi-numerical Algorithms section 4.3.1.

Adapted from the Apache ORC C++ implementation

The implementation is split into two parts :

  1. BasicDecimal128

    • can be safely compiled to IR without references to libstdc++.

  2. Decimal128

    • has additional functionality on top of BasicDecimal128 to deal with strings and streams.

Public Functions

constexpr Decimal128(const BasicDecimal128 &value)

constructor creates a Decimal128 from a BasicDecimal128.

Decimal128(const std::string &value)

Parse the number from a base 10 string representation.

constexpr Decimal128()

Empty constructor creates a Decimal128 with a value of 0.

Status Divide(const Decimal128 &divisor, Decimal128 *result, Decimal128 *remainder) const

Divide this number by right and return the result.

This operation is not destructive. The answer rounds to zero. Signs work like: 21 / 5 -> 4, 1 -21 / 5 -> -4, -1 21 / -5 -> -4, 1 -21 / -5 -> 4, -1

Parameters
  • [in] divisor: the number to divide by

  • [out] result: the quotient

  • [out] remainder: the remainder after the division

std::string ToString(int32_t scale) const

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

std::string ToIntegerString() const

Convert the value to an integer string.

operator int64_t() const

Cast this value to an int64_t.

Status Rescale(int32_t original_scale, int32_t new_scale, Decimal128 *out) const

Convert Decimal128 from one scale to another.

template<typename T, typename = internal::EnableIfIsOneOf<T, int32_t, int64_t>>
Status ToInteger(T *out) const

Convert to a signed integer.

Public Static Functions

static Status FromString(const util::string_view &s, Decimal128 *out, int32_t *precision = NULLPTR, int32_t *scale = NULLPTR)

Convert a decimal string to a Decimal128 value, optionally including precision and scale if they’re passed in and not null.

static Status FromBigEndian(const uint8_t *data, int32_t length, Decimal128 *out)

Convert from a big-endian byte representation.

The length must be between 1 and 16.

Return

error status if the length is an invalid value