@@ -61,6 +61,11 @@ fn float_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
61
61
Ok ( vm. ctx . new_bool ( result) )
62
62
}
63
63
64
+ fn float_abs ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
65
+ arg_check ! ( vm, args, required = [ ( i, Some ( vm. ctx. float_type( ) ) ) ] ) ;
66
+ Ok ( vm. ctx . new_float ( get_value ( i) . abs ( ) ) )
67
+ }
68
+
64
69
fn float_add ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
65
70
arg_check ! (
66
71
vm,
@@ -78,13 +83,45 @@ fn float_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
78
83
}
79
84
}
80
85
81
- fn float_sub ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
86
+ fn float_divmod ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
82
87
arg_check ! (
83
88
vm,
84
89
args,
85
90
required = [ ( i, Some ( vm. ctx. float_type( ) ) ) , ( i2, None ) ]
86
91
) ;
92
+ let args = PyFuncArgs :: new ( vec ! [ i. clone( ) , i2. clone( ) ] , vec ! [ ] ) ;
93
+ if objtype:: isinstance ( i2, vm. ctx . float_type ( ) ) || objtype:: isinstance ( i2, vm. ctx . int_type ( ) ) {
94
+ let r1 = float_floordiv ( vm, args. clone ( ) ) ;
95
+ let r2 = float_mod ( vm, args. clone ( ) ) ;
96
+ Ok ( vm. ctx . new_tuple ( vec ! [ r1. unwrap( ) , r2. unwrap( ) ] ) )
97
+ } else {
98
+ Err ( vm. new_type_error ( format ! ( "Cannot divmod power {:?} and {:?}" , i, i2) ) )
99
+ }
100
+ }
101
+
102
+ fn float_floordiv ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
103
+ arg_check ! (
104
+ vm,
105
+ args,
106
+ required = [ ( i, Some ( vm. ctx. float_type( ) ) ) , ( i2, None ) ]
107
+ ) ;
108
+ if objtype:: isinstance ( i2, vm. ctx . float_type ( ) ) {
109
+ Ok ( vm. ctx . new_float ( ( get_value ( i) / get_value ( i2) ) . floor ( ) ) )
110
+ } else if objtype:: isinstance ( i2, vm. ctx . int_type ( ) ) {
111
+ Ok ( vm
112
+ . ctx
113
+ . new_float ( ( get_value ( i) / objint:: get_value ( i2) as f64 ) . floor ( ) ) )
114
+ } else {
115
+ Err ( vm. new_type_error ( format ! ( "Cannot floordiv {:?} and {:?}" , i, i2) ) )
116
+ }
117
+ }
87
118
119
+ fn float_sub ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
120
+ arg_check ! (
121
+ vm,
122
+ args,
123
+ required = [ ( i, Some ( vm. ctx. float_type( ) ) ) , ( i2, None ) ]
124
+ ) ;
88
125
let v1 = get_value ( i) ;
89
126
if objtype:: isinstance ( i2, vm. ctx . float_type ( ) ) {
90
127
Ok ( vm. ctx . new_float ( v1 - get_value ( i2) ) )
@@ -95,6 +132,23 @@ fn float_sub(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
95
132
}
96
133
}
97
134
135
+ fn float_mod ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
136
+ arg_check ! (
137
+ vm,
138
+ args,
139
+ required = [ ( i, Some ( vm. ctx. float_type( ) ) ) , ( i2, None ) ]
140
+ ) ;
141
+ if objtype:: isinstance ( i2, vm. ctx . float_type ( ) ) {
142
+ Ok ( vm. ctx . new_float ( get_value ( i) % get_value ( i2) ) )
143
+ } else if objtype:: isinstance ( i2, vm. ctx . int_type ( ) ) {
144
+ Ok ( vm
145
+ . ctx
146
+ . new_float ( get_value ( i) % objint:: get_value ( i2) as f64 ) )
147
+ } else {
148
+ Err ( vm. new_type_error ( format ! ( "Cannot mod {:?} and {:?}" , i, i2) ) )
149
+ }
150
+ }
151
+
98
152
fn float_pow ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
99
153
arg_check ! (
100
154
vm,
@@ -117,8 +171,12 @@ fn float_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
117
171
pub fn init ( context : & PyContext ) {
118
172
let ref float_type = context. float_type ;
119
173
float_type. set_attr ( "__eq__" , context. new_rustfunc ( float_eq) ) ;
174
+ float_type. set_attr ( "__abs__" , context. new_rustfunc ( float_abs) ) ;
120
175
float_type. set_attr ( "__add__" , context. new_rustfunc ( float_add) ) ;
176
+ float_type. set_attr ( "__divmod__" , context. new_rustfunc ( float_divmod) ) ;
177
+ float_type. set_attr ( "__floordiv__" , context. new_rustfunc ( float_floordiv) ) ;
121
178
float_type. set_attr ( "__init__" , context. new_rustfunc ( float_init) ) ;
179
+ float_type. set_attr ( "__mod__" , context. new_rustfunc ( float_mod) ) ;
122
180
float_type. set_attr ( "__pow__" , context. new_rustfunc ( float_pow) ) ;
123
181
float_type. set_attr ( "__sub__" , context. new_rustfunc ( float_sub) ) ;
124
182
float_type. set_attr ( "__repr__" , context. new_rustfunc ( float_repr) ) ;
0 commit comments