Module: XRBP::NodeStore::STAmount::Arithmatic

Included in:
XRBP::NodeStore::STAmount
Defined in:
lib/xrbp/nodestore/sle/st_amount_arithmatic.rb

Instance Method Summary collapse

Instance Method Details

#*(o) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xrbp/nodestore/sle/st_amount_arithmatic.rb', line 79

def *(o)
  return self * STAmount.new(:mantissa => o) if o.kind_of?(Numeric)

  return STAmount.new :issue => issue if zero? || o.zero?

  if native? && o.native?
    min = sn_value < o.sn_value ?   sn_value : o.sn_value
    max = sn_value < o.sn_value ? o.sn_value :   sn_value

    return STAmount.new :mantissa => min * max
  end

  m1 =   mantissa
  m2 = o.mantissa
  e1 =   exponent
  e2 = o.exponent

  if native?
    while nm < MIN_VAL
      m1 *= 10
      e1 -= 1
    end
  end

  if o.native?
    while dm < MIN_VAL
      m2 *= 10
      e2 -= 1
    end
  end

  # see note: https://github.com/ripple/rippled/blob/b53fda1e1a7f4d09b766724274329df1c29988ab/src/ripple/protocol/impl/STAmount.cpp#L1131
  STAmount.new :issue => issue,
               :mantissa => (m1 * m2)/(10**14) + 7,
               :exponent => (e1 + e2 + 14),
               :neg      => (neg != o.neg)
end

#+(v) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xrbp/nodestore/sle/st_amount_arithmatic.rb', line 5

def +(v)
  return self + STAmount.new(:mantissa => v) if v.kind_of?(Numeric)

  e1 = exponent
  e2 = v.exponent

  m1 = mantissa
  m2 = v.mantissa

  m1 *= -1 if   neg
  m2 *= -1 if v.neg

  while e1 < e2
    m1 /= 10
    e1 += 1
  end

  while e2 < e1
    m2 /= 10
    e2 += 1
  end

  m = m1 + m2
  return STAmount.new :issue => issue if m >= -10 && m <= 10
  return STAmount.new :mantissa => m,
                      :exponent => e1,
                      :issue    => issue if m >= 0
  return STAmount.new :mantissa => -m,
                      :exponent => e1,
                      :issue    => issue
end

#-(v) ⇒ Object



37
38
39
# File 'lib/xrbp/nodestore/sle/st_amount_arithmatic.rb', line 37

def -(v)
  self + (-v)
end

#-@Object



117
118
119
120
121
122
# File 'lib/xrbp/nodestore/sle/st_amount_arithmatic.rb', line 117

def -@
    STAmount.new(:mantissa => mantissa,
                 :exponent => exponent,
                 :issue    => issue,
                 :neg      => !neg)
end

#/(v) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xrbp/nodestore/sle/st_amount_arithmatic.rb', line 41

def /(v)
  return self / STAmount.new(:mantissa => v) if v.kind_of?(Numeric)

  if v.is_a?(Rate)
    return self if v == Rate.parity
    return self / v.to_amount
  end

  raise "divide by zero" if v.zero?
  return STAmount.new :issue => issue if zero?

  nm = mantissa
  dm = v.mantissa

  ne = exponent
  de = v.exponent

  if native?
    while nm < MIN_VAL
      nm *= 10
      ne -= 1
    end
  end

  if v.native?
    while dm < MIN_VAL
      dm *= 10
      de -= 1
    end
  end

  # see note: https://github.com/ripple/rippled/blob/b53fda1e1a7f4d09b766724274329df1c29988ab/src/ripple/protocol/impl/STAmount.cpp#L1075
  STAmount.new :issue => issue,
               :mantissa => (nm * 10**17)/dm + 5,
               :exponent => (ne - de - 17),
               :neg      => (neg != v.neg)
end