forked from go-python/gpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
394 lines (337 loc) · 8.67 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
// Float objects
package py
import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
)
var FloatType = ObjectType.NewType("float", "float(x) -> floating point number\n\nConvert a string or number to a floating point number, if possible.", FloatNew, nil)
// Bits of precision in a float64
const (
float64precision = 53
float64MaxExponent = 1023
)
type Float float64
// Type of this Float64 object
func (o Float) Type() *Type {
return FloatType
}
// FloatNew
func FloatNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
var xObj Object = Float(0)
err := ParseTupleAndKeywords(args, kwargs, "|O", []string{"x"}, &xObj)
if err != nil {
return nil, err
}
// Special case converting string types
switch x := xObj.(type) {
// FIXME Bytearray
case Bytes:
return FloatFromString(string(x))
case String:
return FloatFromString(string(x))
}
return MakeFloat(xObj)
}
func (a Float) M__str__() (Object, error) {
return String(fmt.Sprintf("%g", a)), nil
}
func (a Float) M__repr__() (Object, error) {
return a.M__str__()
}
// FloatFromString turns a string into a Float
func FloatFromString(str string) (Object, error) {
str = strings.TrimSpace(str)
f, err := strconv.ParseFloat(str, 64)
if err != nil {
if numErr, ok := err.(*strconv.NumError); ok {
if numErr.Err == strconv.ErrRange {
if str[0] == '-' {
return Float(math.Inf(-1)), nil
} else {
return Float(math.Inf(1)), nil
}
}
}
return nil, ExceptionNewf(ValueError, "invalid literal for float: '%s'", str)
}
return Float(f), nil
}
var expectingFloat = ExceptionNewf(TypeError, "a float is required")
// Returns the float value of obj if it is exactly a float
func FloatCheckExact(obj Object) (Float, error) {
f, ok := obj.(Float)
if !ok {
return 0, expectingFloat
}
return f, nil
}
// Returns the float value of obj if it is a float subclass
func FloatCheck(obj Object) (Float, error) {
// FIXME should be checking subclasses
return FloatCheckExact(obj)
}
// PyFloat_AsDouble
func FloatAsFloat64(obj Object) (float64, error) {
f, err := FloatCheck(obj)
if err == nil {
return float64(f), nil
}
fObj, err := MakeFloat(obj)
if err != nil {
return 0, err
}
f, err = FloatCheck(fObj)
if err == nil {
return float64(f), nil
}
return float64(f), err
}
// Arithmetic
// Errors
var floatDivisionByZero = ExceptionNewf(ZeroDivisionError, "float division by zero")
// Convert an Object to an Float
//
// Retrurns ok as to whether the conversion worked or not
func convertToFloat(other Object) (Float, bool) {
switch b := other.(type) {
case Float:
return b, true
case Int:
return Float(b), true
case *BigInt:
x, err := b.Float()
return x, err == nil
case Bool:
if b {
return Float(1), true
} else {
return Float(0), true
}
}
return 0, false
}
func (a Float) M__neg__() (Object, error) {
return -a, nil
}
func (a Float) M__pos__() (Object, error) {
return a, nil
}
func (a Float) M__abs__() (Object, error) {
return Float(math.Abs(float64(a))), nil
}
func (a Float) M__add__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(a + b), nil
}
return NotImplemented, nil
}
func (a Float) M__radd__(other Object) (Object, error) {
return a.M__add__(other)
}
func (a Float) M__iadd__(other Object) (Object, error) {
return a.M__add__(other)
}
func (a Float) M__sub__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(a - b), nil
}
return NotImplemented, nil
}
func (a Float) M__rsub__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(b - a), nil
}
return NotImplemented, nil
}
func (a Float) M__isub__(other Object) (Object, error) {
return a.M__sub__(other)
}
func (a Float) M__mul__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(a * b), nil
}
return NotImplemented, nil
}
func (a Float) M__rmul__(other Object) (Object, error) {
return a.M__mul__(other)
}
func (a Float) M__imul__(other Object) (Object, error) {
return a.M__mul__(other)
}
func (a Float) M__truediv__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
if b == 0 {
return nil, floatDivisionByZero
}
return Float(a / b), nil
}
return NotImplemented, nil
}
func (a Float) M__rtruediv__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
if a == 0 {
return nil, floatDivisionByZero
}
return Float(b / a), nil
}
return NotImplemented, nil
}
func (a Float) M__itruediv__(other Object) (Object, error) {
return Float(a).M__truediv__(other)
}
func (a Float) M__floordiv__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(math.Floor(float64(a / b))), nil
}
return NotImplemented, nil
}
func (a Float) M__rfloordiv__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(math.Floor(float64(b / a))), nil
}
return NotImplemented, nil
}
func (a Float) M__ifloordiv__(other Object) (Object, error) {
return a.M__floordiv__(other)
}
// Does DivMod of two floating point numbers
func floatDivMod(a, b Float) (Float, Float, error) {
if b == 0 {
return 0, 0, floatDivisionByZero
}
q := Float(math.Floor(float64(a / b)))
r := a - q*b
return q, Float(r), nil
}
func (a Float) M__mod__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
_, r, err := floatDivMod(a, b)
return r, err
}
return NotImplemented, nil
}
func (a Float) M__rmod__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
_, r, err := floatDivMod(b, a)
return r, err
}
return NotImplemented, nil
}
func (a Float) M__imod__(other Object) (Object, error) {
return a.M__mod__(other)
}
func (a Float) M__divmod__(other Object) (Object, Object, error) {
if b, ok := convertToFloat(other); ok {
return floatDivMod(a, b)
}
return NotImplemented, None, nil
}
func (a Float) M__rdivmod__(other Object) (Object, Object, error) {
if b, ok := convertToFloat(other); ok {
return floatDivMod(b, a)
}
return NotImplemented, None, nil
}
func (a Float) M__pow__(other, modulus Object) (Object, error) {
if modulus != None {
return NotImplemented, nil
}
if b, ok := convertToFloat(other); ok {
return Float(math.Pow(float64(a), float64(b))), nil
}
return NotImplemented, nil
}
func (a Float) M__rpow__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return Float(math.Pow(float64(b), float64(a))), nil
}
return NotImplemented, nil
}
func (a Float) M__ipow__(other, modulus Object) (Object, error) {
return a.M__pow__(other, modulus)
}
func (a Float) M__bool__() (Object, error) {
return NewBool(a != 0), nil
}
func (a Float) M__int__() (Object, error) {
if a >= IntMin && a <= IntMax {
return Int(a), nil
}
frac, exp := math.Frexp(float64(a)) // x = frac << exp; 0.5 <= abs(x) < 1
fracInt := int64(frac * (1 << float64precision)) // x = frac << (exp - float64precision)
res := big.NewInt(fracInt)
shift := exp - float64precision
switch {
case shift > 0:
res.Lsh(res, uint(shift))
case shift < 0:
res.Rsh(res, uint(-shift))
}
return (*BigInt)(res), nil
}
func (a Float) M__float__() (Object, error) {
return a, nil
}
func (a Float) M__complex__() (Object, error) {
if r, ok := convertToComplex(a); ok {
return r, nil
}
return cantConvert(a, "complex")
}
func (a Float) M__round__(digitsObj Object) (Object, error) {
digits := 0
if digitsObj != None {
var err error
digits, err = MakeGoInt(digitsObj)
if err != nil {
return nil, err
}
}
scale := Float(math.Pow(10, float64(digits)))
return scale * Float(math.Floor(float64(a)/float64(scale))), nil
}
// Rich comparison
func (a Float) M__lt__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a < b), nil
}
return NotImplemented, nil
}
func (a Float) M__le__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a <= b), nil
}
return NotImplemented, nil
}
func (a Float) M__eq__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a == b), nil
}
return NotImplemented, nil
}
func (a Float) M__ne__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a != b), nil
}
return NotImplemented, nil
}
func (a Float) M__gt__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a > b), nil
}
return NotImplemented, nil
}
func (a Float) M__ge__(other Object) (Object, error) {
if b, ok := convertToFloat(other); ok {
return NewBool(a >= b), nil
}
return NotImplemented, nil
}
// Check interface is satisfied
var _ floatArithmetic = Float(0)
var _ conversionBetweenTypes = Float(0)
var _ I__bool__ = Float(0)
var _ richComparison = Float(0)