py: Make float representation configurable with object representation.

This commit is contained in:
Damien George
2015-10-17 22:57:34 +01:00
parent 7e359c648b
commit aedb859177
3 changed files with 34 additions and 16 deletions

View File

@@ -49,7 +49,7 @@ const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, M_PI};
STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_float_t *o = o_in;
mp_float_t o_val = mp_obj_float_get(o_in);
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char buf[16];
const int precision = 7;
@@ -57,7 +57,7 @@ STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
char buf[32];
const int precision = 16;
#endif
mp_format_float(o->value, buf, sizeof(buf), 'g', precision, '\0');
mp_format_float(o_val, buf, sizeof(buf), 'g', precision, '\0');
mp_print_str(print, buf);
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
// Python floats always have decimal point (unless inf or nan)
@@ -91,24 +91,24 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
}
STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
mp_obj_float_t *o = o_in;
mp_float_t val = mp_obj_float_get(o_in);
switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->value != 0);
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(val != 0);
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
default: return MP_OBJ_NULL; // op not supported
}
}
STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_float_t *lhs = lhs_in;
mp_float_t lhs_val = mp_obj_float_get(lhs_in);
#if MICROPY_PY_BUILTINS_COMPLEX
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
return mp_obj_complex_binary_op(op, lhs_val, 0, rhs_in);
} else
#endif
{
return mp_obj_float_binary_op(op, lhs->value, rhs_in);
return mp_obj_float_binary_op(op, lhs_val, rhs_in);
}
}