rippled
IOUAmount.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_BASICS_IOUAMOUNT_H_INCLUDED
21 #define RIPPLE_BASICS_IOUAMOUNT_H_INCLUDED
22 
23 #include <ripple/basics/LocalValue.h>
24 #include <ripple/basics/Number.h>
25 #include <ripple/beast/utility/Zero.h>
26 #include <boost/operators.hpp>
27 #include <cstdint>
28 #include <string>
29 #include <utility>
30 
31 namespace ripple {
32 
43 class IOUAmount : private boost::totally_ordered<IOUAmount>,
44  private boost::additive<IOUAmount>
45 {
46 private:
48  int exponent_;
49 
56  void
57  normalize();
58 
59 public:
60  IOUAmount() = default;
61  explicit IOUAmount(Number const& other);
64 
66 
67  operator Number() const;
68 
69  IOUAmount&
70  operator+=(IOUAmount const& other);
71 
72  IOUAmount&
73  operator-=(IOUAmount const& other);
74 
75  IOUAmount
76  operator-() const;
77 
78  bool
79  operator==(IOUAmount const& other) const;
80 
81  bool
82  operator<(IOUAmount const& other) const;
83 
85  explicit operator bool() const noexcept;
86 
88  int
89  signum() const noexcept;
90 
91  int
92  exponent() const noexcept;
93 
94  std::int64_t
95  mantissa() const noexcept;
96 
97  static IOUAmount
99 };
100 
101 inline IOUAmount::IOUAmount(beast::Zero)
102 {
103  *this = beast::zero;
104 }
105 
108 {
109  normalize();
110 }
111 
113 {
114  // The -100 is used to allow 0 to sort less than small positive values
115  // which will have a large negative exponent.
116  mantissa_ = 0;
117  exponent_ = -100;
118  return *this;
119 }
120 
121 inline IOUAmount::operator Number() const
122 {
123  return Number{mantissa_, exponent_};
124 }
125 
126 inline IOUAmount&
128 {
129  *this += -other;
130  return *this;
131 }
132 
133 inline IOUAmount
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::operator bool() const noexcept
152 {
153  return mantissa_ != 0;
154 }
155 
156 inline int
157 IOUAmount::signum() const noexcept
158 {
159  return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
160 }
161 
162 inline int
163 IOUAmount::exponent() const noexcept
164 {
165  return exponent_;
166 }
167 
168 inline std::int64_t
169 IOUAmount::mantissa() const noexcept
170 {
171  return mantissa_;
172 }
173 
175 to_string(IOUAmount const& amount);
176 
177 /* Return num*amt/den
178  This function keeps more precision than computing
179  num*amt, storing the result in an IOUAmount, then
180  dividing by den.
181 */
182 IOUAmount
183 mulRatio(
184  IOUAmount const& amt,
185  std::uint32_t num,
186  std::uint32_t den,
187  bool roundUp);
188 
189 // Since many uses of the number class do not have access to a ledger,
190 // getSTNumberSwitchover needs to be globally accessible.
191 
192 bool
194 
195 void
196 setSTNumberSwitchover(bool v);
197 
201 class NumberSO
202 {
203  bool saved_;
204 
205 public:
207  {
209  }
210 
211  NumberSO(NumberSO const&) = delete;
212  NumberSO&
213  operator=(NumberSO const&) = delete;
214 
215  explicit NumberSO(bool v) : saved_(getSTNumberSwitchover())
216  {
218  }
219 };
220 
221 } // namespace ripple
222 
223 #endif
ripple::mulRatio
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition: IOUAmount.cpp:182
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:163
ripple::NumberSO::~NumberSO
~NumberSO()
Definition: IOUAmount.h:206
ripple::setSTNumberSwitchover
void setSTNumberSwitchover(bool v)
Definition: IOUAmount.cpp:48
std::string
STL class.
utility
ripple::IOUAmount::minPositiveAmount
static IOUAmount minPositiveAmount()
Definition: IOUAmount.cpp:61
ripple::NumberSO::saved_
bool saved_
Definition: IOUAmount.h:203
ripple::IOUAmount::normalize
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition: IOUAmount.cpp:67
ripple::NumberSO
RAII class to set and restore the Number switchover.
Definition: IOUAmount.h:201
ripple::NumberSO::operator=
NumberSO & operator=(NumberSO const &)=delete
ripple::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:43
ripple::IOUAmount::operator<
bool operator<(IOUAmount const &other) const
Definition: IOUAmount.h:146
ripple::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:157
ripple::Number
Definition: Number.h:36
ripple::IOUAmount::mantissa_
std::int64_t mantissa_
Definition: IOUAmount.h:47
ripple::IOUAmount::exponent_
int exponent_
Definition: IOUAmount.h:48
ripple::NumberSO::NumberSO
NumberSO(NumberSO const &)=delete
ripple::IOUAmount::operator-=
IOUAmount & operator-=(IOUAmount const &other)
Definition: IOUAmount.h:127
beast::Zero
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:42
ripple::IOUAmount::IOUAmount
IOUAmount()=default
cstdint
ripple::NumberSO::NumberSO
NumberSO(bool v)
Definition: IOUAmount.h:215
std::int64_t
ripple::IOUAmount::operator+=
IOUAmount & operator+=(IOUAmount const &other)
Definition: IOUAmount.cpp:130
ripple::IOUAmount::operator==
bool operator==(IOUAmount const &other) const
Definition: IOUAmount.h:140
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::IOUAmount::mantissa
std::int64_t mantissa() const noexcept
Definition: IOUAmount.h:169
ripple::getSTNumberSwitchover
bool getSTNumberSwitchover()
Definition: IOUAmount.cpp:42
ripple::IOUAmount::operator-
IOUAmount operator-() const
Definition: IOUAmount.h:134
std
STL namespace.
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::IOUAmount::operator=
IOUAmount & operator=(beast::Zero)
Definition: IOUAmount.h:112
beast
Definition: base_uint.h:641
string