Skip to content

Commit 7f9c32e

Browse files
committed
codal_port/modmicrobit: Make scale return int if to-tuple is all ints.
Addresses issue #121. Signed-off-by: Damien George <[email protected]>
1 parent 1641d15 commit 7f9c32e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/codal_port/modmicrobit.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <math.h>
2728
#include "py/obj.h"
2829
#include "py/mphal.h"
2930
#include "drv_softtimer.h"
@@ -152,7 +153,12 @@ STATIC mp_obj_t microbit_scale(size_t n_args, const mp_obj_t *pos_args, mp_map_t
152153
// Compute scaled value.
153154
mp_float_t to_value = (from_value - from_min) / (from_max - from_min) * (to_max - to_min) + to_min;
154155

155-
return mp_obj_new_float(to_value);
156+
// Return float or int based on type of "to" tuple.
157+
if (mp_obj_is_float(to_items[0]) || mp_obj_is_float(to_items[1])) {
158+
return mp_obj_new_float(to_value);
159+
} else {
160+
return mp_obj_new_int(MICROPY_FLOAT_C_FUN(nearbyint)(to_value));
161+
}
156162
}
157163
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(microbit_scale_obj, 0, microbit_scale);
158164

0 commit comments

Comments
 (0)