Skip to content

Commit 956d765

Browse files
committed
py: Fix printing of "inf" and "nan" floating point values.
1 parent c52f125 commit 956d765

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

py/objfloat.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
4949
char buf[16];
5050
mp_format_float(o->value, buf, sizeof(buf), 'g', 7, '\0');
5151
mp_print_str(print, buf);
52-
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) {
53-
// Python floats always have decimal point
52+
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
53+
// Python floats always have decimal point (unless inf or nan)
5454
mp_print_str(print, ".0");
5555
}
5656
#else
5757
char buf[32];
5858
sprintf(buf, "%.16g", (double) o->value);
5959
mp_print_str(print, buf);
60-
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) {
61-
// Python floats always have decimal point
60+
if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
61+
// Python floats always have decimal point (unless inf or nan)
6262
mp_print_str(print, ".0");
6363
}
6464
#endif

tests/float/float1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
# float construction
99
print(float(1.2))
10+
print(float("1.2"))
11+
print(float("+1"))
12+
print(float("1e1"))
13+
print(float("1e+1"))
14+
print(float("1e-1"))
15+
print(float("inf"))
16+
print(float("INF"))
17+
print(float("infinity"))
18+
print(float("INFINITY"))
19+
print(float("nan"))
20+
print(float("NaN"))
21+
try:
22+
float("1e+")
23+
except ValueError:
24+
print("ValueError")
25+
try:
26+
float("1z")
27+
except ValueError:
28+
print("ValueError")
1029

1130
# unary operators
1231
print(bool(0.0))

0 commit comments

Comments
 (0)