Class: BigDecimal

Inherits:
Numeric show all
Defined in:
opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal.rb,
opal/stdlib/bigdecimal/bignumber.js.rb

Constant Summary collapse

ROUND_MODE =
256
ROUND_UP =

NOTE: the numeric values of the ROUND_* constants follow BigNumber.js, they are NOT the same as MRI

0
ROUND_DOWN =
1
ROUND_CEILING =
2
ROUND_FLOOR =
3
ROUND_HALF_UP =
4
ROUND_HALF_DOWN =
5
ROUND_HALF_EVEN =
6
SIGN_NaN =
0
SIGN_POSITIVE_ZERO =
1
SIGN_NEGATIVE_ZERO =
-1
SIGN_POSITIVE_FINITE =
2
SIGN_NEGATIVE_FINITE =
-2
SIGN_POSITIVE_INFINITE =
3
SIGN_NEGATIVE_INFINITE =
-3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#to_json, #to_n

Constructor Details

#initialize(initial, digits = 0) ⇒ BigDecimal

Returns a new instance of BigDecimal.



43
44
45
# File 'opal/stdlib/bigdecimal.rb', line 43

def initialize(initial, digits = 0)
  @bignumber = JS.new(BigNumber, initial)
end

Instance Attribute Details

#bignumberObject (readonly)

Returns the value of attribute bignumber.



41
42
43
# File 'opal/stdlib/bigdecimal.rb', line 41

def bignumber
  @bignumber
end

Class Method Details

.limit(digits = nil) ⇒ Object



28
29
30
31
# File 'opal/stdlib/bigdecimal.rb', line 28

def self.limit(digits = nil)
  @digits = digits if digits
  @digits
end

.mode(mode, value = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'opal/stdlib/bigdecimal.rb', line 33

def self.mode(mode, value = nil)
  case mode
  when ROUND_MODE
    @round_mode = value if value
    @round_mode || ROUND_HALF_UP
  end
end

Instance Method Details

#<(other) ⇒ Object



70
71
72
73
# File 'opal/stdlib/bigdecimal.rb', line 70

def <(other)
  return false if nan? || other && other.nan?
  super
end

#<=(other) ⇒ Object



75
76
77
78
# File 'opal/stdlib/bigdecimal.rb', line 75

def <=(other)
  return false if nan? || other && other.nan?
  super
end

#<=>(other) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'opal/stdlib/bigdecimal.rb', line 60

def <=>(other)
  result = case other
           when self.class
             bignumber.JS.comparedTo(other.bignumber)
           when Number
             bignumber.JS.comparedTo(other)
           end
  `#{result} === null ? nil : #{result}`
end

#==(other) ⇒ Object Also known as: ===



47
48
49
50
51
52
53
54
55
56
# File 'opal/stdlib/bigdecimal.rb', line 47

def ==(other)
  case other
  when self.class
    bignumber.JS.equals(other.bignumber)
  when Number
    bignumber.JS.equals(other)
  else
    false
  end
end

#>(other) ⇒ Object



80
81
82
83
# File 'opal/stdlib/bigdecimal.rb', line 80

def >(other)
  return false if nan? || other && other.nan?
  super
end

#>=(other) ⇒ Object



85
86
87
88
# File 'opal/stdlib/bigdecimal.rb', line 85

def >=(other)
  return false if nan? || other && other.nan?
  super
end

#absObject



90
91
92
# File 'opal/stdlib/bigdecimal.rb', line 90

def abs
  self.class.new(bignumber.JS.abs)
end

#add(other, digits = 0) ⇒ Object Also known as: +



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'opal/stdlib/bigdecimal.rb', line 94

def add(other, digits = 0)
  if digits.nil?
    raise TypeError, 'wrong argument type nil (expected Fixnum)'
  end

  if digits < 0
    raise ArgumentError, 'argument must be positive'
  end

  other, _ = coerce(other)

  result = bignumber.JS.plus(other.bignumber)

  if digits > 0
    result = result.JS.toDigits(digits, self.class.mode(ROUND_MODE))
  end

  self.class.new(result)
end

#ceil(n = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'opal/stdlib/bigdecimal.rb', line 116

def ceil(n = nil)
  unless bignumber.JS.isFinite
    raise FloatDomainError, "Computation results to 'Infinity'"
  end

  if n.nil?
    bignumber.JS.round(0, ROUND_CEILING).JS.toNumber
  elsif n >= 0
    self.class.new(bignumber.JS.round(n, ROUND_CEILING))
  else
    self.class.new(bignumber.JS.round(0, ROUND_CEILING))
  end
end

#coerce(other) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'opal/stdlib/bigdecimal.rb', line 130

def coerce(other)
  case other
  when self.class
    [other, self]
  when Number
    [self.class.new(other), self]
  else
    raise TypeError, "#{other.class} can't be coerced into #{self.class}"
  end
end

#div(other, digits = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'opal/stdlib/bigdecimal.rb', line 141

def div(other, digits = nil)
  return self / other if digits == 0

  other, _ = coerce(other)

  if nan? || other.nan?
    raise FloatDomainError, "Computation results to 'NaN'(Not a Number)"
  end

  if digits.nil?
    if other.zero?
      raise ZeroDivisionError, 'divided by 0'
    end

    if infinite?
      raise FloatDomainError, "Computation results to 'Infinity'"
    end

    return self.class.new(bignumber.JS.dividedToIntegerBy(other.bignumber))
  end

  self.class.new(bignumber.JS.dividedBy(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE)))
end

#finite?Boolean

Returns:



165
166
167
# File 'opal/stdlib/bigdecimal.rb', line 165

def finite?
  bignumber.JS.isFinite
end

#infinite?Boolean

Returns:



169
170
171
172
# File 'opal/stdlib/bigdecimal.rb', line 169

def infinite?
  return nil if finite? || nan?
  bignumber.JS.isNegative ? -1 : 1
end

#minus(other) ⇒ Object Also known as: -



174
175
176
177
# File 'opal/stdlib/bigdecimal.rb', line 174

def minus(other)
  other, _ = coerce(other)
  self.class.new(bignumber.JS.minus(other.bignumber))
end

#mult(other, digits = nil) ⇒ Object Also known as: *



181
182
183
184
185
186
187
188
189
# File 'opal/stdlib/bigdecimal.rb', line 181

def mult(other, digits = nil)
  other, _ = coerce(other)

  if digits.nil?
    return self.class.new(bignumber.JS.times(other.bignumber))
  end

  self.class.new(bignumber.JS.times(other.bignumber).JS.round(digits, self.class.mode(ROUND_MODE)))
end

#nan?Boolean

Returns:



193
194
195
# File 'opal/stdlib/bigdecimal.rb', line 193

def nan?
  bignumber.JS.isNaN
end

#quo(other) ⇒ Object Also known as: /



197
198
199
200
# File 'opal/stdlib/bigdecimal.rb', line 197

def quo(other)
  other, _ = coerce(other)
  self.class.new(bignumber.JS.dividedBy(other.bignumber))
end

#signObject



204
205
206
207
208
209
210
211
# File 'opal/stdlib/bigdecimal.rb', line 204

def sign
  if bignumber.JS.isNaN
    return SIGN_NaN
  end
  if bignumber.JS.isZero
    return bignumber.JS.isNegative ? SIGN_NEGATIVE_ZERO : SIGN_POSITIVE_ZERO
  end
end

#sub(other, precision) ⇒ Object



213
214
215
216
# File 'opal/stdlib/bigdecimal.rb', line 213

def sub(other, precision)
  other, _ = coerce(other)
  self.class.new(bignumber.JS.minus(other.bignumber))
end

#to_fObject



218
219
220
# File 'opal/stdlib/bigdecimal.rb', line 218

def to_f
  bignumber.JS.toNumber
end

#to_s(s = '') ⇒ Object



222
223
224
# File 'opal/stdlib/bigdecimal.rb', line 222

def to_s(s = '')
  bignumber.JS.toString
end

#zero?Boolean

Returns:



226
227
228
# File 'opal/stdlib/bigdecimal.rb', line 226

def zero?
  bignumber.JS.isZero
end