1#include <xrpl/protocol/STNumber.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/Asset.h>
8#include <xrpl/protocol/Rules.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STBase.h>
12#include <xrpl/protocol/STTakesAsset.h>
13#include <xrpl/protocol/Serializer.h>
15#include <boost/lexical_cast.hpp>
16#include <boost/regex/v5/regbase.hpp>
17#include <boost/regex/v5/regex.hpp>
18#include <boost/regex/v5/regex_match.hpp>
38 auto mantissa = sit.
geti64();
39 auto exponent = sit.
geti32();
62 "STNumber::associateAsset",
71 XRPL_ASSERT(
getFName().isBinary(),
"xrpl::STNumber::add : field is binary");
72 XRPL_ASSERT(
getFName().fieldType ==
getSType(),
"xrpl::STNumber::add : field type match");
75 auto const mantissa =
value.mantissa();
76 auto const exponent =
value.exponent();
87 XRPL_ASSERT_PARTS(
value_ ==
value,
"xrpl::STNumber::add",
"value is already rounded");
101 "xrpl::STNumber::add",
102 "STNumber only used with large mantissa scale");
110 "xrpl::STNumber::add",
111 "mantissa in valid range");
137 return emplace(n, buf, std::move(*
this));
144 t.
getSType() == this->getSType(),
"xrpl::STNumber::isEquivalent : field type match");
164 static boost::regex
const kReNumber(
169 "([eE]([+-]?)([0-9]+))?"
171 boost::regex_constants::optimize);
175 if (!boost::regex_match(number, match, kReNumber))
188 bool const negative = (match[1].matched && (match[1] ==
"-"));
193 if (!match[4].matched)
195 mantissa = boost::lexical_cast<std::uint64_t>(
std::string(match[2]));
201 mantissa = boost::lexical_cast<std::uint64_t>(match[2] + match[4]);
202 exponent = -(match[4].length());
205 if (match[5].matched)
208 if (match[6].matched && (match[6] ==
"-"))
210 exponent -= boost::lexical_cast<int>(
std::string(match[7]));
214 exponent += boost::lexical_cast<int>(
std::string(match[7]));
218 return {.mantissa = mantissa, .exponent = exponent, .negative = negative};
228 if (value.asInt() >= 0)
238 else if (value.isUInt())
242 else if (value.isString())
Number is a floating point type that can represent a wide range of values.
static MantissaRange::MantissaScale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
static constexpr auto kSmdNeedsAsset
A type which can be exported to a well known binary format.
SField const & getFName() const
static STBase * emplace(std::size_t n, void *buf, T &&val)
virtual SerializedTypeID getSType() const
void add(Serializer &s) const override
bool isDefault() const override
void associateAsset(Asset const &a) override
void setValue(Number const &v)
std::string getText() const override
bool isEquivalent(STBase const &t) const override
STBase * move(std::size_t n, void *buf) override
SerializedTypeID getSType() const override
STBase * copy(std::size_t n, void *buf) const override
Number const & value() const
Intermediate class for any STBase-derived class to store an Asset.
virtual void associateAsset(Asset const &a)
std::optional< Asset > asset_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
NumberParts partsFromString(std::string const &number)
std::optional< Rules > const & getCurrentTransactionRules()
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
std::string to_string(BaseUInt< Bits, Tag > const &a)
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
STNumber numberFromJson(SField const &field, json::Value const &value)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)