aboutsummaryrefslogtreecommitdiff
path: root/py/objcomplex.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r--py/objcomplex.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 2031c1c03..b7af1b16b 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -45,17 +45,17 @@ typedef struct _mp_obj_complex_t {
STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
-#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
+ #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char buf[16];
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
const int precision = 6;
#else
const int precision = 7;
#endif
-#else
+ #else
char buf[32];
const int precision = 16;
-#endif
+ #endif
if (o->real == 0) {
mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0');
mp_printf(print, "%sj", buf);
@@ -117,13 +117,18 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si
STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
switch (op) {
- case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->real != 0 || o->imag != 0);
- 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_BOOL:
+ return mp_obj_new_bool(o->real != 0 || o->imag != 0);
+ 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:
- return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real*o->real + o->imag*o->imag));
- default: return MP_OBJ_NULL; // op not supported
+ return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real * o->real + o->imag * o->imag));
+ default:
+ return MP_OBJ_NULL; // op not supported
}
}
@@ -188,7 +193,7 @@ mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_flo
case MP_BINARY_OP_MULTIPLY:
case MP_BINARY_OP_INPLACE_MULTIPLY: {
mp_float_t real;
- multiply:
+ multiply:
real = lhs_real * rhs_real - lhs_imag * rhs_imag;
lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
lhs_real = real;
@@ -211,7 +216,7 @@ mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_flo
lhs_imag = -lhs_real / rhs_imag;
lhs_real = real;
} else {
- mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
+ mp_float_t rhs_len_sq = rhs_real * rhs_real + rhs_imag * rhs_imag;
rhs_real /= rhs_len_sq;
rhs_imag /= -rhs_len_sq;
goto multiply;
@@ -225,7 +230,7 @@ mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_flo
// = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
// = exp(x3 + i*y3)
// = exp(x3)*(cos(y3) + i*sin(y3))
- mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
+ mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real * lhs_real + lhs_imag * lhs_imag);
if (abs1 == 0) {
if (rhs_imag == 0 && rhs_real >= 0) {
lhs_real = (rhs_real == 0);
@@ -244,7 +249,8 @@ mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_flo
break;
}
- case MP_BINARY_OP_EQUAL: return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
+ case MP_BINARY_OP_EQUAL:
+ return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
default:
return MP_OBJ_NULL; // op not supported