summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-11-03 12:33:01 +1100
committerDamien George <damien.p.george@gmail.com>2016-11-03 12:33:01 +1100
commit561844f3ba2dc81ce37c58468099042e27cd422b (patch)
tree95bc16e21265b3a517710325be39a50002b8afa2
parentca973bd3083492777095a07c20965a4644899ec9 (diff)
downloadmicropython-561844f3ba2dc81ce37c58468099042e27cd422b.tar.gz
micropython-561844f3ba2dc81ce37c58468099042e27cd422b.zip
py: Add MICROPY_FLOAT_CONST macro for defining float constants.
All float constants in the core should use this macro to prevent unnecessary creation of double-precision floats, which makes code less efficient.
-rw-r--r--py/modmath.c9
-rw-r--r--py/mpconfig.h2
-rw-r--r--py/parsenum.c2
3 files changed, 7 insertions, 6 deletions
diff --git a/py/modmath.c b/py/modmath.c
index 0c70f34cd1..7c51eab03a 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -32,9 +32,8 @@
#include <math.h>
// M_PI is not part of the math.h standard and may not be defined
-#ifndef M_PI
-#define M_PI (3.14159265358979323846)
-#endif
+// And by defining our own we can ensure it uses the correct const format.
+#define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)
/// \module math - mathematical functions
///
@@ -204,13 +203,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf);
/// \function radians(x)
STATIC mp_obj_t mp_math_radians(mp_obj_t x_obj) {
- return mp_obj_new_float(mp_obj_get_float(x_obj) * M_PI / 180.0);
+ return mp_obj_new_float(mp_obj_get_float(x_obj) * (MP_PI / MICROPY_FLOAT_CONST(180.0)));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians);
/// \function degrees(x)
STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) {
- return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI);
+ return mp_obj_new_float(mp_obj_get_float(x_obj) * (MICROPY_FLOAT_CONST(180.0) / MP_PI));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index fd82b079cc..3945a1a5ab 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -505,10 +505,12 @@ typedef long long mp_longint_impl_t;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#define MICROPY_PY_BUILTINS_FLOAT (1)
+#define MICROPY_FLOAT_CONST(x) x##F
#define MICROPY_FLOAT_C_FUN(fun) fun##f
typedef float mp_float_t;
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
#define MICROPY_PY_BUILTINS_FLOAT (1)
+#define MICROPY_FLOAT_CONST(x) x
#define MICROPY_FLOAT_C_FUN(fun) fun
typedef double mp_float_t;
#else
diff --git a/py/parsenum.c b/py/parsenum.c
index 1010ad3055..b1c449c9b8 100644
--- a/py/parsenum.c
+++ b/py/parsenum.c
@@ -227,7 +227,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
} else {
if (in == PARSE_DEC_IN_FRAC) {
dec_val += dig * frac_mult;
- frac_mult *= 0.1;
+ frac_mult *= MICROPY_FLOAT_CONST(0.1);
} else {
dec_val = 10 * dec_val + dig;
}