rippled
Loading...
Searching...
No Matches
IOUAmount.h
1#pragma once
2
3#include <xrpl/basics/LocalValue.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6
7#include <boost/operators.hpp>
8
9#include <cstdint>
10#include <string>
11
12namespace xrpl {
13
24class IOUAmount : private boost::totally_ordered<IOUAmount>, private boost::additive<IOUAmount>
25{
26private:
28 using exponent_type = int;
31
38 void
39 normalize();
40
41 static IOUAmount
42 fromNumber(Number const& number);
43
44public:
45 IOUAmount() = default;
46 explicit IOUAmount(Number const& other);
49
51
52 operator Number() const;
53
55 operator+=(IOUAmount const& other);
56
58 operator-=(IOUAmount const& other);
59
61 operator-() const;
62
63 bool
64 operator==(IOUAmount const& other) const;
65
66 bool
67 operator<(IOUAmount const& other) const;
68
70 explicit
71 operator bool() const noexcept;
72
74 int
75 signum() const noexcept;
76
78 exponent() const noexcept;
79
81 mantissa() const noexcept;
82
83 static IOUAmount
85
86 friend std::ostream&
87 operator<<(std::ostream& os, IOUAmount const& x)
88 {
89 return os << to_string(x);
90 }
91};
92
94{
95 *this = beast::zero;
96}
97
99 : mantissa_(mantissa), exponent_(exponent)
100{
101 normalize();
102}
103
104inline IOUAmount&
106{
107 // The -100 is used to allow 0 to sort less than small positive values
108 // which will have a large negative exponent.
109 mantissa_ = 0;
110 exponent_ = -100;
111 return *this;
112}
113
114inline IOUAmount::
115operator Number() const
116{
117 return Number{mantissa_, exponent_};
118}
119
120inline IOUAmount&
122{
123 *this += -other;
124 return *this;
125}
126
127inline IOUAmount
129{
130 return {-mantissa_, exponent_};
131}
132
133inline bool
135{
136 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
137}
138
139inline bool
140IOUAmount::operator<(IOUAmount const& other) const
141{
142 return Number{*this} < Number{other};
143}
144
145inline IOUAmount::
146operator bool() const noexcept
147{
148 return mantissa_ != 0;
149}
150
151inline int
152IOUAmount::signum() const noexcept
153{
154 return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
155}
156
158IOUAmount::exponent() const noexcept
159{
160 return exponent_;
161}
162
164IOUAmount::mantissa() const noexcept
165{
166 return mantissa_;
167}
168
170to_string(IOUAmount const& amount);
171
172/* Return num*amt/den
173 This function keeps more precision than computing
174 num*amt, storing the result in an IOUAmount, then
175 dividing by den.
176*/
178mulRatio(IOUAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp);
179
180// Since many uses of the number class do not have access to a ledger,
181// getSTNumberSwitchover needs to be globally accessible.
182
183bool
185
186void
188
193{
194 bool saved_;
195
196public:
201
202 NumberSO(NumberSO const&) = delete;
203 NumberSO&
204 operator=(NumberSO const&) = delete;
205
207 {
209 }
210};
211
212} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:25
IOUAmount & operator+=(IOUAmount const &other)
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:164
exponent_type exponent_
Definition IOUAmount.h:30
bool operator==(IOUAmount const &other) const
Definition IOUAmount.h:134
IOUAmount()=default
exponent_type exponent() const noexcept
Definition IOUAmount.h:158
static IOUAmount fromNumber(Number const &number)
Definition IOUAmount.cpp:55
bool operator<(IOUAmount const &other) const
Definition IOUAmount.h:140
IOUAmount & operator=(beast::Zero)
Definition IOUAmount.h:105
IOUAmount operator-() const
Definition IOUAmount.h:128
mantissa_type mantissa_
Definition IOUAmount.h:29
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:152
static IOUAmount minPositiveAmount()
Definition IOUAmount.cpp:66
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition IOUAmount.cpp:72
IOUAmount & operator-=(IOUAmount const &other)
Definition IOUAmount.h:121
RAII class to set and restore the Number switchover.
Definition IOUAmount.h:193
NumberSO & operator=(NumberSO const &)=delete
NumberSO(bool v)
Definition IOUAmount.h:206
NumberSO(NumberSO const &)=delete
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
void setSTNumberSwitchover(bool v)
Definition IOUAmount.cpp:40
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
bool getSTNumberSwitchover()
Definition IOUAmount.cpp:34
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:25