Skip to content

Commit a6f64ee

Browse files
committed
Change int __pow__ to use bigint pow.
1 parent 081a33f commit a6f64ee

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/snippets/builtin_bin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
assert bin(-(2**24)) == '-0b1' + '0' * 24
77
assert bin(-(2**24-1)) == '-0b' + '1' * 24
88

9-
# TODO: change to 2**65 when we have support for pow in bigint
10-
a = 2**20 * 2**20 * 2**25
9+
a = 2 ** 65
1110
assert bin(a) == '0b1' + '0' * 65
1211
assert bin(a-1) == '0b' + '1' * 65
1312
assert bin(-(a)) == '-0b1' + '0' * 65

tests/snippets/whats_left_to_implement.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,11 @@
873873
not_implemented.append(("bytes", method))
874874

875875
for method in complex_expected_methods:
876-
# TODO: uncomment this when complex is implemented
877-
# try:
878-
# if not hasattr(complex, method):
879-
# not_implemented.append(("complex", method))
880-
# except NameError:
881-
not_implemented.append(("complex", method))
876+
try:
877+
if not hasattr(complex, method):
878+
not_implemented.append(("complex", method))
879+
except NameError:
880+
not_implemented.append(("complex", method))
882881

883882
for method in dict_expected_methods:
884883
try:

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Shing Lyu <shing.lyu@gmail.com>"]
66
[dependencies]
77
bitflags = "1.0.4"
88
num-complex = "0.2"
9-
num-bigint = "0.2"
9+
num-bigint = "0.2.1"
1010
num-traits = "0.2"
1111
log = "0.3"
1212
rustpython_parser = {path = "../parser"}

vm/src/obj/objcomplex.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn init(context: &PyContext) {
1212
complex_type.set_attr("__add__", context.new_rustfunc(complex_add));
1313
complex_type.set_attr("__new__", context.new_rustfunc(complex_new));
1414
complex_type.set_attr("__repr__", context.new_rustfunc(complex_repr));
15+
complex_type.set_attr("conjugate", context.new_rustfunc(complex_conjugate));
1516
}
1617

1718
pub fn get_value(obj: &PyObjectRef) -> Complex64 {
@@ -64,6 +65,13 @@ fn complex_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6465
}
6566
}
6667

68+
fn complex_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
69+
arg_check!(vm, args, required = [(i, Some(vm.ctx.complex_type()))]);
70+
71+
let v1 = get_value(i);
72+
Ok(vm.ctx.new_complex(v1.conj()))
73+
}
74+
6775
fn complex_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6876
arg_check!(vm, args, required = [(obj, Some(vm.ctx.complex_type()))]);
6977
let v = get_value(obj);

vm/src/obj/objint.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::objfloat;
77
use super::objstr;
88
use super::objtype;
99
use num_bigint::{BigInt, ToBigInt};
10-
use num_traits::{Signed, ToPrimitive, Zero};
10+
use num_traits::{Pow, Signed, ToPrimitive, Zero};
1111

1212
// This proxy allows for easy switching between types.
1313
// TODO: maybe this is a good idea:
@@ -296,10 +296,7 @@ fn int_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
296296
let v1 = get_value(i);
297297
if objtype::isinstance(i2, &vm.ctx.int_type()) {
298298
let v2 = get_value(i2).to_u32().unwrap();
299-
// TODO: look for bigint pow method
300-
Ok(vm
301-
.ctx
302-
.new_int(v1.to_i32().unwrap().pow(v2).to_bigint().unwrap()))
299+
Ok(vm.ctx.new_int(v1.pow(v2).to_bigint().unwrap()))
303300
} else if objtype::isinstance(i2, &vm.ctx.float_type()) {
304301
let v2 = objfloat::get_value(i2);
305302
Ok(vm.ctx.new_float((v1.to_f64().unwrap()).powf(v2)))

0 commit comments

Comments
 (0)