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 <string>
10
11namespace xrpl {
12
23class IOUAmount : private boost::totally_ordered<IOUAmount>, private boost::additive<IOUAmount>
24{
25private:
27 using exponent_type = int;
30
37 void
38 normalize();
39
40 static IOUAmount
41 fromNumber(Number const& number);
42
43public:
44 IOUAmount() = default;
45 explicit IOUAmount(Number const& other);
48
50
51 operator Number() const;
52
54 operator+=(IOUAmount const& other);
55
57 operator-=(IOUAmount const& other);
58
60 operator-() const;
61
62 bool
63 operator==(IOUAmount const& other) const;
64
65 bool
66 operator<(IOUAmount const& other) const;
67
69 explicit
70 operator bool() const noexcept;
71
73 [[nodiscard]] int
74 signum() const noexcept;
75
76 [[nodiscard]] exponent_type
77 exponent() const noexcept;
78
79 [[nodiscard]] mantissa_type
80 mantissa() const noexcept;
81
82 static IOUAmount
84
85 friend std::ostream&
86 operator<<(std::ostream& os, IOUAmount const& x)
87 {
88 return os << to_string(x);
89 }
90};
91
93{
94 *this = beast::kZero;
95}
96
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::
114operator Number() const
115{
116 return Number{mantissa_, exponent_};
117}
118
119inline IOUAmount&
121{
122 *this += -other;
123 return *this;
124}
125
126inline IOUAmount
128{
129 return {-mantissa_, exponent_};
130}
131
132inline bool
134{
135 return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
136}
137
138inline bool
139IOUAmount::operator<(IOUAmount const& other) const
140{
141 return Number{*this} < Number{other};
142}
143
144inline IOUAmount::
145operator bool() const noexcept
146{
147 return mantissa_ != 0;
148}
149
150inline int
151IOUAmount::signum() const noexcept
152{
153 if (mantissa_ < 0)
154 return -1;
155 return (mantissa_ != 0) ? 1 : 0;
156}
157
159IOUAmount::exponent() const noexcept
160{
161 return exponent_;
162}
163
165IOUAmount::mantissa() const noexcept
166{
167 return mantissa_;
168}
169
171to_string(IOUAmount const& amount);
172
173/* Return num*amt/den
174 This function keeps more precision than computing
175 num*amt, storing the result in an IOUAmount, then
176 dividing by den.
177*/
179mulRatio(IOUAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp);
180
181} // namespace xrpl
Floating point representation of amounts with high dynamic range.
Definition IOUAmount.h:24
IOUAmount & operator+=(IOUAmount const &other)
Definition IOUAmount.cpp:71
mantissa_type mantissa() const noexcept
Definition IOUAmount.h:165
exponent_type exponent_
Definition IOUAmount.h:29
bool operator==(IOUAmount const &other) const
Definition IOUAmount.h:133
IOUAmount()=default
exponent_type exponent() const noexcept
Definition IOUAmount.h:159
static IOUAmount fromNumber(Number const &number)
Definition IOUAmount.cpp:29
bool operator<(IOUAmount const &other) const
Definition IOUAmount.h:139
IOUAmount & operator=(beast::Zero)
Definition IOUAmount.h:104
std::int64_t mantissa_type
Definition IOUAmount.h:26
IOUAmount operator-() const
Definition IOUAmount.h:127
mantissa_type mantissa_
Definition IOUAmount.h:28
int signum() const noexcept
Return the sign of the amount.
Definition IOUAmount.h:151
static IOUAmount minPositiveAmount()
Definition IOUAmount.cpp:40
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition IOUAmount.cpp:46
IOUAmount & operator-=(IOUAmount const &other)
Definition IOUAmount.h:120
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
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:633
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition IOUAmount.cpp:93
Zero allows classes to offer efficient comparisons to zero.
Definition Zero.h:25