summaryrefslogtreecommitdiff
path: root/py/objcomplex.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-18 00:06:43 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-18 00:06:43 +0300
commit9dce823cfd0a9991350184f08a1373f3887134f4 (patch)
tree214ca2cd6d0a6c2a93f3f76dd2f5c76857f3a49f /py/objcomplex.c
parent72491b3e40db75e7edf5831e8914a1ca5c8e9937 (diff)
py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS.
This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative.
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r--py/objcomplex.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 07b9d5d41..e7a3c244a 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -124,6 +124,11 @@ STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
+ case MP_UNARY_OP_ABS: {
+ mp_float_t real, imag;
+ mp_obj_complex_get(o_in, &real, &imag);
+ return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
+ }
default: return MP_OBJ_NULL; // op not supported
}
}