Skip to content

Commit aedb859

Browse files
committed
py: Make float representation configurable with object representation.
1 parent 7e359c6 commit aedb859

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

py/obj.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
4444
return (mp_obj_t)&mp_type_int;
4545
} else if (MP_OBJ_IS_QSTR(o_in)) {
4646
return (mp_obj_t)&mp_type_str;
47+
#if MICROPY_PY_BUILTINS_FLOAT
48+
} else if (mp_obj_is_float(o_in)) {
49+
return (mp_obj_t)&mp_type_float;
50+
#endif
4751
} else {
4852
const mp_obj_base_t *o = o_in;
4953
return (mp_obj_t)o->type;

py/obj.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
8383
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
8484
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
8585

86+
#if MICROPY_PY_BUILTINS_FLOAT
87+
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
88+
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
89+
extern const struct _mp_obj_float_t mp_const_float_e_obj;
90+
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
91+
92+
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
93+
mp_float_t mp_obj_float_get(mp_obj_t self_in);
94+
mp_obj_t mp_obj_new_float(mp_float_t value);
95+
#endif
96+
8697
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
8798
{ return ((((mp_int_t)(o)) & 3) == 0); }
8899

@@ -98,6 +109,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
98109
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
99110
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
100111

112+
#if MICROPY_PY_BUILTINS_FLOAT
113+
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
114+
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
115+
extern const struct _mp_obj_float_t mp_const_float_e_obj;
116+
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
117+
118+
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
119+
mp_float_t mp_obj_float_get(mp_obj_t self_in);
120+
mp_obj_t mp_obj_new_float(mp_float_t value);
121+
#endif
122+
101123
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
102124
{ return ((((mp_int_t)(o)) & 1) == 0); }
103125

@@ -473,7 +495,6 @@ mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
473495
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
474496
#if MICROPY_PY_BUILTINS_FLOAT
475497
mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
476-
mp_obj_t mp_obj_new_float(mp_float_t val);
477498
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
478499
#endif
479500
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
@@ -564,13 +585,6 @@ void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_
564585

565586
#if MICROPY_PY_BUILTINS_FLOAT
566587
// float
567-
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
568-
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
569-
extern const struct _mp_obj_float_t mp_const_float_e_obj;
570-
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
571-
572-
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
573-
mp_float_t mp_obj_float_get(mp_obj_t self_in);
574588
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
575589

576590
// complex

py/objfloat.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, M_PI};
4949

5050
STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
5151
(void)kind;
52-
mp_obj_float_t *o = o_in;
52+
mp_float_t o_val = mp_obj_float_get(o_in);
5353
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
5454
char buf[16];
5555
const int precision = 7;
5656
#else
5757
char buf[32];
5858
const int precision = 16;
5959
#endif
60-
mp_format_float(o->value, buf, sizeof(buf), 'g', precision, '\0');
60+
mp_format_float(o_val, buf, sizeof(buf), 'g', precision, '\0');
6161
mp_print_str(print, buf);
6262
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
6363
// 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
9191
}
9292

9393
STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
94-
mp_obj_float_t *o = o_in;
94+
mp_float_t val = mp_obj_float_get(o_in);
9595
switch (op) {
96-
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->value != 0);
96+
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(val != 0);
9797
case MP_UNARY_OP_POSITIVE: return o_in;
98-
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
98+
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
9999
default: return MP_OBJ_NULL; // op not supported
100100
}
101101
}
102102

103103
STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
104-
mp_obj_float_t *lhs = lhs_in;
104+
mp_float_t lhs_val = mp_obj_float_get(lhs_in);
105105
#if MICROPY_PY_BUILTINS_COMPLEX
106106
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
107-
return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
107+
return mp_obj_complex_binary_op(op, lhs_val, 0, rhs_in);
108108
} else
109109
#endif
110110
{
111-
return mp_obj_float_binary_op(op, lhs->value, rhs_in);
111+
return mp_obj_float_binary_op(op, lhs_val, rhs_in);
112112
}
113113
}
114114

0 commit comments

Comments
 (0)