Skip to content

Commit eabdf67

Browse files
committed
py: Add function to convert long int to float.
1 parent 8138205 commit eabdf67

5 files changed

Lines changed: 39 additions & 4 deletions

File tree

py/obj.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
176176
return 1;
177177
} else if (MP_OBJ_IS_SMALL_INT(arg)) {
178178
return MP_OBJ_SMALL_INT_VALUE(arg);
179+
} else if (MP_OBJ_IS_TYPE(arg, &int_type)) {
180+
return mp_obj_int_as_float(arg);
179181
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
180182
return mp_obj_float_get(arg);
181183
} else {

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
330330
extern const mp_obj_type_t int_type;
331331
// For long int, returns value truncated to machine_int_t
332332
machine_int_t mp_obj_int_get(mp_obj_t self_in);
333+
#if MICROPY_ENABLE_FLOAT
334+
mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
335+
#endif
333336
// Will rains exception if value doesn't fit into machine_int_t
334337
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
335338

py/objint.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
101101
return MP_OBJ_SMALL_INT_VALUE(self_in);
102102
}
103103

104+
#if MICROPY_ENABLE_FLOAT
105+
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
106+
return MP_OBJ_SMALL_INT_VALUE(self_in);
107+
}
108+
#endif
109+
104110
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
105111

106112
const mp_obj_type_t int_type = {

py/objint_longlong.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,26 @@ mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
153153
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
154154
if (MP_OBJ_IS_SMALL_INT(self_in)) {
155155
return MP_OBJ_SMALL_INT_VALUE(self_in);
156+
} else {
157+
mp_obj_int_t *self = self_in;
158+
return self->val;
156159
}
157-
mp_obj_int_t *self = self_in;
158-
return self->val;
159160
}
160161

161162
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
162163
// TODO: Check overflow
163164
return mp_obj_int_get(self_in);
164165
}
165166

167+
#if MICROPY_ENABLE_FLOAT
168+
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
169+
if (MP_OBJ_IS_SMALL_INT(self_in)) {
170+
return MP_OBJ_SMALL_INT_VALUE(self_in);
171+
} else {
172+
mp_obj_int_t *self = self_in;
173+
return self->val;
174+
}
175+
}
176+
#endif
177+
166178
#endif

py/objint_mpz.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,26 @@ mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
215215
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
216216
if (MP_OBJ_IS_SMALL_INT(self_in)) {
217217
return MP_OBJ_SMALL_INT_VALUE(self_in);
218+
} else {
219+
mp_obj_int_t *self = self_in;
220+
return mpz_as_int(&self->mpz);
218221
}
219-
mp_obj_int_t *self = self_in;
220-
return mpz_as_int(&self->mpz);
221222
}
222223

223224
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
224225
// TODO: Check overflow
225226
return mp_obj_int_get(self_in);
226227
}
227228

229+
#if MICROPY_ENABLE_FLOAT
230+
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
231+
if (MP_OBJ_IS_SMALL_INT(self_in)) {
232+
return MP_OBJ_SMALL_INT_VALUE(self_in);
233+
} else {
234+
mp_obj_int_t *self = self_in;
235+
return mpz_as_float(&self->mpz);
236+
}
237+
}
238+
#endif
239+
228240
#endif

0 commit comments

Comments
 (0)