@@ -14,8 +14,8 @@ use std;
14
14
macro_rules! make_math_func {
15
15
( $fname: ident, $fun: ident ) => {
16
16
fn $fname( vm: & mut VirtualMachine , args: PyFuncArgs ) -> PyResult {
17
- arg_check!( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
18
- let value = objfloat:: get_value ( value) ;
17
+ arg_check!( vm, args, required = [ ( value, None ) ] ) ;
18
+ let value = objfloat:: make_float ( vm , value) ? ;
19
19
let value = value. $fun( ) ;
20
20
let value = vm. ctx. new_float( value) ;
21
21
Ok ( value)
@@ -27,20 +27,20 @@ macro_rules! make_math_func {
27
27
make_math_func ! ( math_fabs, abs) ;
28
28
29
29
fn math_isfinite ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
30
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
31
- let value = objfloat:: get_value ( value) . is_finite ( ) ;
30
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
31
+ let value = objfloat:: make_float ( vm , value) ? . is_finite ( ) ;
32
32
Ok ( vm. ctx . new_bool ( value) )
33
33
}
34
34
35
35
fn math_isinf ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
36
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
37
- let value = objfloat:: get_value ( value) . is_infinite ( ) ;
36
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
37
+ let value = objfloat:: make_float ( vm , value) ? . is_infinite ( ) ;
38
38
Ok ( vm. ctx . new_bool ( value) )
39
39
}
40
40
41
41
fn math_isnan ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
42
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
43
- let value = objfloat:: get_value ( value) . is_nan ( ) ;
42
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
43
+ let value = objfloat:: make_float ( vm , value) ? . is_nan ( ) ;
44
44
Ok ( vm. ctx . new_bool ( value) )
45
45
}
46
46
@@ -49,42 +49,30 @@ make_math_func!(math_exp, exp);
49
49
make_math_func ! ( math_expm1, exp_m1) ;
50
50
51
51
fn math_log ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
52
- arg_check ! (
53
- vm,
54
- args,
55
- required = [ ( x, Some ( vm. ctx. float_type( ) ) ) ] ,
56
- optional = [ ( base, Some ( vm. ctx. float_type( ) ) ) ]
57
- ) ;
58
- let x = objfloat:: get_value ( x) ;
52
+ arg_check ! ( vm, args, required = [ ( x, None ) ] , optional = [ ( base, None ) ] ) ;
53
+ let x = objfloat:: make_float ( vm, x) ?;
59
54
match base {
60
55
None => Ok ( vm. ctx . new_float ( x. ln ( ) ) ) ,
61
56
Some ( base) => {
62
- let base = objfloat:: get_value ( base) ;
57
+ let base = objfloat:: make_float ( vm , base) ? ;
63
58
Ok ( vm. ctx . new_float ( x. log ( base) ) )
64
59
}
65
60
}
66
61
}
67
62
68
63
fn math_log1p ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
69
- arg_check ! ( vm, args, required = [ ( x, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
70
- let x = objfloat:: get_value ( x ) ;
64
+ arg_check ! ( vm, args, required = [ ( x, None ) ] ) ;
65
+ let x = objfloat:: make_float ( vm , x ) ? ;
71
66
Ok ( vm. ctx . new_float ( ( x + 1.0 ) . ln ( ) ) )
72
67
}
73
68
74
69
make_math_func ! ( math_log2, log2) ;
75
70
make_math_func ! ( math_log10, log10) ;
76
71
77
72
fn math_pow ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
78
- arg_check ! (
79
- vm,
80
- args,
81
- required = [
82
- ( x, Some ( vm. ctx. float_type( ) ) ) ,
83
- ( y, Some ( vm. ctx. float_type( ) ) )
84
- ]
85
- ) ;
86
- let x = objfloat:: get_value ( x) ;
87
- let y = objfloat:: get_value ( y) ;
73
+ arg_check ! ( vm, args, required = [ ( x, None ) , ( y, None ) ] ) ;
74
+ let x = objfloat:: make_float ( vm, x) ?;
75
+ let y = objfloat:: make_float ( vm, y) ?;
88
76
Ok ( vm. ctx . new_float ( x. powf ( y) ) )
89
77
}
90
78
@@ -96,47 +84,33 @@ make_math_func!(math_asin, asin);
96
84
make_math_func ! ( math_atan, atan) ;
97
85
98
86
fn math_atan2 ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
99
- arg_check ! (
100
- vm,
101
- args,
102
- required = [
103
- ( y, Some ( vm. ctx. float_type( ) ) ) ,
104
- ( x, Some ( vm. ctx. float_type( ) ) )
105
- ]
106
- ) ;
107
- let y = objfloat:: get_value ( y) ;
108
- let x = objfloat:: get_value ( x) ;
87
+ arg_check ! ( vm, args, required = [ ( y, None ) , ( x, None ) ] ) ;
88
+ let y = objfloat:: make_float ( vm, y) ?;
89
+ let x = objfloat:: make_float ( vm, x) ?;
109
90
Ok ( vm. ctx . new_float ( y. atan2 ( x) ) )
110
91
}
111
92
112
93
make_math_func ! ( math_cos, cos) ;
113
94
114
95
fn math_hypot ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
115
- arg_check ! (
116
- vm,
117
- args,
118
- required = [
119
- ( x, Some ( vm. ctx. float_type( ) ) ) ,
120
- ( y, Some ( vm. ctx. float_type( ) ) )
121
- ]
122
- ) ;
123
- let x = objfloat:: get_value ( x) ;
124
- let y = objfloat:: get_value ( y) ;
96
+ arg_check ! ( vm, args, required = [ ( x, None ) , ( y, None ) ] ) ;
97
+ let x = objfloat:: make_float ( vm, x) ?;
98
+ let y = objfloat:: make_float ( vm, y) ?;
125
99
Ok ( vm. ctx . new_float ( x. hypot ( y) ) )
126
100
}
127
101
128
102
make_math_func ! ( math_sin, sin) ;
129
103
make_math_func ! ( math_tan, tan) ;
130
104
131
105
fn math_degrees ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
132
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
133
- let x = objfloat:: get_value ( value) ;
106
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
107
+ let x = objfloat:: make_float ( vm , value) ? ;
134
108
Ok ( vm. ctx . new_float ( x * ( 180.0 / std:: f64:: consts:: PI ) ) )
135
109
}
136
110
137
111
fn math_radians ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
138
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
139
- let x = objfloat:: get_value ( value) ;
112
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
113
+ let x = objfloat:: make_float ( vm , value) ? ;
140
114
Ok ( vm. ctx . new_float ( x * ( std:: f64:: consts:: PI / 180.0 ) ) )
141
115
}
142
116
@@ -150,8 +124,8 @@ make_math_func!(math_tanh, tanh);
150
124
151
125
// Special functions:
152
126
fn math_erf ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
153
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
154
- let x = objfloat:: get_value ( value) ;
127
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
128
+ let x = objfloat:: make_float ( vm , value) ? ;
155
129
156
130
if x. is_nan ( ) {
157
131
Ok ( vm. ctx . new_float ( x) )
@@ -161,8 +135,8 @@ fn math_erf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
161
135
}
162
136
163
137
fn math_erfc ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
164
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
165
- let x = objfloat:: get_value ( value) ;
138
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
139
+ let x = objfloat:: make_float ( vm , value) ? ;
166
140
167
141
if x. is_nan ( ) {
168
142
Ok ( vm. ctx . new_float ( x) )
@@ -172,32 +146,28 @@ fn math_erfc(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172
146
}
173
147
174
148
fn math_gamma ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
175
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
176
- let x = objfloat:: get_value ( value) ;
149
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
150
+ let x = objfloat:: make_float ( vm , value) ? ;
177
151
178
152
if x. is_finite ( ) {
179
153
Ok ( vm. ctx . new_float ( gamma ( x) ) )
154
+ } else if x. is_nan ( ) || x. is_sign_positive ( ) {
155
+ Ok ( vm. ctx . new_float ( x) )
180
156
} else {
181
- if x. is_nan ( ) || x. is_sign_positive ( ) {
182
- Ok ( vm. ctx . new_float ( x) )
183
- } else {
184
- Ok ( vm. ctx . new_float ( std:: f64:: NAN ) )
185
- }
157
+ Ok ( vm. ctx . new_float ( std:: f64:: NAN ) )
186
158
}
187
159
}
188
160
189
161
fn math_lgamma ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
190
- arg_check ! ( vm, args, required = [ ( value, Some ( vm . ctx . float_type ( ) ) ) ] ) ;
191
- let x = objfloat:: get_value ( value) ;
162
+ arg_check ! ( vm, args, required = [ ( value, None ) ] ) ;
163
+ let x = objfloat:: make_float ( vm , value) ? ;
192
164
193
165
if x. is_finite ( ) {
194
166
Ok ( vm. ctx . new_float ( ln_gamma ( x) ) )
167
+ } else if x. is_nan ( ) {
168
+ Ok ( vm. ctx . new_float ( x) )
195
169
} else {
196
- if x. is_nan ( ) {
197
- Ok ( vm. ctx . new_float ( x) )
198
- } else {
199
- Ok ( vm. ctx . new_float ( std:: f64:: INFINITY ) )
200
- }
170
+ Ok ( vm. ctx . new_float ( std:: f64:: INFINITY ) )
201
171
}
202
172
}
203
173
0 commit comments