xrpld
Loading...
Searching...
No Matches
xrpl::MantissaRange Struct Referencefinal

MantissaRange defines a range for the mantissa of a normalized Number. More...

#include <Number.h>

Collaboration diagram for xrpl::MantissaRange:

Public Types

enum class  MantissaScale { Small , LargeLegacy , Large }
enum class  CuspRoundingFix : bool { Disabled = false , Enabled = true }
using rep = std::uint64_t

Public Member Functions

constexpr MantissaRange (MantissaScale sc)

Static Public Member Functions

static MantissaRange const & getMantissaRange (MantissaScale scale)
static std::set< MantissaScale > const & getAllScales ()

Public Attributes

MantissaScale const scale
int const log {getExponent(scale)}
rep const min {getMin(scale, log)}
rep const max {(min * 10) - 1}
CuspRoundingFix const cuspRoundingFixEnabled {isCuspFixEnabled(scale)}

Static Private Member Functions

static constexpr int getExponent (MantissaScale scale)
static constexpr rep getMin (MantissaScale scale, int exponent)
static constexpr CuspRoundingFix isCuspFixEnabled (MantissaScale scale)
static std::unordered_map< MantissaScale, MantissaRange > const & getRanges ()

Detailed Description

MantissaRange defines a range for the mantissa of a normalized Number.

The mantissa is in the range [min, max], where

  • min is a power of 10, and
  • max = min * 10 - 1.

The MantissaScale enum indicates properties of the range: size, and some behavioral options. This intentionally restricts the number of unique MantissaRanges that can be instantiated: one for each scale.

The "Small" scale is based on the behavior of STAmount for IOUs. It has a min value of 10^15, and a max value of 10^16-1. This was sufficient for uses before Lending Protocol was implemented, mostly related to AMM.

However, it does not have sufficient precision to represent the full integer range of int64_t values (-2^63 to 2^63-1), which are needed for XRP and MPT values. The implementation of SingleAssetVault, and LendingProtocol need to represent those integer values accurately and precisely, both for the STNumber field type, and for internal calculations. That necessitated the "large" scale.

The "Large" scales are intended to represent all values that can be represented by an STAmount - IOUs, XRP, and MPTs. It has a min value of 10^18, and a max value of 10^19-1. "LargeLegacy" is like "Large", but preserves a rounding error when a computation results in a mantissa of Number::kMaxRep that needs to be rounded up, but rounds down instead. It will maintain consistent behavior until the fixCleanup3_2_0 amendment is enabled.

Note that if the mentioned amendments are eventually retired, this class should be left in place, but the "Small" scale option should be removed. This will allow for future expansion beyond 64-bits if it is ever needed.

Definition at line 119 of file Number.h.

Member Typedef Documentation

◆ rep

Definition at line 121 of file Number.h.

Member Enumeration Documentation

◆ MantissaScale

Enumerator
Small 
LargeLegacy 
Large 

Definition at line 123 of file Number.h.

◆ CuspRoundingFix

enum class xrpl::MantissaRange::CuspRoundingFix : bool
strong
Enumerator
Disabled 
Enabled 

Definition at line 131 of file Number.h.

Constructor & Destructor Documentation

◆ MantissaRange()

xrpl::MantissaRange::MantissaRange ( MantissaScale sc)
explicitconstexpr

Definition at line 136 of file Number.h.

Member Function Documentation

◆ getMantissaRange()

MantissaRange const & xrpl::MantissaRange::getMantissaRange ( MantissaScale scale)
static

Definition at line 99 of file Number.cpp.

◆ getAllScales()

std::set< MantissaRange::MantissaScale > const & xrpl::MantissaRange::getAllScales ( )
static

Definition at line 37 of file Number.cpp.

◆ getExponent()

constexpr int xrpl::MantissaRange::getExponent ( MantissaScale scale)
staticconstexprprivate

Definition at line 154 of file Number.h.

◆ getMin()

constexpr rep xrpl::MantissaRange::getMin ( MantissaScale scale,
int exponent )
staticconstexprprivate

Definition at line 175 of file Number.h.

◆ isCuspFixEnabled()

constexpr CuspRoundingFix xrpl::MantissaRange::isCuspFixEnabled ( MantissaScale scale)
staticconstexprprivate

Definition at line 183 of file Number.h.

◆ getRanges()

std::unordered_map< MantissaRange::MantissaScale, MantissaRange > const & xrpl::MantissaRange::getRanges ( )
staticprivate

Definition at line 48 of file Number.cpp.

Member Data Documentation

◆ scale

MantissaScale const xrpl::MantissaRange::scale

Definition at line 140 of file Number.h.

◆ log

int const xrpl::MantissaRange::log {getExponent(scale)}

Definition at line 141 of file Number.h.

◆ min

rep const xrpl::MantissaRange::min {getMin(scale, log)}

Definition at line 142 of file Number.h.

◆ max

rep const xrpl::MantissaRange::max {(min * 10) - 1}

Definition at line 143 of file Number.h.

◆ cuspRoundingFixEnabled

CuspRoundingFix const xrpl::MantissaRange::cuspRoundingFixEnabled {isCuspFixEnabled(scale)}

Definition at line 144 of file Number.h.