|
xrpld
|
Number is a floating point type that can represent a wide range of values. More...
#include <Number.h>

Classes | |
| struct | Unchecked |
| struct | Normalized |
| class | Guard |
| Guard. More... | |
Public Types | |
| enum class | RoundingMode { ToNearest , TowardsZero , Downward , Upward } |
Public Member Functions | |
| constexpr | Number ()=default |
| Number (rep mantissa) | |
| Number (rep mantissa, int exponent) | |
| constexpr | Number (bool negative, internalrep mantissa, int exponent, Unchecked) noexcept |
| constexpr | Number (internalrep mantissa, int exponent, Unchecked) noexcept |
| Number (bool negative, internalrep mantissa, int exponent, Normalized) | |
| Number (internalrep mantissa, int exponent, Normalized) | |
| constexpr rep | mantissa () const noexcept |
| Returns the mantissa of the external view of the Number. | |
| constexpr int | exponent () const noexcept |
| Returns the exponent of the external view of the Number. | |
| constexpr Number | operator+ () const noexcept |
| constexpr Number | operator- () const noexcept |
| Number & | operator++ () |
| Number | operator++ (int) |
| Number & | operator-- () |
| Number | operator-- (int) |
| Number & | operator+= (Number const &x) |
| Number & | operator-= (Number const &x) |
| Number & | operator*= (Number const &x) |
| Number & | operator/= (Number const &x) |
| operator rep () const | |
| Conversions to Number are implicit and conversions away from Number are explicit. | |
| constexpr int | signum () const noexcept |
| Return the sign of the amount. | |
| Number | truncate () const noexcept |
| template<auto MinMantissa, auto MaxMantissa, Integral64 T = std::decay_t<decltype(MinMantissa)>> | |
| std::pair< T, int > | normalizeToRange () const |
Static Public Member Functions | |
| static Number | min () noexcept |
| static Number | max () noexcept |
| static Number | lowest () noexcept |
| static RoundingMode | getround () |
| static RoundingMode | setround (RoundingMode inMode) |
| static MantissaRange::MantissaScale | getMantissaScale () |
| Returns which mantissa scale is currently in use for normalization. | |
| static void | setMantissaScale (MantissaRange::MantissaScale scale) |
| Changes which mantissa scale is used for normalization. | |
| static internalrep | minMantissa () |
| static internalrep | maxMantissa () |
| static int | mantissaLog () |
| static Number | one () |
| static internalrep | externalToInternal (rep mantissa) |
Static Public Attributes | |
| static constexpr int | kMinExponent = -32768 |
| static constexpr int | kMaxExponent = 32768 |
| static constexpr internalrep | kMaxRep = std::numeric_limits<rep>::max() |
| static constexpr internalrep | kMaxRepUp = ((kMaxRep / 10) + 1) * 10 |
Private Types | |
| using | rep = std::int64_t |
| using | internalrep = MantissaRange::rep |
Private Member Functions | |
| void | normalize (MantissaRange const &range) |
| void | normalize (Guard const &guard) |
| bool | isnormal () const noexcept |
| Number | shiftExponent (int exponentDelta) const |
Static Private Member Functions | |
| template<class T> | |
| 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. | |
| template<> | |
| void | normalize (bool &negative, uint128_t &mantissa, int &exponent, internalrep const &minMantissa, internalrep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix) |
| template<> | |
| void | normalize (bool &negative, unsigned long long &mantissa, int &exponent, internalrep const &minMantissa, internalrep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix) |
| template<> | |
| void | normalize (bool &negative, unsigned long &mantissa, int &exponent, internalrep const &minMantissa, internalrep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix) |
Private Attributes | |
| bool | negative_ {false} |
| internalrep | mantissa_ {0} |
| int | exponent_ {std::numeric_limits<int>::lowest()} |
Static Private Attributes | |
| static RoundingMode | mode = Number::RoundingMode::ToNearest |
| static std::reference_wrapper< MantissaRange const > | kRange |
Friends | |
| constexpr bool | operator== (Number const &x, Number const &y) noexcept |
| constexpr bool | operator< (Number const &l, Number const &r) noexcept |
| constexpr bool | operator> (Number const &x, Number const &y) noexcept |
| constexpr bool | operator<= (Number const &x, Number const &y) noexcept |
| constexpr bool | operator>= (Number const &x, Number const &y) noexcept |
| std::ostream & | operator<< (std::ostream &os, Number const &x) |
| std::string | to_string (Number const &amount) |
| Number | root (Number f, unsigned d) |
| Number | root2 (Number f) |
| template<class T> | |
| void | doNormalize (bool &negative, T &mantissa, int &exponent, MantissaRange::rep const &minMantissa, MantissaRange::rep const &maxMantissa, MantissaRange::CuspRoundingFix cuspRoundingFix, bool dropped) |
Number is a floating point type that can represent a wide range of values.
It can represent all values that can be represented by an STAmount - regardless of asset type - XRPAmount, MPTAmount, and IOUAmount, with at least as much precision as those types require.
-— Internal Representation -—
Internally, Number is represented with three values:
The internal mantissa is an unsigned integer in the range defined by the current MantissaRange. The exponent is an integer in the range [minExponent, maxExponent].
See the description of MantissaRange for more details on the ranges.
A non-zero mantissa is (almost) always normalized, meaning it and the exponent are grown or shrunk until the mantissa is in the range [MantissaRange.min, MantissaRange.max].
Note:
-— External Interface -—
The external interface of Number consists of a std::int64_t mantissa, which is restricted to 63-bits, and an int exponent, which must be in the range [minExponent, maxExponent]. The range of the mantissa depends on which MantissaRange is currently active. For the "short" range, the mantissa will be between 10^15 and 10^16-1. For the "large" range, the mantissa will be between -(2^63-1) and 2^63-1. As noted above, the "large" range is needed to represent the full range of valid XRP and MPT integer values accurately.
Note:
-— Mantissa Range Switching -—
The mantissa range may be changed at runtime via setMantissaScale(). The default mantissa range is "large". The range is updated whenever transaction processing begins, based on whether SingleAssetVault or LendingProtocol are enabled. If either is enabled, the mantissa range is set to "large". If not, it is set to "small", preserving backward compatibility and correct "amendment-gating".
It is extremely unlikely that any more calls to setMantissaScale() will be needed outside of unit tests.
-— Usage With Different Ranges -—
Outside of unit tests, and existing checks, code that uses Number should not know or care which mantissa range is active.
The results of computations using Numbers with a small mantissa may differ from computations using Numbers with a large mantissa, specifically as it effects the results after rounding. That is why the large mantissa range is amendment gated in transaction processing.
It is extremely unlikely that any more calls to getMantissaScale() will be needed outside of unit tests.
Code that uses Number should not assume or check anything about the mantissa() or exponent() except that they fit into the "large" range specified in the "External Interface" section.
--— Unit Tests --—
Within unit tests, it may be useful to explicitly switch between the two ranges, or to check which range is active when checking the results of computations. If the test is doing the math directly, the set/getMantissaScale() functions may be most appropriate. However, if the test has anything to do with transaction processing, it should enable or disable the amendments that control the mantissa range choice (SingleAssetVault and LendingProtocol), and/or check if either of those amendments are enabled to determine which result to expect.
|
private |
|
private |
|
strong |
|
explicitconstexprdefault |
|
explicit |
|
explicitconstexprnoexcept |
|
explicitconstexprnoexcept |
|
explicit |
|
explicit |
|
nodiscardconstexprnoexcept |
|
nodiscardconstexprnoexcept |
Definition at line 879 of file libxrpl/basics/Number.cpp.
Definition at line 1087 of file libxrpl/basics/Number.cpp.
Definition at line 1141 of file libxrpl/basics/Number.cpp.
|
explicit |
Conversions to Number are implicit and conversions away from Number are explicit.
This design encourages and facilitates the use of Number as the preferred type for floating point arithmetic as it makes "mixed mode" more convenient, e.g. MPTAmount + Number.
Definition at line 1320 of file libxrpl/basics/Number.cpp.
|
nodiscardconstexprnoexcept |
|
nodiscardnoexcept |
Definition at line 1349 of file libxrpl/basics/Number.cpp.
|
static |
Definition at line 133 of file libxrpl/basics/Number.cpp.
|
static |
Definition at line 139 of file libxrpl/basics/Number.cpp.
|
static |
Returns which mantissa scale is currently in use for normalization.
If you think you need to call this outside of unit tests, no you don't.
Definition at line 145 of file libxrpl/basics/Number.cpp.
|
static |
Changes which mantissa scale is used for normalization.
If you think you need to call this outside of unit tests, no you don't.
Definition at line 151 of file libxrpl/basics/Number.cpp.
|
static |
|
static |
|
static |
Definition at line 703 of file libxrpl/basics/Number.cpp.
|
nodiscard |
|
static |
Definition at line 684 of file libxrpl/basics/Number.cpp.
|
private |
Definition at line 842 of file libxrpl/basics/Number.cpp.
|
private |
Definition at line 848 of file libxrpl/basics/Number.cpp.
|
staticprivate |
Normalize Number components to an arbitrary range.
min/maxMantissa are parameters because this function is used by both normalize(), which reads from kRange, and by normalizeToRange, which is public and can accept an arbitrary range from the caller.
|
nodiscardprivatenoexcept |
|
nodiscardprivate |
Definition at line 863 of file libxrpl/basics/Number.cpp.
|
staticprivate |
Definition at line 795 of file libxrpl/basics/Number.cpp.
|
staticprivate |
Definition at line 812 of file libxrpl/basics/Number.cpp.
|
staticprivate |
Definition at line 829 of file libxrpl/basics/Number.cpp.
|
friend |
|
friend |
Definition at line 1367 of file libxrpl/basics/Number.cpp.
Definition at line 1491 of file libxrpl/basics/Number.cpp.
Definition at line 1565 of file libxrpl/basics/Number.cpp.
|
friend |
Definition at line 711 of file libxrpl/basics/Number.cpp.
|
private |
|
private |
|
staticconstexpr |
|
staticconstexpr |
|
thread_localstaticprivate |
|
thread_localstaticprivate |