diff options
author | Damien George <damien.p.george@gmail.com> | 2017-10-04 23:15:55 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-10-10 16:01:13 +1100 |
commit | 08a196697c5dfb8cbe3b3e85c1c5f94e3a27804c (patch) | |
tree | 502d4d42aefb770f31eecb0dca8b55bdab7beaee | |
parent | 81a06d2c9c3ce081043e1eb948b65014f1b1786a (diff) | |
download | micropython-08a196697c5dfb8cbe3b3e85c1c5f94e3a27804c.tar.gz micropython-08a196697c5dfb8cbe3b3e85c1c5f94e3a27804c.zip |
py/formatfloat: Don't print the negative sign of a NaN value.
NaN may have the sign bit set but it has no meaning, so don't print it out.
-rw-r--r-- | py/formatfloat.c | 4 | ||||
-rw-r--r-- | tests/float/complex1.py | 1 | ||||
-rw-r--r-- | tests/float/float1.py | 1 |
3 files changed, 4 insertions, 2 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c index 35cd5d51af..b61a958a25 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -30,6 +30,7 @@ #include <assert.h> #include <stdlib.h> #include <stdint.h> +#include <math.h> #include "py/formatfloat.h" /*********************************************************************** @@ -82,7 +83,6 @@ static inline int fp_isless1(float x) { union floatbits fb = {x}; return fb.u < #define FPROUND_TO_ONE 0.999999999995 #define FPDECEXP 256 #define FPMIN_BUF_SIZE 7 // +9e+199 -#include <math.h> #define fp_signbit(x) signbit(x) #define fp_isspecial(x) 1 #define fp_isnan(x) isnan(x) @@ -122,7 +122,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } return buf_size >= 2; } - if (fp_signbit(f)) { + if (fp_signbit(f) && !isnan(f)) { *s++ = '-'; f = -f; } else { diff --git a/tests/float/complex1.py b/tests/float/complex1.py index 8544105453..479b4b3485 100644 --- a/tests/float/complex1.py +++ b/tests/float/complex1.py @@ -59,6 +59,7 @@ ans = (-1.2) ** -3.4; print("%.5g %.5g" % (ans.real, ans.imag)) # check printing of inf/nan print(float('nan') * 1j) +print(float('-nan') * 1j) print(float('inf') * (1 + 1j)) print(float('-inf') * (1 + 1j)) diff --git a/tests/float/float1.py b/tests/float/float1.py index 137dacc233..c64f965a7d 100644 --- a/tests/float/float1.py +++ b/tests/float/float1.py @@ -21,6 +21,7 @@ print(float("INF")) print(float("infinity")) print(float("INFINITY")) print(float("nan")) +print(float("-nan")) print(float("NaN")) try: float("") |