3#include <xrpl/beast/utility/instrumentation.h>
27constexpr std::optional<int>
31 while (value >= 10 && value % 10 == 0)
61template <
typename T, std::
size_t Digits>
70 for (; exponent < result.
size() - 1; ++exponent,
power *= 10)
72 result[exponent] =
power;
76 result[exponent] =
power;
78 throw std::logic_error(
"Power of 10 table is not big enough for the given type");
85template <
typename T = std::u
int64_t, std::
size_t Digits = detail::kU
int64Digits>
223 if (exponent < 0 || exponent >=
kPowerOfTen.size())
365 static_assert(
kMaxRep == 9'223'372'036'854'775'807);
368 static_assert(
kMaxRepUp == 9'223'372'036'854'775'810ULL);
389 explicit constexpr Number(
401 [[nodiscard]]
constexpr rep
403 [[nodiscard]] constexpr
int
407 operator+() const noexcept;
409 operator-() const noexcept;
420 operator+=(
Number const& x);
422 operator-=(
Number const& x);
425 operator*=(
Number const& x);
427 operator/=(
Number const& x);
443 operator
rep() const;
445 friend constexpr
bool
448 return x.negative_ == y.negative_ && x.mantissa_ == y.mantissa_ &&
449 x.exponent_ == y.exponent_;
452 friend constexpr bool
455 bool const lneg = l.negative_;
456 bool const rneg = r.negative_;
465 if (l.mantissa_ == 0)
466 return r.mantissa_ > 0;
470 if (r.mantissa_ == 0)
474 if (l.exponent_ > r.exponent_)
476 if (l.exponent_ < r.exponent_)
483 return l.mantissa_ > r.mantissa_;
486 return l.mantissa_ < r.mantissa_;
492 [[nodiscard]]
constexpr int
503 friend constexpr
bool
509 friend constexpr bool
515 friend constexpr bool
699 "xrpl::Number::mantissa",
700 "large normalized mantissa has no remainder");
721 "xrpl::Number::exponent",
722 "large normalized mantissa has no remainder");
740 x.negative_ = !x.negative_;
835 return *
this ==
Number{} ||
836 (
range.min <= absM && absM <=
range.max && (absM <=
kMaxRep || absM % 10 == 0) &&
840template <auto MinMantissa, auto MaxMantissa, Integral64 T>
847 auto constexpr kMIN =
static_cast<T
>(MinMantissa);
848 auto constexpr kMAX =
static_cast<T
>(MaxMantissa);
849 static_assert(kMIN > 0);
850 static_assert(kMIN % 10 == 0);
852 static_assert(kMAX % 10 == 9);
853 static_assert((kMAX + 1) / 10 == kMIN);
863 "xrpl::Number::normalizeToRange",
864 "Number is non-negative for unsigned range.");
871 auto const sign = negative ? -1 : 1;
887power(Number
const& f,
unsigned n);
894root(Number f,
unsigned d);
902power(Number
const& f,
unsigned n,
unsigned d);
static constexpr MantissaRange const & mantissaRange(MantissaScale scale)
NumberMantissaScaleGuard(NumberMantissaScaleGuard const &)=delete
MantissaRange::MantissaScale const saved_
~NumberMantissaScaleGuard()
NumberMantissaScaleGuard & operator=(NumberMantissaScaleGuard const &)=delete
NumberMantissaScaleGuard(MantissaRange::MantissaScale scale) noexcept
NumberRoundModeGuard(Number::RoundingMode mode) noexcept
SaveNumberRoundMode saved_
NumberRoundModeGuard & operator=(NumberRoundModeGuard const &)=delete
NumberRoundModeGuard(NumberRoundModeGuard const &)=delete
Number is a floating point type that can represent a wide range of values.
constexpr rep mantissa() const noexcept
Returns the mantissa of the external view of the Number.
std::pair< T, int > normalizeToRange() const
static InternalRep maxMantissa()
MantissaRange::rep InternalRep
static constexpr InternalRep kMaxRepUp
Number truncate() const noexcept
friend std::string to_string(Number const &amount)
friend constexpr bool operator<(Number const &l, Number const &r) noexcept
static constexpr int kMinExponent
static RoundingMode setround(RoundingMode inMode)
static constexpr InternalRep kMaxRep
friend void doNormalize(bool &negative, T &mantissa, int &exponent, MantissaRange::rep const &minMantissa, MantissaRange::rep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix, bool dropped)
constexpr Number operator-() const noexcept
static Number max() noexcept
static RoundingMode getround()
static std::reference_wrapper< MantissaRange const > kRange
static void normalize(bool &negative, T &mantissa, int &exponent, InternalRep const &minMantissa, InternalRep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix)
Normalize Number components to an arbitrary range.
constexpr Number operator+() const noexcept
Number shiftExponent(int exponentDelta) const
static MantissaRange::MantissaScale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
friend constexpr bool operator>=(Number const &x, Number const &y) noexcept
static InternalRep minMantissa()
static constexpr int kMaxExponent
bool isnormal() const noexcept
constexpr int exponent() const noexcept
Returns the exponent of the external view of the Number.
friend std::ostream & operator<<(std::ostream &os, Number const &x)
friend Number root2(Number f)
constexpr int signum() const noexcept
Return the sign of the amount.
Number & operator-=(Number const &x)
static InternalRep externalToInternal(rep mantissa)
static Number min() noexcept
void normalize(MantissaRange const &range)
constexpr Number()=default
static void setMantissaScale(MantissaRange::MantissaScale scale)
Changes which mantissa scale is used for normalization.
static Number lowest() noexcept
friend constexpr bool operator<=(Number const &x, Number const &y) noexcept
friend Number root(Number f, unsigned d)
SaveNumberRoundMode(SaveNumberRoundMode const &)=delete
SaveNumberRoundMode & operator=(SaveNumberRoundMode const &)=delete
SaveNumberRoundMode(Number::RoundingMode mode) noexcept
Number::RoundingMode mode_
constexpr std::size_t kUint64Digits
Builds a table of the powers of 10.
consteval std::array< T, Digits > buildPowersOfTen()
constexpr std::size_t kUint128Digits
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static constexpr Number kNumZero
constexpr BaseUInt< Bits, Tag > operator+(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
Number operator-(Number const &x, Number const &y)
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Number operator/(Number const &x, Number const &y)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
constexpr std::array< T, Digits > kPowerOfTenImpl
Number root(Number f, unsigned d)
Number power(Number const &f, unsigned n)
Number operator*(Number const &x, Number const &y)
std::string to_string(BaseUInt< Bits, Tag > const &a)
constexpr std::optional< int > logTen(T value)
constexpr auto kPowerOfTen
constexpr bool isPowerOfTen(T value)
constexpr Number abs(Number x) noexcept
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
constexpr Number squelch(Number const &x, Number const &limit) noexcept
MantissaRange defines a range for the mantissa of a normalized Number.
MantissaScale const scale
static constexpr int getExponent(MantissaScale scale)
CuspRoundingFix const cuspRoundingFix
constexpr MantissaRange(MantissaScale sc)
static constexpr rep getMin(MantissaScale scale, int exponent)
static constexpr CuspRoundingFix isCuspFixEnabled(MantissaScale scale)
static std::set< MantissaScale > const & getAllScales()