Skip to content

Commit 87ceed7

Browse files
Merge pull request RustPython#287 from makarchuk/float-conversion
Float Conversion for RustPython#211
2 parents 69e3571 + 74b84d4 commit 87ceed7

File tree

2 files changed

+43
-61
lines changed

2 files changed

+43
-61
lines changed

tests/snippets/math.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
assert +a == 4
1919

2020
# import math
21-
# print(math.cos(1.2))
21+
# assert(math.exp(2) == math.exp(2.0))
22+
# assert(math.exp(True) == math.exp(1.0))
23+
#
24+
# class Conversible():
25+
# def __float__(self):
26+
# print("Converting to float now!")
27+
# return 1.1111
28+
#
29+
# assert math.log(1.1111) == math.log(Conversible())

vm/src/stdlib/math.rs

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std;
1414
macro_rules! make_math_func {
1515
( $fname:ident, $fun:ident ) => {
1616
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)?;
1919
let value = value.$fun();
2020
let value = vm.ctx.new_float(value);
2121
Ok(value)
@@ -27,20 +27,20 @@ macro_rules! make_math_func {
2727
make_math_func!(math_fabs, abs);
2828

2929
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();
3232
Ok(vm.ctx.new_bool(value))
3333
}
3434

3535
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();
3838
Ok(vm.ctx.new_bool(value))
3939
}
4040

4141
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();
4444
Ok(vm.ctx.new_bool(value))
4545
}
4646

@@ -49,42 +49,30 @@ make_math_func!(math_exp, exp);
4949
make_math_func!(math_expm1, exp_m1);
5050

5151
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)?;
5954
match base {
6055
None => Ok(vm.ctx.new_float(x.ln())),
6156
Some(base) => {
62-
let base = objfloat::get_value(base);
57+
let base = objfloat::make_float(vm, base)?;
6358
Ok(vm.ctx.new_float(x.log(base)))
6459
}
6560
}
6661
}
6762

6863
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)?;
7166
Ok(vm.ctx.new_float((x + 1.0).ln()))
7267
}
7368

7469
make_math_func!(math_log2, log2);
7570
make_math_func!(math_log10, log10);
7671

7772
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)?;
8876
Ok(vm.ctx.new_float(x.powf(y)))
8977
}
9078

@@ -96,47 +84,33 @@ make_math_func!(math_asin, asin);
9684
make_math_func!(math_atan, atan);
9785

9886
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)?;
10990
Ok(vm.ctx.new_float(y.atan2(x)))
11091
}
11192

11293
make_math_func!(math_cos, cos);
11394

11495
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)?;
12599
Ok(vm.ctx.new_float(x.hypot(y)))
126100
}
127101

128102
make_math_func!(math_sin, sin);
129103
make_math_func!(math_tan, tan);
130104

131105
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)?;
134108
Ok(vm.ctx.new_float(x * (180.0 / std::f64::consts::PI)))
135109
}
136110

137111
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)?;
140114
Ok(vm.ctx.new_float(x * (std::f64::consts::PI / 180.0)))
141115
}
142116

@@ -150,8 +124,8 @@ make_math_func!(math_tanh, tanh);
150124

151125
// Special functions:
152126
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)?;
155129

156130
if x.is_nan() {
157131
Ok(vm.ctx.new_float(x))
@@ -161,8 +135,8 @@ fn math_erf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
161135
}
162136

163137
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)?;
166140

167141
if x.is_nan() {
168142
Ok(vm.ctx.new_float(x))
@@ -172,8 +146,8 @@ fn math_erfc(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172146
}
173147

174148
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)?;
177151

178152
if x.is_finite() {
179153
Ok(vm.ctx.new_float(gamma(x)))
@@ -187,8 +161,8 @@ fn math_gamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
187161
}
188162

189163
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);
164+
arg_check!(vm, args, required = [(value, None)]);
165+
let x = objfloat::make_float(vm, value)?;
192166

193167
if x.is_finite() {
194168
Ok(vm.ctx.new_float(ln_gamma(x)))

0 commit comments

Comments
 (0)