aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--py/objstr.c10
-rw-r--r--stmhal/math.c15
-rw-r--r--stmhal/servo.c6
-rw-r--r--tests/basics/math-fun.py4
4 files changed, 24 insertions, 11 deletions
diff --git a/py/objstr.c b/py/objstr.c
index f22c6b1ba..a3f2d6075 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -788,9 +788,11 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg)));
}
+ }
-#if MICROPY_ENABLE_FLOAT
- } else if (arg_looks_numeric(arg)) {
+ // NOTE: no else here. We need the e, f, g etc formats for integer
+ // arguments (from above if) to take this if.
+ if (arg_looks_numeric(arg)) {
if (!type) {
// Even though the docs say that an unspecified type is the same
@@ -828,6 +830,7 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
flags |= PF_FLAG_PAD_NAN_INF; // '{:06e}'.format(float('-inf')) should give '-00inf'
switch (type) {
+#if MICROPY_ENABLE_FLOAT
case 'e':
case 'E':
case 'f':
@@ -841,14 +844,13 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
flags |= PF_FLAG_ADD_PERCENT;
pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg) * 100.0F, 'f', flags, fill, width, precision);
break;
+#endif
default:
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Unknown format code '%c' for object of type 'float'",
type, mp_obj_get_type_str(arg)));
}
-#endif
-
} else {
// arg doesn't look like a number
diff --git a/stmhal/math.c b/stmhal/math.c
index 8afdc82a6..534389df5 100644
--- a/stmhal/math.c
+++ b/stmhal/math.c
@@ -1,4 +1,6 @@
#include <stdint.h>
+#include <math.h>
+
typedef float float_t;
typedef union {
float f;
@@ -86,7 +88,6 @@ float erfcf(float x) { return 0.0; }
float modff(float x, float *y) { return 0.0; }
float frexpf(float x, int *exp) { return 0.0; }
float ldexpf(float x, int exp) { return 0.0; }
-int __fpclassifyf(float x) { return 0; }
/*****************************************************************************/
// from musl-0.9.15 libm.h
@@ -136,6 +137,18 @@ do { \
} while (0)
/*****************************************************************************/
+// __fpclassifyf from musl-0.9.15
+
+int __fpclassifyf(float x)
+{
+ union {float f; uint32_t i;} u = {x};
+ int e = u.i>>23 & 0xff;
+ if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
+ if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
+ return FP_NORMAL;
+}
+
+/*****************************************************************************/
// scalbnf from musl-0.9.15
float scalbnf(float x, int n)
diff --git a/stmhal/servo.c b/stmhal/servo.c
index 9c757c565..893fb11bd 100644
--- a/stmhal/servo.c
+++ b/stmhal/servo.c
@@ -27,8 +27,6 @@ typedef struct _pyb_servo_obj_t {
uint16_t pulse_dest;
} pyb_servo_obj_t;
-STATIC const mp_obj_type_t servo_obj_type;
-
STATIC pyb_servo_obj_t pyb_servo_obj[PYB_SERVO_NUM];
void servo_init(void) {
@@ -36,7 +34,7 @@ void servo_init(void) {
// reset servo objects
for (int i = 0; i < PYB_SERVO_NUM; i++) {
- pyb_servo_obj[i].base.type = &servo_obj_type;
+ pyb_servo_obj[i].base.type = &pyb_servo_type;
pyb_servo_obj[i].servo_id = i + 1;
pyb_servo_obj[i].time_left = 0;
pyb_servo_obj[i].pulse_cur = 150; // units of 10us
@@ -149,7 +147,7 @@ STATIC mp_obj_t pyb_servo_make_new(mp_obj_t type_in, uint n_args, uint n_kw, con
// check servo number
if (!(0 <= servo_id && servo_id < PYB_SERVO_NUM)) {
- nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id));
+ nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Servo %d does not exist", servo_id + 1));
}
// get and init servo object
diff --git a/tests/basics/math-fun.py b/tests/basics/math-fun.py
index eb80ab0f5..7a37c5845 100644
--- a/tests/basics/math-fun.py
+++ b/tests/basics/math-fun.py
@@ -34,7 +34,7 @@ functions = [('sqrt', sqrt, p_test_values),
for function_name, function, test_vals in functions:
print(function_name)
for value in test_vals:
- print("{:8.7g}".format(function(value)))
+ print("{:.7g}".format(function(value)))
binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.),
(-23., -42.), (1., 0.0), (1., -0.0)])
@@ -43,4 +43,4 @@ binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.)
for function_name, function, test_vals in binary_functions:
print(function_name)
for value1, value2 in test_vals:
- print("{:8.7g}".format(function(value1, value2)))
+ print("{:.7g}".format(function(value1, value2)))