1#include <xrpl/protocol/STNumber.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/core/LexicalCast.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/protocol/Rules.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STAmount.h>
9#include <xrpl/protocol/STBase.h>
10#include <xrpl/protocol/STIssue.h>
11#include <xrpl/protocol/Serializer.h>
13#include <boost/lexical_cast.hpp>
14#include <boost/regex.hpp>
31 auto mantissa = sit.
geti64();
32 auto exponent = sit.
geti32();
61 XRPL_ASSERT(
getFName().isBinary(),
"xrpl::STNumber::add : field is binary");
62 XRPL_ASSERT(
getFName().fieldType ==
getSType(),
"xrpl::STNumber::add : field type match");
77 XRPL_ASSERT_PARTS(
value_ ==
value,
"xrpl::STNumber::add",
"value is already rounded");
90 "xrpl::STNumber::add",
91 "STNumber only used with large mantissa scale");
98 "xrpl::STNumber::add",
99 "mantissa in valid range");
125 return emplace(n, buf, std::move(*
this));
131 XRPL_ASSERT(t.
getSType() == this->getSType(),
"xrpl::STNumber::isEquivalent : field type match");
151 static boost::regex
const reNumber(
156 "([eE]([+-]?)([0-9]+))?"
158 boost::regex_constants::optimize);
162 if (!boost::regex_match(number,
match, reNumber))
163 Throw<std::runtime_error>(
"'" + number +
"' is not a number");
175 bool negative = (
match[1].matched && (
match[1] ==
"-"));
180 if (!
match[4].matched)
188 mantissa = boost::lexical_cast<std::uint64_t>(
match[2] +
match[4]);
189 exponent = -(
match[4].length());
192 if (
match[5].matched)
201 return {mantissa, exponent, negative};
211 if (value.
asInt() >= 0)
238 Throw<std::runtime_error>(
"not a number");
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
std::string asString() const
Returns the unquoted string value.
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.
constexpr int exponent() const noexcept
Returns the exponent of the external view of the Number.
static MantissaRange::mantissa_scale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
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::string to_string(base_uint< Bits, Tag > const &a)
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
std::optional< Rules > const & getCurrentTransactionRules()
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)