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/beast/utility/Zero.h>
24 #include <boost/operators.hpp>
25 #include <cstdint>
26 #include <string>
27 #include <utility>
28 
29 namespace ripple {
30 
41 class IOUAmount : private boost::totally_ordered<IOUAmount>,
42  private boost::additive<IOUAmount>
43 {
44 private:
46  int exponent_;
47 
54  void
55  normalize();
56 
57 public:
58  IOUAmount() = default;
59  IOUAmount(IOUAmount const& other) = default;
60  IOUAmount&
61  operator=(IOUAmount const& other) = default;
62 
64  {
65  *this = beast::zero;
66  }
67 
70  {
71  normalize();
72  }
73 
75  {
76  // The -100 is used to allow 0 to sort less than small positive values
77  // which will have a large negative exponent.
78  mantissa_ = 0;
79  exponent_ = -100;
80  return *this;
81  }
82 
83  IOUAmount&
84  operator+=(IOUAmount const& other);
85 
86  IOUAmount&
87  operator-=(IOUAmount const& other)
88  {
89  *this += -other;
90  return *this;
91  }
92 
93  IOUAmount
94  operator-() const
95  {
96  return {-mantissa_, exponent_};
97  }
98 
99  bool
100  operator==(IOUAmount const& other) const
101  {
102  return exponent_ == other.exponent_ && mantissa_ == other.mantissa_;
103  }
104 
105  bool
106  operator<(IOUAmount const& other) const;
107 
109  explicit operator bool() const noexcept
110  {
111  return mantissa_ != 0;
112  }
113 
115  int
116  signum() const noexcept
117  {
118  return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
119  }
120 
121  int
122  exponent() const noexcept
123  {
124  return exponent_;
125  }
126 
128  mantissa() const noexcept
129  {
130  return mantissa_;
131  }
132 };
133 
135 to_string(IOUAmount const& amount);
136 
137 /* Return num*amt/den
138  This function keeps more precision than computing
139  num*amt, storing the result in an IOUAmount, then
140  dividing by den.
141 */
142 IOUAmount
143 mulRatio(
144  IOUAmount const& amt,
145  std::uint32_t num,
146  std::uint32_t den,
147  bool roundUp);
148 
149 } // namespace ripple
150 
151 #endif
ripple::mulRatio
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition: IOUAmount.cpp:242
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:122
std::string
STL class.
utility
ripple::IOUAmount::normalize
void normalize()
Adjusts the mantissa and exponent to the proper range.
Definition: IOUAmount.cpp:38
ripple::IOUAmount::IOUAmount
IOUAmount(std::int64_t mantissa, int exponent)
Definition: IOUAmount.h:68
ripple::IOUAmount::operator=
IOUAmount & operator=(beast::Zero)
Definition: IOUAmount.h:74
ripple::IOUAmount
Floating point representation of amounts with high dynamic range.
Definition: IOUAmount.h:41
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:45
ripple::IOUAmount::operator<
bool operator<(IOUAmount const &other) const
Definition: IOUAmount.cpp:122
ripple::IOUAmount::signum
int signum() const noexcept
Return the sign of the amount.
Definition: IOUAmount.h:116
ripple::IOUAmount::IOUAmount
IOUAmount(beast::Zero)
Definition: IOUAmount.h:63
ripple::IOUAmount::mantissa_
std::int64_t mantissa_
Definition: IOUAmount.h:45
ripple::IOUAmount::exponent_
int exponent_
Definition: IOUAmount.h:46
ripple::IOUAmount::operator=
IOUAmount & operator=(IOUAmount const &other)=default
beast::Zero
Zero allows classes to offer efficient comparisons to zero.
Definition: Zero.h:42
ripple::IOUAmount::IOUAmount
IOUAmount()=default
cstdint
ripple::IOUAmount::operator-=
IOUAmount & operator-=(IOUAmount const &other)
Definition: IOUAmount.h:87
std::int64_t
ripple::IOUAmount::operator+=
IOUAmount & operator+=(IOUAmount const &other)
Definition: IOUAmount.cpp:80
ripple::IOUAmount::operator==
bool operator==(IOUAmount const &other) const
Definition: IOUAmount.h:100
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:128
ripple::IOUAmount::operator-
IOUAmount operator-() const
Definition: IOUAmount.h:94
string