forked from goby-lang/goby
/
decimal.go
723 lines (648 loc) · 21.4 KB
/
decimal.go
1
2
3
4
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package vm
import (
"math/big"
"math"
"strings"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
)
// A type alias for representing a decimal
type Decimal = big.Rat
type Int = big.Int
type Float = big.Float
// (Experiment)
// DecimalObject represents a comparable decimal number using Go's Rational representation `big.Rat` from math/big package,
// which consists of a numerator and a denominator with arbitrary size.
// By using Decimal you can avoid errors on float type during calculations.
// To keep accuracy, avoid conversions until all calculations have been finished.
// The numerator can be 0, but the denominator cannot be 0.
// Using Decimal for loop counters or like that is not recommended (TBD).
//
// ```ruby
// "3.14".to_d # => 3.14
// "-0.7238943".to_d # => -0.7238943
// "355/113".to_d # => 3.1415929203539823008849557522123893805309734513274336283185840
//
// a = "16.1".to_d
// b = "1.1".to_d
// e = "17.2".to_d
// a + b # => 0.1
// a + b == e # => true
//
// ('16.1'.to_d + "1.1".to_d).to_s #=> 17.2
// ('16.1'.to_f + "1.1".to_f).to_s #=> 17.200000000000003
// ```
//
// - `Decimal.new` is not supported.
type DecimalObject struct {
*baseObj
value *Decimal
}
// Class methods --------------------------------------------------------
func builtinDecimalClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.initUnsupportedMethodError(sourceLine, "#new", receiver)
},
},
}
}
// Instance methods -----------------------------------------------------
func builtinDecimalInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// Returns the sum of self and a numeric.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// "1.1".to_d + "2.1".to_d # => 3.2
// "1.1".to_d + 2 # => 3.2
// "1.1".to_d + "2.1".to_f
// # => 3.200000000000000088817841970012523233890533447265625
// ```
//
// @return [Decimal]
Name: "+",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue *Decimal, rightValue *Decimal) *Decimal {
return new(Decimal).Add(leftValue, rightValue)
}
return receiver.(*DecimalObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns the subtraction of a decimal from self.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// ("1.5".to_d) - "1.1".to_d # => 0.4
// ("1.5".to_d) - 1 # => 0.5
// ("1.5".to_d) - "1.1".to_f # => 0.4
// #=> 0.399999999999999911182158029987476766109466552734375
// ```
//
// @return [Decimal]
Name: "-",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue *Decimal, rightValue *Decimal) *Decimal {
return new(Decimal).Sub(leftValue, rightValue)
}
return receiver.(*DecimalObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self multiplying a decimal.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// "2.5".to_d * "10.1".to_d # => 25.25
// "2.5".to_d * 10 # => 25
// "2.5".to_d * "10.1".to_f
// #=> 25.24999999999999911182158029987476766109466552734375
// ```
//
// @return [Decimal]
Name: "*",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue *Decimal, rightValue *Decimal) *Decimal {
return new(Decimal).Mul(leftValue, rightValue)
}
return receiver.(*DecimalObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self squaring a decimal.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
// Note that the calculation is via float64 (math.Pow) for now.
//
// ```Ruby
// "4.0".to_d ** "2.5".to_d # => 32
// "4.0".to_d ** 2 # => 16
// "4.0".to_d ** "2.5".to_f # => 32
// "4.0".to_d ** "2.1".to_d
// #=> 18.379173679952561570871694129891693592071533203125
// "4.0".to_d ** "2.1".to_f
// #=> 18.379173679952561570871694129891693592071533203125
// ```
//
// @return [Decimal]
Name: "**",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue *Decimal, rightValue *Decimal) *Decimal {
l, _ := leftValue.Float64()
r, _ := rightValue.Float64()
return new(Decimal).SetFloat64(math.Pow(l, r))
}
return receiver.(*DecimalObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self divided by a decimal.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// "7.5".to_d / "3.1".to_d.fraction # => 75/31
// "7.5".to_d / "3.1".to_d
// # => 2.419354838709677419354838709677419354838709677419354838709677
// "7.5".to_d / 3 # => 2.5
// "7.5".to_d / "3.1".to_f
// #=> 2.419354838709677350038104601967335570360611893758448172620333
// ```
//
// @return [Decimal]
Name: "/",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) *Decimal {
return new(Decimal).Quo(leftValue, rightValue)
}
return receiver.(*DecimalObject).arithmeticOperation(t, args[0], decimalOperation, sourceLine, true)
},
},
{
// Returns if self is larger than a decimal.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// a = "3.14".to_d
// b = "3.16".to_d
// a > b # => false
// b > a # => true
// a > 3 # => true
// a > 4 # => false
// a > "3.1".to_f # => true
// a > "3.2".to_f # => false
// ```
//
// @return [Boolean]
Name: ">",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
if leftValue.Cmp(rightValue) == 1 {
return true
}
return false
}
return receiver.(*DecimalObject).numericComparison(t, args[0], decimalOperation, sourceLine)
},
},
{
// Returns if self is larger than or equals to a Numeric.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// a = "3.14".to_d
// b = "3.16".to_d
// e = "3.14".to_d
// a >= b # => false
// b >= a # => true
// a >= e # => true
// a >= 3 # => true
// a >= 4 # => false
// a >= "3.1".to_f # => true
// a >= "3.2".to_f # => false
// ```
//
// @return [Boolean]
Name: ">=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
switch leftValue.Cmp(rightValue) {
case 1, 0:
return true
default:
return false
}
}
return receiver.(*DecimalObject).numericComparison(t, args[0], decimalOperation, sourceLine)
},
},
{
// Returns if self is smaller than a Numeric.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// a = "3.14".to_d
// b = "3.16".to_d
// a < b # => true
// b < a # => false
// a < 3 # => false
// a < 4 # => true
// a < "3.1".to_f # => false
// a < "3.2".to_f # => true
// ```
//
// @return [Boolean]
Name: "<",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
if leftValue.Cmp(rightValue) == -1 {
return true
}
return false
}
return receiver.(*DecimalObject).numericComparison(t, args[0], decimalOperation, sourceLine)
},
},
{
// Returns if self is smaller than or equals to a decimal.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
//
// ```Ruby
// a = "3.14".to_d
// b = "3.16".to_d
// e = "3.14".to_d
// a <= b # => true
// b <= a # => false
// a <= e # => false
// a <= 3 # => false
// a <= 4 # => true
// a <= "3.1".to_f # => false
// a <= "3.2".to_f # => true
// ```
//
// @return [Boolean]
Name: "<=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
switch leftValue.Cmp(rightValue) {
case -1, 0:
return true
default:
return false
}
}
return receiver.(*DecimalObject).numericComparison(t, args[0], decimalOperation, sourceLine)
},
},
{
// Returns 1 if self is larger than a Numeric, -1 if smaller. Otherwise 0.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
// x < y: -1
// x == y: 0 (including -0 == 0, -Infinity == +Infinity and vice versa)
// x > y: 1
//
// ```Ruby
// "1.5".to_d <=> 3 # => -1
// "1.0".to_d <=> 1 # => 0
// "3.5".to_d <=> 1 # => 1
// ```
//
// @return [Integer]
Name: "<=>",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) int {
return leftValue.Cmp(rightValue)
}
return receiver.(*DecimalObject).rocketComparison(t, args[0], decimalOperation, sourceLine)
},
},
{
// Returns if self is equal to an Object.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
// If the Object is not a Numeric the result is always false.
//
// ```Ruby
// "1.0".to_d == 3 # => false
// "1.0".to_d == 1 # => true
// "1.0".to_d == "1".to_d # => true
// "1.0".to_d == "1".to_f # => false
// "1.0".to_d == "1.0".to_f # => false
// "1.0".to_d == 'str' # => false
// "1.0".to_d == Array # => false
// ```
//
// @return [Boolean]
Name: "==",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
if leftValue.Cmp(rightValue) == 0 {
return true
}
return false
}
return receiver.(*DecimalObject).equalityTest(t, args[0], decimalOperation, true, sourceLine)
},
},
{
// Returns if self is not equal to an Object.
// If the second term is integer or float, they are converted into decimal and then perform calculation.
// If the Object is not a Numeric the result is always false.
//
// ```Ruby
// "1.0".to_d != 3 # => false
// "1.0".to_d != 1 # => true
// "1.0".to_d != "1".to_d # => true
// "1.0".to_d != "1".to_f # => false
// "1.0".to_d != "1.0".to_f # => false
// "1.0".to_d != 'str' # => false
// "1.0".to_d != Array # => false
// ```
//
// @return [Boolean]
Name: "!=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
decimalOperation := func(leftValue *Decimal, rightValue *Decimal) bool {
if leftValue.Cmp(rightValue) != 0 {
return true
}
return false
}
return receiver.(*DecimalObject).equalityTest(t, args[0], decimalOperation, false, sourceLine)
},
},
{
// Returns a string with fraction format of the decimal.
// If the denominator is 1, '/1` is omitted.
// Minus sign will be preserved.
// (Actually, the internal rational number is always deducted)
//
// ```Ruby
// a = "-355/113".to_d
// a.reduction #=> -355/113
// b = "-331/1".to_d
// b.reduction #=> -331
// ```
//
// @return [String]
Name: "reduction",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitStringObject(receiver.(*DecimalObject).value.RatString())
},
},
{
// Returns the denominator of the decimal value which contains Go's big.Rat type.
// The value is Decimal.
// The value does not contain a minus sign.
//
// ```Ruby
// a = "-355/113".to_d
// a.denominator #=> 113
// ```
//
// @return [Decimal]
Name: "denominator",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.initDecimalObject(new(Decimal).SetInt(receiver.(*DecimalObject).value.Denom()))
},
},
{
// Returns a string with fraction format of the decimal.
// Even though the denominator is 1, fraction format is used.
// Minus sign will be preserved.
//
// ```Ruby
// a = "-355/113".to_d
// a.fraction #=> -355/113
// b = "-331/1".to_d
// b.fraction #=> -331/1
// ```
//
// @return [String]
Name: "fraction",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitStringObject(receiver.(*DecimalObject).value.String())
},
},
{
// Inverses the numerator and the denominator of the decimal and returns it.
// Minus sign will move to the new numerator.
//
// ```Ruby
// a = "-355/113".to_d
// a.inverse.fraction #=> -113/355
// ```
//
// @return [Decimal]
Name: "inverse",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
d := receiver.(*DecimalObject).value
return t.vm.initDecimalObject(d.Inv(d))
},
},
{
// Returns the numerator of the decimal value which contains Go's big.Rat type.
// The value is Decimal.
// The value can contain a minus sign.
//
// ```Ruby
// a = "-355/113".to_d
// a.numerator #=> -355
// ```
//
// @return [Decimal]
Name: "numerator",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.initDecimalObject(new(Decimal).SetInt(receiver.(*DecimalObject).value.Num()))
},
},
{
// Returns an array with two Decimal elements: numerator and denominator.
//
// ```ruby
// "129.30928304982039482039842".to_d.to_a
// # => [6465464152491019741019921, 50000000000000000000000]
// ```
//
// @return [Array]
Name: "to_a",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
n := receiver.(*DecimalObject).value.Num()
d := receiver.(*DecimalObject).value.Denom()
elems := []Object{}
elems = append(elems, t.vm.initDecimalObject(new(Decimal).SetInt(n)))
elems = append(elems, t.vm.initDecimalObject(new(Decimal).SetInt(d)))
return t.vm.InitArrayObject(elems)
},
},
{
// Returns Float object from Decimal object.
// In most case the number of digits in Float is shorter than the one in Decimal.
//
// ```Ruby
// a = "355/113".to_d
// a.to_s # => 3.1415929203539823008849557522123893805309734513274336283185840
// a.to_f # => 3.1415929203539825
// ```
//
// @return [Float]
Name: "to_f",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.initFloatObject(receiver.(*DecimalObject).FloatValue())
},
},
{
// Returns the truncated Integer object from Decimal object.
//
// ```Ruby
// a = "355/113".to_d
// a.to_i # => 3
// ```
//
// @return [Integer]
Name: "to_i",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitIntegerObject(receiver.(*DecimalObject).IntegerValue())
},
},
{
// Returns the float-converted decimal value with a string style.
// Maximum digit under the dots is 60.
// This is just to print the final value should not be used for recalculation.
//
// ```Ruby
// a = "355/113".to_d
// a.to_s # => 3.1415929203539823008849557522123893805309734513274336283185840
// ```
//
// @return [String]
Name: "to_s",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.InitStringObject(receiver.(*DecimalObject).toString())
},
},
}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func (vm *VM) initDecimalObject(value *Decimal) *DecimalObject {
return &DecimalObject{
baseObj: &baseObj{class: vm.topLevelClass(classes.DecimalClass)},
value: value,
}
}
func (vm *VM) initDecimalClass() *RClass {
dc := vm.initializeClass(classes.DecimalClass)
dc.setBuiltinMethods(builtinDecimalInstanceMethods(), false)
dc.setBuiltinMethods(builtinDecimalClassMethods(), true)
return dc
}
// Polymorphic helper functions -----------------------------------------
// Value returns the object
func (f *DecimalObject) Value() interface{} {
return f.value
}
// Alias of Value()
func (f *DecimalObject) DecimalValue() interface{} {
return f.Value()
}
// Returns integer part of decimal
func (f *DecimalObject) IntegerValue() int {
return int(f.FloatValue())
}
// Float interface
func (f *DecimalObject) FloatValue() float64 {
x, _ := f.value.Float64()
return x
}
// Apply the passed arithmetic operation, while performing type conversion.
func (d *DecimalObject) arithmeticOperation(
t *Thread,
rightObject Object,
decimalOperation func(leftValue *Decimal, rightValue *Decimal) *Decimal,
sourceLine int,
division bool,
) Object {
var result Decimal
rightValue, ok := assertNumeric(rightObject)
if ok == false {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", rightObject.Class().Name)
}
if division && rightValue.RatString() == "0" {
return t.vm.InitErrorObject(errors.ZeroDivisionError, sourceLine, errors.DividedByZero)
}
leftValue := d.value
result = *decimalOperation(leftValue, rightValue)
return t.vm.initDecimalObject(&result)
}
// Apply an equality test, returning true if the objects are considered equal,
// and false otherwise.
func (d *DecimalObject) equalityTest(
t *Thread,
rightObject Object,
decimalOperation func(leftValue *Decimal, rightValue *Decimal) bool,
nonInverse bool,
sourceLine int,
) Object {
var rightValue *Decimal
var result bool
switch rightObject.(type) {
case *DecimalObject:
rightValue = rightObject.(*DecimalObject).value
default:
return toBooleanObject(nonInverse == false)
}
leftValue := d.value
result = decimalOperation(leftValue, rightValue)
return toBooleanObject(result)
}
// Apply the passed numeric comparison, while performing type conversion.
func (d *DecimalObject) numericComparison(
t *Thread,
rightObject Object,
decimalOperation func(leftValue *Decimal, rightValue *Decimal) bool,
sourceLine int,
) Object {
var result bool
rightValue, ok := assertNumeric(rightObject)
if ok == false {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", rightObject.Class().Name)
}
leftValue := d.value
result = decimalOperation(leftValue, rightValue)
return toBooleanObject(result)
}
// Apply the passed numeric comparison for rocket operator '<=>', while performing type conversion.
func (d *DecimalObject) rocketComparison(
t *Thread,
rightObject Object,
decimalOperation func(leftValue *Decimal, rightValue *Decimal) int,
sourceLine int,
) Object {
var result int
rightValue, ok := assertNumeric(rightObject)
if ok == false {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", rightObject.Class().Name)
}
leftValue := d.value
result = decimalOperation(leftValue, rightValue)
newInt := t.vm.InitIntegerObject(result)
newInt.flag = i
return newInt
}
// toString returns the object's approximate float value as the string format.
func (d *DecimalObject) toString() string {
fs := d.value.FloatString(60)
fs = strings.TrimRight(fs, "0")
fs = strings.TrimRight(fs, ".")
return fs
}
// toJSON just delegates to toString
func (d *DecimalObject) toJSON(t *Thread) string {
return d.toString()
}
// Other helper functions ----------------------------------------------
// Type assertion for numeric
func assertNumeric(n Object) (v *Decimal, result bool) {
result = true
switch n.(type) {
case *DecimalObject:
v = n.(*DecimalObject).value
case *IntegerObject:
v = intToDecimal(n)
case *FloatObject:
v = floatToDecimal(n)
default:
result = false
}
return v, result
}
// intToDecimal converts int to Decimal
func intToDecimal(i Object) *Decimal {
return new(Decimal).SetInt64(int64(i.(*IntegerObject).value))
}
// floatToDecimal converts int to Decimal
func floatToDecimal(i Object) *Decimal {
return new(Decimal).SetFloat64(float64(i.(*FloatObject).value))
}