xrpld
Loading...
Searching...
No Matches
IOUAmount.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/utility/Zero.h>
5
6#include <boost/operators.hpp>
7
8#include <cstdint>
9#include <ostream>
10#include <string>
11
12namespace xrpl {
13
25class IOUAmount : private boost::totally_ordered<IOUAmount>, private boost::additive<IOUAmount>
26{
27private:
29 using exponent_type = int;
32
40 void
41 normalize();
42
43 static IOUAmount
44 fromNumber(Number const& number);
45
46public:
47 IOUAmount() = default;
48 explicit IOUAmount(Number const& other);
51
53
54 operator Number() const;
55
57 operator+=(IOUAmount const& other);
58
60 operator-=(IOUAmount const& other);
61
63 operator-() const;
64
65 bool
66 operator==(IOUAmount const& other) const;
67
68 bool
69 operator<(IOUAmount const& other) const;
70
74 explicit
75 operator bool() const noexcept;
76
80 [[nodiscard]] int
81 signum() const noexcept;
82
83 [[nodiscard]] exponent_type
84 exponent() const noexcept;
85
86 [[nodiscard]] mantissa_type
87 mantissa() const noexcept;
88
89 static IOUAmount
91
92 friend std::ostream&
93 operator<<(std::ostream& os, IOUAmount const& x)
94 {
95 return os << to_string(x);
96 }
97};
98
100{
101 *this = beast::kZero;
102}
103
109
110inline IOUAmount&
112{
113 // The -100 is used to allow 0 to sort less than small positive values
114 // which will have a large negative exponent.
115 mantissa_ = 0;
116 exponent_ = -100;
117 return *this;
118}
119
120inline IOUAmount::
121operator Number() const
122{
123 return Number{mantissa_, exponent_};
124}
125
126inline IOUAmount&
128{
129 *this += -other;
130 return *this;
131}
132
133inline IOUAmount
135{
136 return {-mantissa_, exponent_};
137}
138
139inline bool
141{
142 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
143}
144
145inline bool
146IOUAmount::operator<(IOUAmount const& other) const
147{
148 return Number{*this} < Number{other};
149}
150
151inline IOUAmount::
152operator bool() const noexcept
153{
154 return mantissa_ != 0;
155}
156
157inline int
158IOUAmount::signum() const noexcept
159{
160 if (mantissa_ < 0)
161 return -1;
162 return (mantissa_ != 0) ? 1 : 0;
163}
164
166IOUAmount::exponent() const noexcept
167{
168 return exponent_;
169}
170
172IOUAmount::mantissa() const noexcept
173{
174 return mantissa_;
175}
176
178to_string(IOUAmount const& amount);
179
180/* Return num*amt/den
181 This function keeps more precision than computing
182 num*amt, storing the result in an IOUAmount, then
183 dividing by den.
184*/
186mulRatio(IOUAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp);
187
188} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:26
IOUAmount & operator+=(IOUAmount const &other)
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:172
exponent_type exponent_
Definition IOUAmount.h:31
bool operator==(IOUAmount const &other) const
Definition IOUAmount.h:140
IOUAmount()=default
exponent_type exponent() const noexcept
Definition IOUAmount.h:166
static IOUAmount fromNumber(Number const &number)
bool operator<(IOUAmount const &other) const
Definition IOUAmount.h:146
IOUAmount & operator=(beast::Zero)
Definition IOUAmount.h:111
std::int64_t mantissa_type
Definition IOUAmount.h:28
IOUAmount operator-() const
Definition IOUAmount.h:134
mantissa_type mantissa_
Definition IOUAmount.h:30
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:158
static IOUAmount minPositiveAmount()
void normalize()
Adjusts the mantissa and exponent to the proper range.
IOUAmount & operator-=(IOUAmount const &other)
Definition IOUAmount.h:127
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
constexpr Zero kZero
Definition Zero.h:30
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:26