From 956d76578634a86ea50246de45add748b96cb4ec Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Apr 2015 16:51:29 +0100 Subject: [PATCH] py: Fix printing of "inf" and "nan" floating point values. --- py/objfloat.c | 8 ++++---- tests/float/float1.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/py/objfloat.c b/py/objfloat.c index eb59cc5b78..f74e12f9d4 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -49,16 +49,16 @@ STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t char buf[16]; mp_format_float(o->value, buf, sizeof(buf), 'g', 7, '\0'); mp_print_str(print, buf); - if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) { - // Python floats always have decimal point + if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) { + // Python floats always have decimal point (unless inf or nan) mp_print_str(print, ".0"); } #else char buf[32]; sprintf(buf, "%.16g", (double) o->value); mp_print_str(print, buf); - if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) { - // Python floats always have decimal point + if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) { + // Python floats always have decimal point (unless inf or nan) mp_print_str(print, ".0"); } #endif diff --git a/tests/float/float1.py b/tests/float/float1.py index bb670586ae..f670a63486 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -7,6 +7,25 @@ print(1.2) # float construction print(float(1.2)) +print(float("1.2")) +print(float("+1")) +print(float("1e1")) +print(float("1e+1")) +print(float("1e-1")) +print(float("inf")) +print(float("INF")) +print(float("infinity")) +print(float("INFINITY")) +print(float("nan")) +print(float("NaN")) +try: + float("1e+") +except ValueError: + print("ValueError") +try: + float("1z") +except ValueError: + print("ValueError") # unary operators print(bool(0.0))