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
42  : private boost::totally_ordered<IOUAmount>
43  , private boost::additive <IOUAmount>
44 {
45 private:
47  int exponent_;
48 
55  void
56  normalize ();
57 
58 public:
59  IOUAmount () = default;
60  IOUAmount (IOUAmount const& other) = default;
61  IOUAmount&operator= (IOUAmount const& other) = default;
62 
64  {
65  *this = beast::zero;
66  }
67 
71  {
72  normalize ();
73  }
74 
75  IOUAmount&
77  {
78  // The -100 is used to allow 0 to sort less than small positive values
79  // which will have a large negative exponent.
80  mantissa_ = 0;
81  exponent_ = -100;
82  return *this;
83  }
84 
85  IOUAmount&
86  operator+= (IOUAmount const& other);
87 
88  IOUAmount&
89  operator-= (IOUAmount const& other)
90  {
91  *this += -other;
92  return *this;
93  }
94 
95  IOUAmount
96  operator- () const
97  {
98  return { -mantissa_, exponent_ };
99  }
100 
101  bool
102  operator==(IOUAmount const& other) const
103  {
104  return exponent_ == other.exponent_ &&
105  mantissa_ == other.mantissa_;
106  }
107 
108  bool
109  operator<(IOUAmount const& other) const;
110 
112  explicit
113  operator bool() const noexcept
114  {
115  return mantissa_ != 0;
116  }
117 
119  int
120  signum() const noexcept
121  {
122  return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
123  }
124 
125  int
126  exponent() const noexcept
127  {
128  return exponent_;
129  }
130 
132  mantissa() const noexcept
133  {
134  return mantissa_;
135  }
136 };
137 
139 to_string (IOUAmount const& amount);
140 
141 /* Return num*amt/den
142  This function keeps more precision than computing
143  num*amt, storing the result in an IOUAmount, then
144  dividing by den.
145 */
146 IOUAmount
147 mulRatio (
148  IOUAmount const& amt,
149  std::uint32_t num,
150  std::uint32_t den,
151  bool roundUp);
152 
153 }
154 
155 #endif
ripple::mulRatio
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
Definition: IOUAmount.cpp:248
ripple::IOUAmount::exponent
int exponent() const noexcept
Definition: IOUAmount.h:126
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
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:41
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:120
ripple::IOUAmount::IOUAmount
IOUAmount(beast::Zero)
Definition: IOUAmount.h:63
ripple::IOUAmount::mantissa_
std::int64_t mantissa_
Definition: IOUAmount.h:46
ripple::IOUAmount::exponent_
int exponent_
Definition: IOUAmount.h:47
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:89
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:102
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:132
ripple::IOUAmount::operator-
IOUAmount operator-() const
Definition: IOUAmount.h:96
string