Skip to content

Commit c1b8923

Browse files
committed
More arithmetic operations
1 parent c8d406d commit c1b8923

File tree

4 files changed

+518
-26
lines changed

4 files changed

+518
-26
lines changed

py/complex.go

+147-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,157 @@
22

33
package py
44

5+
import (
6+
"math"
7+
)
8+
59
var ComplexType = NewType("complex64")
610

7-
type Complex complex64
11+
type Complex complex128
812

913
// Type of this Complex object
1014
func (o Complex) Type() *Type {
1115
return ComplexType
1216
}
17+
18+
// Convert an Object to an Complex
19+
//
20+
// Retrurns ok as to whether the conversion worked or not
21+
func convertToComplex(other Object) (Complex, bool) {
22+
switch b := other.(type) {
23+
case Complex:
24+
return b, true
25+
case Float:
26+
return Complex(complex(b, 0)), true
27+
case Int:
28+
return Complex(complex(float64(b), 0)), true
29+
case Bool:
30+
if b {
31+
return Complex(1), true
32+
} else {
33+
return Complex(0), true
34+
}
35+
}
36+
return 0, false
37+
}
38+
39+
func (a Complex) M__add__(other Object) Object {
40+
if b, ok := convertToComplex(other); ok {
41+
return Complex(a + b)
42+
}
43+
return NotImplemented
44+
}
45+
46+
func (a Complex) M__radd__(other Object) Object {
47+
return a.M__add__(other)
48+
}
49+
50+
func (a Complex) M__iadd__(other Object) Object {
51+
return a.M__add__(other)
52+
}
53+
54+
func (a Complex) M__sub__(other Object) Object {
55+
if b, ok := convertToComplex(other); ok {
56+
return Complex(a - b)
57+
}
58+
return NotImplemented
59+
}
60+
61+
func (a Complex) M__rsub__(other Object) Object {
62+
if b, ok := convertToComplex(other); ok {
63+
return Complex(b - a)
64+
}
65+
return NotImplemented
66+
}
67+
68+
func (a Complex) M__isub(other Object) Object {
69+
return a.M__sub__(other)
70+
}
71+
72+
func (a Complex) M__mul__(other Object) Object {
73+
if b, ok := convertToComplex(other); ok {
74+
return Complex(a * b)
75+
}
76+
return NotImplemented
77+
}
78+
79+
func (a Complex) M__rmul__(other Object) Object {
80+
return a.M__mul__(other)
81+
}
82+
83+
func (a Complex) M__imul__(other Object) Object {
84+
return a.M__mul__(other)
85+
}
86+
87+
func (a Complex) M__truediv__(other Object) Object {
88+
if b, ok := convertToComplex(other); ok {
89+
return Complex(a / b)
90+
}
91+
return NotImplemented
92+
}
93+
94+
func (a Complex) M__rtruediv__(other Object) Object {
95+
if b, ok := convertToComplex(other); ok {
96+
return Complex(b / a)
97+
}
98+
return NotImplemented
99+
}
100+
101+
func (a Complex) M__itruediv(other Object) Object {
102+
return Complex(a).M__truediv__(other)
103+
}
104+
105+
// Floor a complex number
106+
func complexFloor(a Complex) Complex {
107+
return Complex(complex(math.Floor(real(a)), math.Floor(imag(a))))
108+
}
109+
110+
// Floor divide two complex numbers
111+
func complexFloorDiv(a, b Complex) Complex {
112+
q := complexFloor(a / b)
113+
r := a - q*b
114+
return Complex(r)
115+
}
116+
117+
func (a Complex) M__floordiv__(other Object) Object {
118+
if b, ok := convertToComplex(other); ok {
119+
return complexFloor(a / b)
120+
}
121+
return NotImplemented
122+
}
123+
124+
func (a Complex) M__rfloordiv__(other Object) Object {
125+
if b, ok := convertToComplex(other); ok {
126+
return complexFloor(b / a)
127+
}
128+
return NotImplemented
129+
}
130+
131+
func (a Complex) M__ifloordiv(other Object) Object {
132+
return a.M__floordiv__(other)
133+
}
134+
135+
// Does Mod of two floating point numbers
136+
func complexMod(a, b Complex) Complex {
137+
q := complexFloor(a / b)
138+
r := a - Complex(q)*b
139+
return Complex(r)
140+
}
141+
142+
func (a Complex) M__mod__(other Object) Object {
143+
if b, ok := convertToComplex(other); ok {
144+
return complexMod(a, b)
145+
}
146+
return NotImplemented
147+
}
148+
149+
func (a Complex) M__rmod__(other Object) Object {
150+
if b, ok := convertToComplex(other); ok {
151+
return complexMod(b, a)
152+
}
153+
return NotImplemented
154+
}
155+
156+
func (a Complex) M__imod(other Object) Object {
157+
return a.M__mod__(other)
158+
}

py/float.go

+134
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
package py
44

5+
import (
6+
"math"
7+
)
8+
59
var FloatType = NewType("float")
610

711
type Float float64
@@ -10,3 +14,133 @@ type Float float64
1014
func (o Float) Type() *Type {
1115
return FloatType
1216
}
17+
18+
// Arithmetic
19+
20+
// Convert an Object to an Float
21+
//
22+
// Retrurns ok as to whether the conversion worked or not
23+
func convertToFloat(other Object) (Float, bool) {
24+
switch b := other.(type) {
25+
case Float:
26+
return b, true
27+
case Int:
28+
return Float(b), true
29+
case Bool:
30+
if b {
31+
return Float(1), true
32+
} else {
33+
return Float(0), true
34+
}
35+
}
36+
return 0, false
37+
}
38+
39+
func (a Float) M__add__(other Object) Object {
40+
if b, ok := convertToFloat(other); ok {
41+
return Float(a + b)
42+
}
43+
return NotImplemented
44+
}
45+
46+
func (a Float) M__radd__(other Object) Object {
47+
return a.M__add__(other)
48+
}
49+
50+
func (a Float) M__iadd__(other Object) Object {
51+
return a.M__add__(other)
52+
}
53+
54+
func (a Float) M__sub__(other Object) Object {
55+
if b, ok := convertToFloat(other); ok {
56+
return Float(a - b)
57+
}
58+
return NotImplemented
59+
}
60+
61+
func (a Float) M__rsub__(other Object) Object {
62+
if b, ok := convertToFloat(other); ok {
63+
return Float(b - a)
64+
}
65+
return NotImplemented
66+
}
67+
68+
func (a Float) M__isub(other Object) Object {
69+
return a.M__sub__(other)
70+
}
71+
72+
func (a Float) M__mul__(other Object) Object {
73+
if b, ok := convertToFloat(other); ok {
74+
return Float(a * b)
75+
}
76+
return NotImplemented
77+
}
78+
79+
func (a Float) M__rmul__(other Object) Object {
80+
return a.M__mul__(other)
81+
}
82+
83+
func (a Float) M__imul__(other Object) Object {
84+
return a.M__mul__(other)
85+
}
86+
87+
func (a Float) M__truediv__(other Object) Object {
88+
if b, ok := convertToFloat(other); ok {
89+
return Float(a / b)
90+
}
91+
return NotImplemented
92+
}
93+
94+
func (a Float) M__rtruediv__(other Object) Object {
95+
if b, ok := convertToFloat(other); ok {
96+
return Float(b / a)
97+
}
98+
return NotImplemented
99+
}
100+
101+
func (a Float) M__itruediv(other Object) Object {
102+
return Float(a).M__truediv__(other)
103+
}
104+
105+
func (a Float) M__floordiv__(other Object) Object {
106+
if b, ok := convertToFloat(other); ok {
107+
return Float(math.Floor(float64(a / b)))
108+
}
109+
return NotImplemented
110+
}
111+
112+
func (a Float) M__rfloordiv__(other Object) Object {
113+
if b, ok := convertToFloat(other); ok {
114+
return Float(math.Floor(float64(b / a)))
115+
}
116+
return NotImplemented
117+
}
118+
119+
func (a Float) M__ifloordiv(other Object) Object {
120+
return a.M__floordiv__(other)
121+
}
122+
123+
// Does Mod of two floating point numbers
124+
func floatMod(a, b Float) Float {
125+
q := Float(math.Floor(float64(a / b)))
126+
r := a - q*b
127+
return Float(r)
128+
}
129+
130+
func (a Float) M__mod__(other Object) Object {
131+
if b, ok := convertToFloat(other); ok {
132+
return floatMod(a, b)
133+
}
134+
return NotImplemented
135+
}
136+
137+
func (a Float) M__rmod__(other Object) Object {
138+
if b, ok := convertToFloat(other); ok {
139+
return floatMod(b, a)
140+
}
141+
return NotImplemented
142+
}
143+
144+
func (a Float) M__imod(other Object) Object {
145+
return a.M__mod__(other)
146+
}

0 commit comments

Comments
 (0)