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
98inline IOUAmount::IOUAmount(mantissa_type mantissa, exponent_type exponent) : mantissa_(mantissa), exponent_(exponent)
99{
100 normalize();
101}
102
103inline IOUAmount&
105{
106 // The -100 is used to allow 0 to sort less than small positive values
107 // which will have a large negative exponent.
108 mantissa_ = 0;
109 exponent_ = -100;
110 return *this;
111}
112
113inline IOUAmount::operator Number() const
114{
115 return Number{mantissa_, exponent_};
116}
117
118inline IOUAmount&
120{
121 *this += -other;
122 return *this;
123}
124
125inline IOUAmount
127{
128 return {-mantissa_, exponent_};
129}
130
131inline bool
133{
134 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
135}
136
137inline bool
138IOUAmount::operator<(IOUAmount const& other) const
139{
140 return Number{*this} < Number{other};
141}
142
143inline IOUAmount::operator bool() const noexcept
144{
145 return mantissa_ != 0;
146}
147
148inline int
149IOUAmount::signum() const noexcept
150{
151 return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
152}
153
155IOUAmount::exponent() const noexcept
156{
157 return exponent_;
158}
159
161IOUAmount::mantissa() const noexcept
162{
163 return mantissa_;
164}
165
167to_string(IOUAmount const& amount);
168
169/* Return num*amt/den
170 This function keeps more precision than computing
171 num*amt, storing the result in an IOUAmount, then
172 dividing by den.
173*/
175mulRatio(IOUAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp);
176
177// Since many uses of the number class do not have access to a ledger,
178// getSTNumberSwitchover needs to be globally accessible.
179
180bool
182
183void
185
190{
191 bool saved_;
192
193public:
198
199 NumberSO(NumberSO const&) = delete;
200 NumberSO&
201 operator=(NumberSO const&) = delete;
202
204 {
206 }
207};
208
209} // 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:161
exponent_type exponent_
Definition IOUAmount.h:30
bool operator==(IOUAmount const &other) const
Definition IOUAmount.h:132
IOUAmount()=default
exponent_type exponent() const noexcept
Definition IOUAmount.h:155
static IOUAmount fromNumber(Number const &number)
Definition IOUAmount.cpp:55
bool operator<(IOUAmount const &other) const
Definition IOUAmount.h:138
IOUAmount & operator=(beast::Zero)
Definition IOUAmount.h:104
IOUAmount operator-() const
Definition IOUAmount.h:126
mantissa_type mantissa_
Definition IOUAmount.h:29
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:149
static IOUAmount minPositiveAmount()
Definition IOUAmount.cpp:65
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition IOUAmount.cpp:71
IOUAmount & operator-=(IOUAmount const &other)
Definition IOUAmount.h:119
RAII class to set and restore the Number switchover.
Definition IOUAmount.h:190
NumberSO & operator=(NumberSO const &)=delete
NumberSO(bool v)
Definition IOUAmount.h:203
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:597
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