Class: Code::Object::Decimal
Instance Attribute Summary
Attributes inherited from Code::Object
#raw
Instance Method Summary
collapse
#<=>, #==, #as_json, as_json, call, #code_and_operator, code_and_operator, code_as_json, #code_as_json, code_different, #code_different, code_equal_equal, #code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, #code_exclusive_range, code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, #code_self, code_self, code_to_json, #code_to_json, #falsy?, falsy?, #hash, inspect, #inspect, maybe, multi_fetch, #multi_fetch, repeat, sig, #sig, #succ, to_json, #to_json, #to_s, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Decimal
Returns a new instance of Decimal.
6
7
8
9
10
11
12
13
14
|
# File 'lib/code/object/decimal.rb', line 6
def initialize(*args, **_kargs, &_block)
decimal = args.first || "0"
exponent = args.second || "0"
decimal = decimal.raw if decimal.is_an?(Object)
exponent = exponent.raw if exponent.is_an?(Object)
@raw = decimal.to_d * 10**exponent.to_d
rescue FloatDomainError
raise Error, "#{decimal.inspect} * 10**#{exponent.inspect} is invalid"
end
|
Instance Method Details
#call(**args) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
78
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/code/object/decimal.rb', line 16
def call(**args)
operator = args.fetch(:operator, nil)
arguments = args.fetch(:arguments, List.new)
value = arguments.code_first
case operator.to_s
when "%", "modulo"
sig(args) { Integer | Decimal }
code_modulo(value)
when "&", "bitwise_and"
sig(args) { Integer | Decimal }
code_bitwise_and(value)
when "*", "multiplication"
sig(args) { Integer | Decimal }
code_multiplication(value)
when "**", "power"
sig(args) { Integer | Decimal }
code_power(value)
when "+", "plus"
sig(args) { Object.maybe }
arguments.any? ? code_plus(value) : self
when "-", "minus"
sig(args) { (Integer | Decimal).maybe }
arguments.any? ? code_minus(value) : code_unary_minus
when "/", "division"
sig(args) { Integer | Decimal }
code_division(value)
when "<", "inferior"
sig(args) { Integer | Decimal }
code_inferior(value)
when "<<", "left_shift"
sig(args) { Integer | Decimal }
code_left_shift(value)
when "<=", "inferior_or_equal"
sig(args) { Integer | Decimal }
code_inferior_or_equal(value)
when "<=>", "compare"
sig(args) { Integer | Decimal }
code_compare(value)
when ">", "superior"
sig(args) { Integer | Decimal }
code_superior(value)
when ">=", "superior_or_equal"
sig(args) { Integer | Decimal }
code_superior_or_equal(value)
when ">>", "right_shift"
sig(args) { Integer | Decimal }
code_right_shift(value)
when "^", "bitwise_xor"
sig(args) { Integer | Decimal }
code_bitwise_xor(value)
when "abs"
sig(args)
code_abs
when "ceil"
sig(args) { Integer.maybe }
code_ceil(value)
when "clone"
sig(args)
code_clone
when "eight?"
sig(args)
code_eight?
when "five?"
sig(args)
code_five?
when "floor"
sig(args) { Integer.maybe }
code_floor(value)
when "four?"
sig(args)
code_four?
when "nine?"
sig(args)
code_nine?
when "one?"
sig(args)
code_one?
when "round"
sig(args) { Integer.maybe }
code_round(value)
when "seven?"
sig(args)
code_seven?
when "six?"
sig(args)
code_six?
when "sqrt"
sig(args)
code_sqrt
when "ten?"
sig(args)
code_ten?
when "three?"
sig(args)
code_three?
when "to_decimal"
sig(args)
code_to_decimal
when "to_integer"
sig(args)
code_to_integer
when "to_string"
sig(args)
code_to_string
when "truncate"
sig(args) { Integer.maybe }
code_truncate(value)
when "two?"
sig(args)
code_two?
when "zero?"
sig(args)
code_zero?
when "|", "bitwise_or"
sig(args) { Integer | Decimal }
code_bitwise_or(value)
else
super
end
end
|
138
139
140
|
# File 'lib/code/object/decimal.rb', line 138
def code_abs
Decimal.new(raw.abs)
end
|
#code_bitwise_and(other) ⇒ Object
142
143
144
|
# File 'lib/code/object/decimal.rb', line 142
def code_bitwise_and(other)
Integer.new(raw.to_i & other.raw.to_i)
end
|
#code_bitwise_or(other) ⇒ Object
146
147
148
|
# File 'lib/code/object/decimal.rb', line 146
def code_bitwise_or(other)
Integer.new(raw.to_i | other.raw.to_i)
end
|
#code_bitwise_xor(other) ⇒ Object
150
151
152
|
# File 'lib/code/object/decimal.rb', line 150
def code_bitwise_xor(other)
Integer.new(raw.to_i ^ other.raw.to_i)
end
|
#code_ceil(n = nil) ⇒ Object
154
155
156
157
|
# File 'lib/code/object/decimal.rb', line 154
def code_ceil(n = nil)
n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.ceil(n.raw))
end
|
#code_clone ⇒ Object
159
160
161
|
# File 'lib/code/object/decimal.rb', line 159
def code_clone
Decimal.new(raw)
end
|
#code_compare(other) ⇒ Object
163
164
165
|
# File 'lib/code/object/decimal.rb', line 163
def code_compare(other)
Integer.new(raw <=> other.raw)
end
|
#code_division(other) ⇒ Object
167
168
169
|
# File 'lib/code/object/decimal.rb', line 167
def code_division(other)
Decimal.new(raw / other.raw)
end
|
#code_eight? ⇒ Boolean
171
172
173
|
# File 'lib/code/object/decimal.rb', line 171
def code_eight?
Boolean.new(raw == 8)
end
|
175
176
177
|
# File 'lib/code/object/decimal.rb', line 175
def code_five?
Boolean.new(raw == 5)
end
|
#code_floor(n = nil) ⇒ Object
179
180
181
182
|
# File 'lib/code/object/decimal.rb', line 179
def code_floor(n = nil)
n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.floor(n.raw))
end
|
184
185
186
|
# File 'lib/code/object/decimal.rb', line 184
def code_four?
Boolean.new(raw == 4)
end
|
#code_inferior(other) ⇒ Object
188
189
190
|
# File 'lib/code/object/decimal.rb', line 188
def code_inferior(other)
Boolean.new(raw < other.raw)
end
|
#code_inferior_or_equal(other) ⇒ Object
192
193
194
|
# File 'lib/code/object/decimal.rb', line 192
def code_inferior_or_equal(other)
Boolean.new(raw <= other.raw)
end
|
#code_left_shift(other) ⇒ Object
196
197
198
|
# File 'lib/code/object/decimal.rb', line 196
def code_left_shift(other)
Integer.new(raw.to_i << other.raw.to_i)
end
|
#code_minus(other) ⇒ Object
200
201
202
|
# File 'lib/code/object/decimal.rb', line 200
def code_minus(other)
Decimal.new(raw - other.raw)
end
|
#code_modulo(other) ⇒ Object
204
205
206
|
# File 'lib/code/object/decimal.rb', line 204
def code_modulo(other)
Decimal.new(raw % other.raw)
end
|
#code_multiplication(other) ⇒ Object
208
209
210
|
# File 'lib/code/object/decimal.rb', line 208
def code_multiplication(other)
Decimal.new(raw * other.raw)
end
|
212
213
214
|
# File 'lib/code/object/decimal.rb', line 212
def code_nine?
Boolean.new(raw == 9)
end
|
216
217
218
|
# File 'lib/code/object/decimal.rb', line 216
def code_one?
Boolean.new(raw == 1)
end
|
#code_plus(other) ⇒ Object
220
221
222
223
224
225
226
|
# File 'lib/code/object/decimal.rb', line 220
def code_plus(other)
if other.is_an?(Integer) || other.is_a?(Decimal)
Decimal.new(raw + other.raw)
else
String.new(to_s + other.to_s)
end
end
|
#code_power(other) ⇒ Object
228
229
230
|
# File 'lib/code/object/decimal.rb', line 228
def code_power(other)
Decimal.new(raw**other.raw)
end
|
#code_right_shift(other) ⇒ Object
232
233
234
|
# File 'lib/code/object/decimal.rb', line 232
def code_right_shift(other)
Integer.new(raw.to_i >> other.raw.to_i)
end
|
#code_round(n = nil) ⇒ Object
236
237
238
239
|
# File 'lib/code/object/decimal.rb', line 236
def code_round(n = nil)
n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.round(n.raw))
end
|
#code_seven? ⇒ Boolean
241
242
243
|
# File 'lib/code/object/decimal.rb', line 241
def code_seven?
Boolean.new(raw == 7)
end
|
245
246
247
|
# File 'lib/code/object/decimal.rb', line 245
def code_six?
Boolean.new(raw == 6)
end
|
#code_sqrt ⇒ Object
249
250
251
|
# File 'lib/code/object/decimal.rb', line 249
def code_sqrt
Decimal.new(Math.sqrt(raw).to_s)
end
|
#code_superior(other) ⇒ Object
253
254
255
|
# File 'lib/code/object/decimal.rb', line 253
def code_superior(other)
Boolean.new(raw > other.raw)
end
|
#code_superior_or_equal(other) ⇒ Object
257
258
259
|
# File 'lib/code/object/decimal.rb', line 257
def code_superior_or_equal(other)
Boolean.new(raw >= other.raw)
end
|
261
262
263
|
# File 'lib/code/object/decimal.rb', line 261
def code_ten?
Boolean.new(raw == 10)
end
|
#code_three? ⇒ Boolean
265
266
267
|
# File 'lib/code/object/decimal.rb', line 265
def code_three?
Boolean.new(raw == 3)
end
|
#code_to_decimal ⇒ Object
269
270
271
|
# File 'lib/code/object/decimal.rb', line 269
def code_to_decimal
Decimal.new(raw)
end
|
#code_to_integer ⇒ Object
273
274
275
|
# File 'lib/code/object/decimal.rb', line 273
def code_to_integer
Integer.new(raw.to_i)
end
|
#code_to_string ⇒ Object
277
278
279
|
# File 'lib/code/object/decimal.rb', line 277
def code_to_string
String.new(raw.to_s("F"))
end
|
#code_truncate(n = nil) ⇒ Object
281
282
283
284
|
# File 'lib/code/object/decimal.rb', line 281
def code_truncate(n = nil)
n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.truncate(n.raw))
end
|
286
287
288
|
# File 'lib/code/object/decimal.rb', line 286
def code_two?
Boolean.new(raw == 2)
end
|
#code_unary_minus ⇒ Object
290
291
292
|
# File 'lib/code/object/decimal.rb', line 290
def code_unary_minus
Decimal.new(-raw)
end
|
294
295
296
|
# File 'lib/code/object/decimal.rb', line 294
def code_zero?
Boolean.new(raw.zero?)
end
|