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