Fixed-width integer types, decimal numbers, and numeric parsing and encoding utilities. More...
Concepts | |
| concept | sourcemeta::core::decimal_or_integral |
| concept | sourcemeta::core::any_decimal |
Functions | |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_double (const std::string_view input) noexcept -> std::optional< double > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_double_exact (const std::string_view input) noexcept -> std::optional< double > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_int64_t (const std::string_view input) noexcept -> std::optional< std::int64_t > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_int64_t (const std::string_view input, const int base) noexcept -> std::optional< std::int64_t > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_uint64_t (const std::string_view input) noexcept -> std::optional< std::uint64_t > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_uint32_t (const std::string_view input) noexcept -> std::optional< std::uint32_t > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_uint32_t (const std::string_view input, const int base) noexcept -> std::optional< std::uint32_t > |
| SOURCEMETA_CORE_NUMERIC_EXPORT auto | sourcemeta::core::to_uint16_t (const std::string_view input) noexcept -> std::optional< std::uint16_t > |
| template<typename T> | |
| auto | sourcemeta::core::to_decimal (const T &value) -> Decimal |
| constexpr auto | sourcemeta::core::is_digit (const char character) -> bool |
| constexpr auto | sourcemeta::core::is_positive_digit (const char character) -> bool |
| template<typename T> | |
| constexpr auto | sourcemeta::core::is_byte (const T &value) -> bool |
| template<typename Dividend, typename Divisor> requires decimal_or_integral<Dividend> && decimal_or_integral<Divisor> | |
| auto | sourcemeta::core::divide_floor (const Dividend ÷nd, const Divisor &divisor) |
| template<typename Dividend, typename Divisor> requires decimal_or_integral<Dividend> && decimal_or_integral<Divisor> | |
| auto | sourcemeta::core::divide_ceil (const Dividend ÷nd, const Divisor &divisor) |
| template<typename Minimum, typename Maximum, typename Multiplier> requires decimal_or_integral<Minimum> && decimal_or_integral<Maximum> && decimal_or_integral<Multiplier> | |
| auto | sourcemeta::core::count_multiples (const Minimum &minimum, const Maximum &maximum, const Multiplier &multiplier) |
| template<typename T> | |
| constexpr auto | sourcemeta::core::is_within (const T &value, const std::int64_t lower, const std::int64_t higher) noexcept -> bool |
| template<typename T> | |
| constexpr auto | sourcemeta::core::is_within (const T &value, const std::uint64_t lower, const std::uint64_t higher) noexcept -> bool |
| auto | sourcemeta::core::is_within (const Decimal &value, const Decimal &lower, const Decimal &higher) -> bool |
| template<typename T> | |
| auto | sourcemeta::core::abs (const T &value) |
| constexpr auto | sourcemeta::core::closest_smallest_exponent (const std::uint64_t value, const std::uint8_t base, const std::uint8_t exponent_start, const std::uint8_t exponent_end) -> std::uint8_t |
| template<std::floating_point Real> | |
| constexpr auto | sourcemeta::core::correct_ieee754 (const Real value) -> Real |
| template<std::integral Integer, std::floating_point Real> | |
| constexpr auto | sourcemeta::core::real_digits (Real value, std::uint64_t &point_position) -> Integer |
| template<std::floating_point Real> requires (sizeof(Real) == sizeof(std::uint32_t) || sizeof(Real) == sizeof(std::uint64_t)) | |
| auto | sourcemeta::core::real_equal (const Real left, const Real right) -> bool |
| template<std::floating_point Real, std::integral Integer> | |
| constexpr auto | sourcemeta::core::is_representable_as (const Integer value) -> bool |
| template<std::floating_point Real, std::floating_point Wider> | |
| auto | sourcemeta::core::is_representable_as (const Wider value) -> bool |
| auto | sourcemeta::core::varint_encode (const std::uint64_t value, std::byte *const destination) -> std::size_t |
| auto | sourcemeta::core::varint_decode (const std::byte *const source, const std::size_t size) -> std::uint64_t |
| template<typename T> | |
| auto | sourcemeta::core::zigzag_encode (const T &value) |
| template<typename T> | |
| auto | sourcemeta::core::zigzag_decode (const T &value) |
Variables | |
| template<unsigned int T> | |
| constexpr auto | sourcemeta::core::uint_max |
| constexpr std::size_t | sourcemeta::core::VARINT_MAXIMUM_BYTES {10} |
Fixed-width integer types, decimal numbers, and numeric parsing and encoding utilities.
This functionality is included as follows:
| class sourcemeta::core::Decimal |
Represents an arbitrary-precision decimal number.
Public Member Functions | |
|---|---|
| Decimal () noexcept | |
| Construct a decimal number initialized to zero. | |
| ~Decimal () | |
| Destructor. | |
| Decimal (const Decimal &other) | |
| Copy constructor. | |
| Decimal (Decimal &&other) noexcept | |
| Move constructor. | |
|
template<typename T> requires std::integral<T> && std::is_signed_v<T> && (!std::same_as<T, std::int64_t>) | |
| Decimal (const T value) | |
| Construct a decimal number from a signed integral type. | |
| Decimal (std::int64_t value) | |
| Construct a decimal number from a 64-bit signed integer. | |
|
template<typename T> requires std::integral<T> && std::is_unsigned_v<T> && (!std::same_as<T, std::uint64_t>) | |
| Decimal (const T value) | |
| Construct a decimal number from an unsigned integral type. | |
| Decimal (std::uint64_t value) | |
| Construct a decimal number from a 64-bit unsigned integer. | |
| Decimal (float value) | |
| Construct a decimal number from a 32-bit float. | |
| Decimal (double value) | |
| Construct a decimal number from a 64-bit double. | |
| Decimal (const char *const value) | |
| Construct a decimal number from a C-string. | |
| Decimal (const std::string &value) | |
| Construct a decimal number from a C++ string. | |
| Decimal (const std::string_view value) | |
| Construct a decimal number from a string view. | |
| auto | to_scientific_string () const -> std::string |
| Convert the decimal number to scientific notation string. | |
| auto | to_string () const -> std::string |
| Convert the decimal number to a plain string representation. | |
| auto | to_int64 () const -> std::int64_t |
| Convert the decimal number to a 64-bit signed integer. | |
| auto | to_int32 () const -> std::int32_t |
| Convert the decimal number to a 32-bit signed integer. | |
| auto | to_uint64 () const -> std::uint64_t |
| Convert the decimal number to a 64-bit unsigned integer. | |
| auto | to_uint32 () const -> std::uint32_t |
| Convert the decimal number to a 32-bit unsigned integer. | |
| auto | to_float () const -> float |
| Convert the decimal number to a 32-bit float. | |
| auto | to_double () const -> double |
| Convert the decimal number to a 64-bit double. | |
| auto | is_zero () const -> bool |
| Check if the decimal number is zero. | |
| auto | is_integral () const -> bool |
| SOURCEMETA_FORCEINLINE auto | is_integer () const -> bool |
| SOURCEMETA_FORCEINLINE auto | is_finite () const -> bool |
| Check if the decimal number is finite. | |
| SOURCEMETA_FORCEINLINE auto | is_real () const -> bool |
| auto | is_float () const -> bool |
| auto | is_double () const -> bool |
| auto | is_int32 () const -> bool |
| Check if the decimal number fits in a 32-bit signed integer. | |
| auto | is_int64 () const -> bool |
| Check if the decimal number fits in a 64-bit signed integer. | |
| auto | is_uint32 () const -> bool |
| Check if the decimal number fits in a 32-bit unsigned integer. | |
| auto | is_uint64 () const -> bool |
| Check if the decimal number fits in a 64-bit unsigned integer. | |
| SOURCEMETA_FORCEINLINE auto | is_nan () const -> bool |
| SOURCEMETA_FORCEINLINE auto | is_snan () const -> bool |
| Check if the decimal number is a signaling NaN. | |
| SOURCEMETA_FORCEINLINE auto | is_qnan () const -> bool |
| Check if the decimal number is a quiet NaN. | |
| SOURCEMETA_FORCEINLINE auto | nan_payload () const -> std::uint64_t |
| Get the payload of a NaN value (0 if no payload). | |
| SOURCEMETA_FORCEINLINE auto | is_infinite () const -> bool |
| Check if the decimal number is infinite. | |
| SOURCEMETA_FORCEINLINE auto | is_signed () const -> bool |
| Check if the decimal number is signed (negative, including -0). | |
| auto | to_integral () const -> Decimal |
| Round the decimal number to an integral value. | |
| auto | divisible_by (const Decimal &divisor) const -> bool |
| Check if this decimal number is divisible by another. | |
| auto | reduce () const -> Decimal |
| Strip trailing zeros from the coefficient. | |
| auto | trim () const -> Decimal |
| auto | logb () const -> Decimal |
| Return the adjusted exponent (floor of base-10 logarithm). | |
| auto | scale_by (const Decimal &scale) const -> Decimal |
| Scale the number by a power of 10. | |
| auto | same_quantum (const Decimal &other) const -> bool |
| Check if two numbers have the same quantum (exponent). | |
| auto | compare_total (const Decimal &other) const -> Decimal |
| IEEE 754 total ordering comparison returning -1, 0, or 1. | |
| auto | divide_integer (const Decimal &other) const -> Decimal |
| Integer division (truncate toward zero). | |
Static Public Member Functions | |
| static auto | nan (std::uint64_t payload=0) -> Decimal |
| Create a quiet NaN (Not a Number) value with an optional payload. | |
| static auto | snan (std::uint64_t payload=0) -> Decimal |
| Create a signaling NaN value with an optional payload. | |
| static auto | strict_from (double value) -> Decimal |
| static auto | exact_from (double value) -> Decimal |
| static auto | infinity () -> Decimal |
| Create a positive infinity value. | |
| static auto | negative_infinity () -> Decimal |
| Create a negative infinity value. | |
|
staticnodiscard |
Create a decimal that preserves the exact value stored by a double, expanding every digit of its underlying binary representation
|
nodiscard |
Check if the decimal number can be represented as a 64-bit double without precision loss
|
nodiscard |
Check if the decimal number can be represented as a 32-bit float without precision loss
|
inlinenodiscard |
Check if the decimal number originated as an integer literal. Returns true iff the value was constructed from an integer-typed constructor or from a string source consisting of an optional sign followed by one or more digits, with no fractional part and no exponent part. Returns false for values constructed from floating-point primitives, from strings with a fractional or exponent part, or as the result of arithmetic operations.
|
nodiscard |
Check if the decimal number represents an integer value, which includes a number like 3.0. This is a value-level check: returns true whenever the number mathematically equals an integer, regardless of how it was originally written.
|
inlinenodiscard |
Check if the decimal number is NaN (Not a Number), either quiet or signaling
|
inlinenodiscard |
Check if the decimal number holds a value that is finite and that no integer equals, which is what is left of the real numbers once the integral ones are taken out of them
|
staticnodiscard |
Create a decimal from a double by converting through its shortest round-trip string representation, avoiding IEEE 754 precision artifacts
|
nodiscard |
Strip trailing zeros from the coefficient for as long as the exponent is not zero
| class sourcemeta::core::DecimalParseError |
This class represents a decimal parse error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::VarintParseError |
This class represents a variable-length integer parse error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::NumericDivisionByZeroError |
This class represents a numeric division by zero error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::NumericInvalidOperationError |
This class represents a numeric invalid operation error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::NumericOverflowError |
This class represents a numeric overflow error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::NumericOutOfRangeError |
This class represents a numeric value that falls outside the range of the target type
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| class sourcemeta::core::NumericOutOfMemoryError |
This class represents a numeric out of memory error
Additional Inherited Members | |
| Public Member Functions inherited from sourcemeta::core::Error | |
| auto | message () const noexcept -> StaticString |
| Protected Member Functions inherited from sourcemeta::core::Error | |
| Error (const StaticString message) noexcept | |
| Construct the error given its message. | |
| struct sourcemeta::core::uint128_t |
Cross-platform 128-bit unsigned integer type.
| auto sourcemeta::core::abs | ( | const T & | value | ) |
Compute the absolute value of an integer or Decimal
|
constexpr |
Find the smallest exponent in [exponent_start, exponent_end] such that base raised to the next power exceeds the given value, i.e. (base ^ exponent) <= value < (base ^ (exponent + 1))
|
constexpr |
Correct IEEE 754 floating-point imprecision by rounding values that are extremely close to the nearest integer
| auto sourcemeta::core::count_multiples | ( | const Minimum & | minimum, |
| const Maximum & | maximum, | ||
| const Multiplier & | multiplier ) |
Count the number of multiples of a given multiplier that fall within the closed range [minimum, maximum]. Supports primitive integers, Decimal operands, and mixed combinations.
| auto sourcemeta::core::divide_ceil | ( | const Dividend & | dividend, |
| const Divisor & | divisor ) |
Compute the ceiling of the division of two integral values. Supports primitive integers, Decimal operands, and mixed combinations.
| auto sourcemeta::core::divide_floor | ( | const Dividend & | dividend, |
| const Divisor & | divisor ) |
Compute the floor of the division of two integral values. Supports primitive integers, Decimal operands, and mixed combinations.
|
constexpr |
Check whether a value fits in an unsigned 8-bit byte
|
constexpr |
Check whether the given character is an ASCII decimal digit ('0'-'9'). For example:
|
constexpr |
Check whether the given character is a non-zero ASCII decimal digit ('1'-'9'). For example:
|
constexpr |
Check whether an integer is exactly one of the values an IEEE 754-2019 binary interchange format holds. Section 3.3 of that standard makes every finite value of such a format an integral significand scaled by a power of two. An integer scales by a non negative power of two whose leading bit sits no higher than the width of the integer itself, which every format here reaches, so the width of its odd part is all that can leave it outside. For example:
| auto sourcemeta::core::is_representable_as | ( | const Wider | value | ) | -> bool |
Check whether a floating-point value is exactly one of the values a narrower IEEE 754-2019 binary interchange format holds. The magnitude is settled before narrowing, as a conversion the narrower format cannot reach is not defined, and what returns from the round trip unchanged is what that format holds exactly. A value too small for it falls to zero and fails that comparison, while an infinity and every NaN belong to either format. For example:
|
inline |
Check whether a Decimal value falls within the closed range [lower, higher]
|
constexprnoexcept |
Check whether a value falls within the closed range [lower, higher] using signed 64-bit bounds
|
constexprnoexcept |
Check whether a value falls within the closed range [lower, higher] using unsigned 64-bit bounds
|
constexpr |
Extract the integer digits and decimal point position from a floating-point value. The point_position output indicates how many digits from the right the decimal point sits.
| auto sourcemeta::core::real_equal | ( | const Real | left, |
| const Real | right ) -> bool |
Compare two floating-point values for equality within a small tolerance of four units in the last place, which absorbs the rounding error a typical computation accumulates. A NaN is never equal to anything, and an infinity is equal only to the same infinity. For example:
| auto sourcemeta::core::to_decimal | ( | const T & | value | ) | -> Decimal |
Convert a value to a Decimal, returning a copy if it is already one
|
noexcept |
Attempt to parse a string as a double
|
noexcept |
Attempt to parse a fixed point decimal string as one of the values of the double precision interchange format of IEEE 754-2019, reporting no value unless the string denotes one of them exactly. A number the format does not hold, a string carrying an exponent, and a run of digits wider than a 64-bit accumulator each report no value, which leaves them to an arbitrary precision representation. For example:
|
noexcept |
Attempt to parse a string as a signed 64-bit integer
|
noexcept |
Attempt to parse a string as a signed 64-bit integer in a given base
|
noexcept |
Attempt to parse a string as an unsigned 16-bit decimal integer
|
noexcept |
Attempt to parse a string as an unsigned 32-bit decimal integer
|
noexcept |
Attempt to parse a string as an unsigned 32-bit integer in a given base
|
noexcept |
Attempt to parse a string as an unsigned 64-bit decimal integer.
|
inline |
Decode the given bytes, which must hold exactly one variable-length integer, back into an unsigned integer. For example:
|
inline |
Encode an unsigned integer as a variable-length integer, returning the number of bytes written to the destination, which must have room for the maximum size. For example:
| auto sourcemeta::core::zigzag_decode | ( | const T & | value | ) |
Decode a zigzag-encoded unsigned integer back into a signed integer
| auto sourcemeta::core::zigzag_encode | ( | const T & | value | ) |
Encode a signed integer into an unsigned integer using zigzag encoding, where the sign information is stored in the least significant bit
|
constexpr |
The maximum value representable by an unsigned integer of T bits
|
inlineconstexpr |
The number of bytes that the largest variable-length integer occupies