Skip to content

Commit cbcf7c3

Browse files
Merge pull request #534 from AdamGS/new_float_attributes
Addred real property and is_integer function to float.
2 parents d44f1b3 + f4e6e5d commit cbcf7c3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tests/snippets/floats.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@
8383
assert 1.0.__rtruediv__(2) == 2.0
8484
assert 2.0.__mul__(1) == 2.0
8585
assert 2.0.__rsub__(1) == -1.0
86+
87+
assert (1.7).real == 1.7
88+
assert (1.3).is_integer() == False
89+
assert (1.0).is_integer() == True

vm/src/obj/objfloat.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,19 @@ fn float_rmul(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
418418
float_mul(vm, args)
419419
}
420420

421+
fn float_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
422+
arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]);
423+
let v = get_value(i);
424+
Ok(vm.ctx.new_float(v))
425+
}
426+
427+
fn float_is_integer(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
428+
arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]);
429+
let v = get_value(i);
430+
let result = v == v.round();
431+
Ok(vm.ctx.new_bool(result))
432+
}
433+
421434
pub fn init(context: &PyContext) {
422435
let float_type = &context.float_type;
423436

@@ -465,4 +478,10 @@ pub fn init(context: &PyContext) {
465478
);
466479
context.set_attr(&float_type, "__mul__", context.new_rustfunc(float_mul));
467480
context.set_attr(&float_type, "__rmul__", context.new_rustfunc(float_rmul));
481+
context.set_attr(&float_type, "real", context.new_property(float_real));
482+
context.set_attr(
483+
&float_type,
484+
"is_integer",
485+
context.new_rustfunc(float_is_integer),
486+
);
468487
}

0 commit comments

Comments
 (0)