1#ifndef SOURCEMETA_CORE_NUMERIC_DECIMAL_H_
2#define SOURCEMETA_CORE_NUMERIC_DECIMAL_H_
4#ifndef SOURCEMETA_CORE_NUMERIC_EXPORT
5#include <sourcemeta/core/numeric_export.h>
8#include <sourcemeta/core/preprocessor.h>
17namespace sourcemeta::core {
21class SOURCEMETA_CORE_NUMERIC_EXPORT
Decimal {
37 requires std::integral<T> && std::is_signed_v<T> &&
38 (!std::same_as<T, std::int64_t>)
46 requires std::integral<T> && std::is_unsigned_v<T> &&
47 (!std::same_as<T, std::uint64_t>)
60 explicit Decimal(
const char *
const value);
63 explicit Decimal(
const std::string &value);
66 explicit Decimal(
const std::string_view value);
75 [[nodiscard]]
static auto nan(std::uint64_t payload = 0) ->
Decimal;
78 [[nodiscard]]
static auto snan(std::uint64_t payload = 0) ->
Decimal;
98 [[nodiscard]] auto
to_string() const -> std::
string;
101 [[nodiscard]] auto
to_int64() const -> std::int64_t;
104 [[nodiscard]] auto
to_int32() const -> std::int32_t;
134 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
is_integer() const ->
bool {
135 return (this->flags_ & FLAG_INTEGER_LITERAL) != 0;
139 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_finite() const ->
bool {
140 return (this->flags_ & (FLAG_NAN | FLAG_SNAN | FLAG_INFINITE)) == 0;
146 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_real() const ->
bool {
172 [[nodiscard]] SOURCEMETA_FORCEINLINE auto
is_nan() const ->
bool {
173 return (this->flags_ & FLAG_NAN) != 0;
177 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_snan() const ->
bool {
178 return (this->flags_ & FLAG_SNAN) != 0;
182 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_qnan() const ->
bool {
183 return (this->flags_ & FLAG_NAN) != 0 && ((this->flags_ & FLAG_SNAN) == 0);
190 return static_cast<std::uint64_t
>(this->coefficient_);
194 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_infinite() const ->
bool {
195 return (this->flags_ & FLAG_INFINITE) != 0;
199 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto is_signed() const ->
bool {
200 return (this->flags_ & FLAG_SIGN) != 0;
247 [[nodiscard]] auto operator+(const
Decimal &other) const ->
Decimal;
250 [[nodiscard]] auto operator-(const
Decimal &other) const ->
Decimal;
253 [[nodiscard]] auto operator*(const
Decimal &other) const ->
Decimal;
256 [[nodiscard]] auto operator/(const
Decimal &other) const ->
Decimal;
259 [[nodiscard]] auto operator%(const
Decimal &other) const ->
Decimal;
262 [[nodiscard]] auto operator-() const ->
Decimal;
265 [[nodiscard]] auto operator+() const ->
Decimal;
268 auto operator++() ->
Decimal &;
271 auto operator++(
int) ->
Decimal;
274 auto operator--() ->
Decimal &;
277 auto operator--(
int) ->
Decimal;
280 [[nodiscard]] auto operator==(const
Decimal &other) const ->
bool;
283 [[nodiscard]] auto operator!=(const
Decimal &other) const ->
bool;
286 [[nodiscard]] auto operator<(const
Decimal &other) const ->
bool;
289 [[nodiscard]] auto operator<=(const
Decimal &other) const ->
bool;
292 [[nodiscard]] auto operator>(const
Decimal &other) const ->
bool;
295 [[nodiscard]] auto operator>=(const
Decimal &other) const ->
bool;
298 static constexpr std::uint8_t FLAG_SIGN = 0x01;
299 static constexpr std::uint8_t FLAG_NAN = 0x02;
300 static constexpr std::uint8_t FLAG_SNAN = 0x04;
301 static constexpr std::uint8_t FLAG_INFINITE = 0x08;
302 static constexpr std::uint8_t FLAG_INTEGER_LITERAL = 0x40;
306 [[nodiscard]] auto remove_trailing_zeros(std::int32_t allowance) const
309 std::int64_t coefficient_{0};
310 std::uint64_t coefficient_high_{0};
311 std::int32_t exponent_{0};
312 std::uint8_t flags_{0};
316 requires std::integral<T>
317inline auto operator+(
const T left,
const Decimal &right) ->
Decimal {
322 requires std::integral<T>
323inline auto operator-(
const T left,
const Decimal &right) ->
Decimal {
328 requires std::integral<T>
329inline auto operator*(
const T left,
const Decimal &right) ->
Decimal {
334 requires std::integral<T>
335inline auto operator/(
const T left,
const Decimal &right) ->
Decimal {
340 requires std::integral<T>
341inline auto operator%(
const T left,
const Decimal &right) ->
Decimal {
346 requires std::integral<T>
347inline auto operator==(
const T left,
const Decimal &right) ->
bool {
352 requires std::integral<T>
353inline auto operator!=(
const T left,
const Decimal &right) ->
bool {
358 requires std::integral<T>
359inline auto operator<(
const T left,
const Decimal &right) ->
bool {
364 requires std::integral<T>
365inline auto operator<=(
const T left,
const Decimal &right) ->
bool {
370 requires std::integral<T>
371inline auto operator>(
const T left,
const Decimal &right) ->
bool {
376 requires std::integral<T>
377inline auto operator>=(
const T left,
const Decimal &right) ->
bool {
auto is_double() const -> bool
SOURCEMETA_FORCEINLINE auto is_snan() const -> bool
Check if the decimal number is a signaling NaN.
Definition numeric_decimal.h:177
auto to_double() const -> double
Convert the decimal number to a 64-bit double.
Decimal(const std::string &value)
Construct a decimal number from a C++ string.
Decimal(const char *const value)
Construct a decimal number from a C-string.
auto divide_integer(const Decimal &other) const -> Decimal
Integer division (truncate toward zero).
Decimal(float value)
Construct a decimal number from a 32-bit float.
auto to_uint32() const -> std::uint32_t
Convert the decimal number to a 32-bit unsigned integer.
auto is_uint64() const -> bool
Check if the decimal number fits in a 64-bit unsigned integer.
auto is_uint32() const -> bool
Check if the decimal number fits in a 32-bit unsigned integer.
auto to_string() const -> std::string
Convert the decimal number to a plain string representation.
auto reduce() const -> Decimal
Strip trailing zeros from the coefficient.
SOURCEMETA_FORCEINLINE auto is_signed() const -> bool
Check if the decimal number is signed (negative, including -0).
Definition numeric_decimal.h:199
auto is_int32() const -> bool
Check if the decimal number fits in a 32-bit signed integer.
SOURCEMETA_FORCEINLINE auto is_finite() const -> bool
Check if the decimal number is finite.
Definition numeric_decimal.h:139
SOURCEMETA_FORCEINLINE auto is_real() const -> bool
Definition numeric_decimal.h:146
SOURCEMETA_FORCEINLINE auto is_qnan() const -> bool
Check if the decimal number is a quiet NaN.
Definition numeric_decimal.h:182
auto is_integral() const -> bool
auto is_zero() const -> bool
Check if the decimal number is zero.
SOURCEMETA_FORCEINLINE auto is_integer() const -> bool
Definition numeric_decimal.h:134
auto to_scientific_string() const -> std::string
Convert the decimal number to scientific notation string.
auto is_float() const -> bool
Decimal(const std::string_view value)
Construct a decimal number from a string view.
auto logb() const -> Decimal
Return the adjusted exponent (floor of base-10 logarithm).
static auto exact_from(double value) -> Decimal
auto divisible_by(const Decimal &divisor) const -> bool
Check if this decimal number is divisible by another.
Decimal(const T value)
Construct a decimal number from an unsigned integral type.
Definition numeric_decimal.h:48
Decimal() noexcept
Construct a decimal number initialized to zero.
static auto nan(std::uint64_t payload=0) -> Decimal
Create a quiet NaN (Not a Number) value with an optional payload.
auto to_uint64() const -> std::uint64_t
Convert the decimal number to a 64-bit unsigned integer.
static auto strict_from(double value) -> Decimal
auto same_quantum(const Decimal &other) const -> bool
Check if two numbers have the same quantum (exponent).
SOURCEMETA_FORCEINLINE auto is_nan() const -> bool
Definition numeric_decimal.h:172
SOURCEMETA_FORCEINLINE auto nan_payload() const -> std::uint64_t
Get the payload of a NaN value (0 if no payload).
Definition numeric_decimal.h:187
Decimal(std::int64_t value)
Construct a decimal number from a 64-bit signed integer.
Decimal(double value)
Construct a decimal number from a 64-bit double.
auto to_integral() const -> Decimal
Round the decimal number to an integral value.
auto to_int32() const -> std::int32_t
Convert the decimal number to a 32-bit signed integer.
Decimal(std::uint64_t value)
Construct a decimal number from a 64-bit unsigned integer.
auto compare_total(const Decimal &other) const -> Decimal
IEEE 754 total ordering comparison returning -1, 0, or 1.
static auto negative_infinity() -> Decimal
Create a negative infinity value.
SOURCEMETA_FORCEINLINE auto is_infinite() const -> bool
Check if the decimal number is infinite.
Definition numeric_decimal.h:194
auto trim() const -> Decimal
auto scale_by(const Decimal &scale) const -> Decimal
Scale the number by a power of 10.
static auto infinity() -> Decimal
Create a positive infinity value.
auto to_int64() const -> std::int64_t
Convert the decimal number to a 64-bit signed integer.
static auto snan(std::uint64_t payload=0) -> Decimal
Create a signaling NaN value with an optional payload.
auto to_float() const -> float
Convert the decimal number to a 32-bit float.
auto is_int64() const -> bool
Check if the decimal number fits in a 64-bit signed integer.