pitrou commented on code in PR #43957:
URL: https://github.com/apache/arrow/pull/43957#discussion_r1763398047
##########
cpp/src/arrow/util/decimal.h:
##########
@@ -31,6 +31,243 @@
namespace arrow {
+class Decimal64;
+
+/// Represents a signed 32-bit decimal value in two's complement.
+/// Calulations wrap around and overflow is ignored.
+/// The max decimal precision that can be safely represented is
+/// 9 significant digits.
+///
+/// The implementation is split into two parts :
+///
+/// 1. BasicDecimal32
+/// - can be safely compiled to IR without references to libstdc++
+/// 2. Decimal32
+/// - has additional functionality on top of BasicDecimal32 to deal with
+/// strings and streams
+class ARROW_EXPORT Decimal32 : public BasicDecimal32 {
+ public:
+ /// \cond FALSE
+ // (need to avoid a duplicate definition in sphinx)
+ using BasicDecimal32::BasicDecimal32;
+ /// \endcond
+
+ /// \brief constructor creates a Decimal32 from a BasicDecimal32
+ constexpr Decimal32(const BasicDecimal32& value) noexcept // NOLINT
runtime/explicit
+ : BasicDecimal32(value) {}
+
+ /// \brief Parse the number from a base 10 string representation
+ explicit Decimal32(const std::string& value);
+
+ /// \brief Empty constructor creates a Decimal32 with a value of 0
+ /// this is required for some older compilers
+ constexpr Decimal32() noexcept : BasicDecimal32() {}
+
+ /// \brief 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
+ /// \param[in] divisor the number to divide by
+ /// \return the pair of the quotient and the remainder
+ Result<std::pair<Decimal32, Decimal32>> Divide(const Decimal32& divisor)
const {
+ std::pair<Decimal32, Decimal32> result;
+ auto dstatus = BasicDecimal32::Divide(divisor, &result.first,
&result.second);
+ ARROW_RETURN_NOT_OK(ToArrowStatus(dstatus));
+ return result;
+ }
+
+ /// \brief Convert the Decimal32 value to a base 10 decimal string with the
given scale
+ std::string ToString(int32_t scale) const;
Review Comment:
It would be nice to add tests for the non-trivial methods: `ToString`,
`FromString`, `ToReal`, `FromReal`, `FromBigEndian`.
(same for `Decimal64`)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]