rippled
Rate2.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2015 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 #include <ripple/protocol/Quality.h>
21 #include <ripple/protocol/Rate.h>
22 
23 namespace ripple {
24 
25 Rate const parityRate(QUALITY_ONE);
26 
27 namespace detail {
28 
29 STAmount
30 as_amount(Rate const& rate)
31 {
32  return {noIssue(), rate.value, -9, false};
33 }
34 
35 } // namespace detail
36 
37 STAmount
38 multiply(STAmount const& amount, Rate const& rate)
39 {
40  assert(rate.value != 0);
41 
42  if (rate == parityRate)
43  return amount;
44 
45  return multiply(amount, detail::as_amount(rate), amount.issue());
46 }
47 
48 STAmount
49 multiplyRound(STAmount const& amount, Rate const& rate, bool roundUp)
50 {
51  assert(rate.value != 0);
52 
53  if (rate == parityRate)
54  return amount;
55 
56  return mulRound(amount, detail::as_amount(rate), amount.issue(), roundUp);
57 }
58 
59 STAmount
61  STAmount const& amount,
62  Rate const& rate,
63  Issue const& issue,
64  bool roundUp)
65 {
66  assert(rate.value != 0);
67 
68  if (rate == parityRate)
69  {
70  return amount;
71  }
72 
73  return mulRound(amount, detail::as_amount(rate), issue, roundUp);
74 }
75 
76 STAmount
77 divide(STAmount const& amount, Rate const& rate)
78 {
79  assert(rate.value != 0);
80 
81  if (rate == parityRate)
82  return amount;
83 
84  return divide(amount, detail::as_amount(rate), amount.issue());
85 }
86 
87 STAmount
88 divideRound(STAmount const& amount, Rate const& rate, bool roundUp)
89 {
90  assert(rate.value != 0);
91 
92  if (rate == parityRate)
93  return amount;
94 
95  return divRound(amount, detail::as_amount(rate), amount.issue(), roundUp);
96 }
97 
98 STAmount
100  STAmount const& amount,
101  Rate const& rate,
102  Issue const& issue,
103  bool roundUp)
104 {
105  assert(rate.value != 0);
106 
107  if (rate == parityRate)
108  return amount;
109 
110  return divRound(amount, detail::as_amount(rate), issue, roundUp);
111 }
112 
113 } // namespace ripple
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
ripple::Rate
Represents a transfer rate.
Definition: Rate.h:37
ripple::multiplyRound
STAmount multiplyRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:49
ripple::noIssue
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition: Issue.h:105
ripple::divideRound
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition: Rate2.cpp:88
ripple::parityRate
const Rate parityRate(QUALITY_ONE)
A transfer rate signifying a 1:1 exchange.
Definition: Rate.h:94
ripple::mulRound
STAmount mulRound(STAmount const &v1, STAmount const &v2, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1199
ripple::divide
STAmount divide(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:77
ripple::divRound
STAmount divRound(STAmount const &num, STAmount const &den, Issue const &issue, bool roundUp)
Definition: STAmount.cpp:1285
ripple::STAmount
Definition: STAmount.h:42
ripple::multiply
STAmount multiply(STAmount const &amount, Rate const &rate)
Definition: Rate2.cpp:38
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:197
ripple::detail::as_amount
STAmount as_amount(Rate const &rate)
Definition: Rate2.cpp:30