-
Notifications
You must be signed in to change notification settings - Fork 171
/
float.go
440 lines (373 loc) · 12 KB
/
float.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
package vm
import (
"math"
"strconv"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
)
// FloatObject represents an inexact real number using the native architecture's double-precision floating point
// representation.
//
// ```ruby
// 1.1 + 1.1 # => 2.2
// 2.1 * 2.1 # => 4.41
// ```
//
// - `Float.new` is not supported.
type FloatObject struct {
*baseObj
value float64
}
// Class methods --------------------------------------------------------
func builtinFloatClassMethods() []*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 builtinFloatInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// Returns the sum of self and a Numeric.
//
// ```Ruby
// 1.1 + 2 # => 3.1
// ```
// @return [Float]
Name: "+",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return leftValue + rightValue
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns the modulo between self and a Numeric.
//
// ```Ruby
// 5.5 % 2 # => 1.5
// ```
// @return [Float]
Name: "%",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return math.Mod(leftValue, rightValue)
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, true)
},
},
{
// Returns the subtraction of a Numeric from self.
//
// ```Ruby
// 1.5 - 1 # => 0.5
// ```
// @return [Float]
Name: "-",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return leftValue - rightValue
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self multiplying a Numeric.
//
// ```Ruby
// 2.5 * 10 # => 25.0
// ```
// @return [Float]
Name: "*",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return leftValue * rightValue
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self squaring a Numeric.
//
// ```Ruby
// 4.0 ** 2.5 # => 32.0
// ```
// @return [Float]
Name: "**",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return math.Pow(leftValue, rightValue)
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, false)
},
},
{
// Returns self divided by a Numeric.
//
// ```Ruby
// 7.5 / 3 # => 2.5
// ```
// @return [Float]
Name: "/",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) float64 {
return leftValue / rightValue
}
return receiver.(*FloatObject).arithmeticOperation(t, args[0], operation, sourceLine, true)
},
},
{
// Returns if self is larger than a Numeric.
//
// ```Ruby
// 10 > -1 # => true
// 3 > 3 # => false
// ```
// @return [Boolean]
Name: ">",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) bool {
return leftValue > rightValue
}
return receiver.(*FloatObject).numericComparison(t, args[0], operation, sourceLine)
},
},
{
// Returns if self is larger than or equals to a Numeric.
//
// ```Ruby
// 2 >= 1 # => true
// 1 >= 1 # => true
// ```
// @return [Boolean]
Name: ">=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) bool {
return leftValue >= rightValue
}
return receiver.(*FloatObject).numericComparison(t, args[0], operation, sourceLine)
},
},
{
// Returns if self is smaller than a Numeric.
//
// ```Ruby
// 1 < 3 # => true
// 1 < 1 # => false
// ```
// @return [Boolean]
Name: "<",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) bool {
return leftValue < rightValue
}
return receiver.(*FloatObject).numericComparison(t, args[0], operation, sourceLine)
},
},
{
// Returns if self is smaller than or equals to a Numeric.
//
// ```Ruby
// 1 <= 3 # => true
// 1 <= 1 # => true
// ```
// @return [Boolean]
Name: "<=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
operation := func(leftValue float64, rightValue float64) bool {
return leftValue <= rightValue
}
return receiver.(*FloatObject).numericComparison(t, args[0], operation, sourceLine)
},
},
{
// Returns 1 if self is larger than a Numeric, -1 if smaller. Otherwise 0.
//
// ```Ruby
// 1.5 <=> 3 # => -1
// 1.0 <=> 1 # => 0
// 3.5 <=> 1 # => 1
// ```
// @return [Float]
Name: "<=>",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
rightNumeric, ok := args[0].(Numeric)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", args[0].Class().Name)
}
leftValue := receiver.(*FloatObject).value
rightValue := rightNumeric.floatValue()
if leftValue < rightValue {
return t.vm.InitIntegerObject(-1)
}
if leftValue > rightValue {
return t.vm.InitIntegerObject(1)
}
return t.vm.InitIntegerObject(0)
},
},
{
// Returns if self is equal to an Object.
// If the Object is a Numeric, a comparison is performed, otherwise, the
// result is always false.
//
// ```Ruby
// 1.0 == 3 # => false
// 1.0 == 1 # => true
// 1.0 == '1.0' # => false
// ```
// @return [Boolean]
Name: "==",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
result := receiver.(*FloatObject).equalityTest(args[0])
return toBooleanObject(result)
},
},
{
// Returns if self is not equal to an Object.
// If the Object is a Numeric, a comparison is performed, otherwise, the
// result is always true.
//
// ```Ruby
// 1.0 != 3 # => true
// 1.0 != 1 # => false
// 1.0 != '1.0' # => true
// ```
// @return [Boolean]
Name: "!=",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
result := !receiver.(*FloatObject).equalityTest(args[0])
return toBooleanObject(result)
},
},
{
// Converts the Integer object into Decimal object and returns it.
// Each digit of the float is literally transferred to the corresponding digit
// of the Decimal, via a string representation of the float.
//
// ```Ruby
// "100.1".to_f.to_d # => 100.1
//
// a = "3.14159265358979".to_f
// b = a.to_d #=> 3.14159265358979
// ```
//
// @return [Decimal]
Name: "to_d",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%v", strconv.Itoa(len(args)))
}
fl := receiver.(*FloatObject).value
fs := strconv.FormatFloat(fl, 'f', -1, 64)
de, err := new(Decimal).SetString(fs)
if err == false {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Invalid numeric string. got=%v", fs)
}
return t.vm.initDecimalObject(de)
},
},
{
// Returns the `Integer` representation of self.
//
// ```Ruby
// 100.1.to_i # => 100
// ```
// @return [Integer]
Name: "to_i",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
r := receiver.(*FloatObject)
newInt := t.vm.InitIntegerObject(int(r.value))
newInt.flag = i
return newInt
},
},
{
Name: "ptr",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
r := receiver.(*FloatObject)
return t.vm.initGoObject(&r.value)
},
},
}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func (vm *VM) initFloatObject(value float64) *FloatObject {
return &FloatObject{
baseObj: &baseObj{class: vm.topLevelClass(classes.FloatClass)},
value: value,
}
}
func (vm *VM) initFloatClass() *RClass {
ic := vm.initializeClass(classes.FloatClass)
ic.setBuiltinMethods(builtinFloatInstanceMethods(), false)
ic.setBuiltinMethods(builtinFloatClassMethods(), true)
return ic
}
// Polymorphic helper functions -----------------------------------------
// Value returns the object
func (f *FloatObject) Value() interface{} {
return f.value
}
// Numeric interface
func (f *FloatObject) floatValue() float64 {
return f.value
}
// TODO: Remove instruction argument
// Apply the passed arithmetic operation, while performing type conversion.
func (f *FloatObject) arithmeticOperation(t *Thread, rightObject Object, operation func(leftValue float64, rightValue float64) float64, sourceLine int, division bool) Object {
rightNumeric, ok := rightObject.(Numeric)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", rightObject.Class().Name)
}
leftValue := f.value
rightValue := rightNumeric.floatValue()
if division && rightValue == 0 {
return t.vm.InitErrorObject(errors.ZeroDivisionError, sourceLine, errors.DividedByZero)
}
result := operation(leftValue, rightValue)
return t.vm.initFloatObject(result)
}
// Apply an equality test, returning true if the objects are considered equal,
// and false otherwise.
func (f *FloatObject) equalityTest(rightObject Object) bool {
rightNumeric, ok := rightObject.(Numeric)
if !ok {
return false
}
leftValue := f.value
rightValue := rightNumeric.floatValue()
return leftValue == rightValue
}
// TODO: Remove instruction argument
// Apply the passed numeric comparison, while performing type conversion.
func (f *FloatObject) numericComparison(t *Thread, rightObject Object, operation func(leftValue float64, rightValue float64) bool, sourceLine int) Object {
rightNumeric, ok := rightObject.(Numeric)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, "Numeric", rightObject.Class().Name)
}
leftValue := f.value
rightValue := rightNumeric.floatValue()
result := operation(leftValue, rightValue)
return toBooleanObject(result)
}
// toString returns the object's value as the string format, in non
// exponential format (straight number, without exponent `E<exp>`).
func (f *FloatObject) toString() string {
return strconv.FormatFloat(f.value, 'f', -1, 64) // fmt.Sprintf("%f", f.value)
}
// toJSON just delegates to toString
func (f *FloatObject) toJSON(t *Thread) string {
return f.toString()
}
// equal checks if the Float values between receiver and argument are equal
func (f *FloatObject) equal(e *FloatObject) bool {
return f.value == e.value
}