xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
protocol
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
12
namespace
xrpl
{
13
25
class
IOUAmount
:
private
boost::totally_ordered<IOUAmount>,
private
boost::additive<IOUAmount>
26
{
27
private
:
28
using
MantissaType
=
std::int64_t
;
29
using
ExponentType
= int;
30
MantissaType
mantissa_
{};
31
ExponentType
exponent_
{};
32
40
void
41
normalize
();
42
43
static
IOUAmount
44
fromNumber
(
Number
const
& number);
45
46
public
:
47
IOUAmount
() =
default
;
48
explicit
IOUAmount
(
Number
const
& other);
49
IOUAmount
(
beast::Zero
);
50
IOUAmount
(
MantissaType
mantissa
,
ExponentType
exponent
);
51
52
IOUAmount
&
operator=
(
beast::Zero
);
53
54
operator
Number
()
const
;
55
56
IOUAmount
&
57
operator+=
(
IOUAmount
const
& other);
58
59
IOUAmount
&
60
operator-=
(
IOUAmount
const
& other);
61
62
IOUAmount
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]]
ExponentType
84
exponent
() const noexcept;
85
86
[[nodiscard]]
MantissaType
87
mantissa
() const noexcept;
88
89
static
IOUAmount
90
minPositiveAmount
();
91
92
friend
std
::ostream&
93
operator<<(
std
::ostream& os,
IOUAmount
const& x)
94
{
95
return
os <<
to_string
(x);
96
}
97
};
98
99
inline
IOUAmount::IOUAmount
(
beast::Zero
)
100
{
101
*
this
=
beast::kZero
;
102
}
103
104
inline
IOUAmount::IOUAmount
(
MantissaType
mantissa
,
ExponentType
exponent
)
105
:
mantissa_
(
mantissa
),
exponent_
(
exponent
)
106
{
107
normalize
();
108
}
109
110
inline
IOUAmount
&
111
IOUAmount::operator=
(
beast::Zero
)
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
120
inline
IOUAmount::
121
operator
Number
()
const
122
{
123
return
Number
{
mantissa_
,
exponent_
};
124
}
125
126
inline
IOUAmount
&
127
IOUAmount::operator-=
(
IOUAmount
const
& other)
128
{
129
*
this
+= -other;
130
return
*
this
;
131
}
132
133
inline
IOUAmount
134
IOUAmount::operator-
()
const
135
{
136
return
{-
mantissa_
,
exponent_
};
137
}
138
139
inline
bool
140
IOUAmount::operator==
(
IOUAmount
const
& other)
const
141
{
142
return
exponent_
== other.
exponent_
&&
mantissa_
== other.
mantissa_
;
143
}
144
145
inline
bool
146
IOUAmount::operator<
(
IOUAmount
const
& other)
const
147
{
148
return
Number
{*
this
} <
Number
{other};
149
}
150
151
inline
IOUAmount::
152
operator bool() const noexcept
153
{
154
return
mantissa_
!= 0;
155
}
156
157
inline
int
158
IOUAmount::signum
() const noexcept
159
{
160
if
(
mantissa_
< 0)
161
return
-1;
162
return
(
mantissa_
!= 0) ? 1 : 0;
163
}
164
165
inline
IOUAmount::ExponentType
166
IOUAmount::exponent
() const noexcept
167
{
168
return
exponent_
;
169
}
170
171
inline
IOUAmount::MantissaType
172
IOUAmount::mantissa
() const noexcept
173
{
174
return
mantissa_
;
175
}
176
177
std::string
178
to_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
*/
185
IOUAmount
186
mulRatio
(
IOUAmount
const
& amt,
std::uint32_t
num,
std::uint32_t
den,
bool
roundUp);
187
188
}
// namespace xrpl
std::string
xrpl::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition
IOUAmount.h:26
xrpl::IOUAmount::operator+=
IOUAmount & operator+=(IOUAmount const &other)
Definition
libxrpl/protocol/IOUAmount.cpp:71
xrpl::IOUAmount::mantissa_
MantissaType mantissa_
Definition
IOUAmount.h:30
xrpl::IOUAmount::MantissaType
std::int64_t MantissaType
Definition
IOUAmount.h:28
xrpl::IOUAmount::operator==
bool operator==(IOUAmount const &other) const
Definition
IOUAmount.h:140
xrpl::IOUAmount::IOUAmount
IOUAmount()=default
xrpl::IOUAmount::fromNumber
static IOUAmount fromNumber(Number const &number)
Definition
libxrpl/protocol/IOUAmount.cpp:29
xrpl::IOUAmount::operator<
bool operator<(IOUAmount const &other) const
Definition
IOUAmount.h:146
xrpl::IOUAmount::exponent_
ExponentType exponent_
Definition
IOUAmount.h:31
xrpl::IOUAmount::operator=
IOUAmount & operator=(beast::Zero)
Definition
IOUAmount.h:111
xrpl::IOUAmount::mantissa
MantissaType mantissa() const noexcept
Definition
IOUAmount.h:172
xrpl::IOUAmount::exponent
ExponentType exponent() const noexcept
Definition
IOUAmount.h:166
xrpl::IOUAmount::operator-
IOUAmount operator-() const
Definition
IOUAmount.h:134
xrpl::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition
IOUAmount.h:158
xrpl::IOUAmount::minPositiveAmount
static IOUAmount minPositiveAmount()
Definition
libxrpl/protocol/IOUAmount.cpp:40
xrpl::IOUAmount::ExponentType
int ExponentType
Definition
IOUAmount.h:29
xrpl::IOUAmount::normalize
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition
libxrpl/protocol/IOUAmount.cpp:46
xrpl::IOUAmount::operator-=
IOUAmount & operator-=(IOUAmount const &other)
Definition
IOUAmount.h:127
xrpl::Number
Number is a floating point type that can represent a wide range of values.
Definition
Number.h:351
cstdint
std::int64_t
beast::kZero
constexpr Zero kZero
Definition
Zero.h:30
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::mulRatio
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition
libxrpl/protocol/IOUAmount.cpp:93
ostream
string
beast::Zero
Zero allows classes to offer efficient comparisons to zero.
Definition
Zero.h:26
Generated by
1.17.0