Skip to content

Commit 08a1966

Browse files
committed
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.
1 parent 81a06d2 commit 08a1966

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

py/formatfloat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <assert.h>
3131
#include <stdlib.h>
3232
#include <stdint.h>
33+
#include <math.h>
3334
#include "py/formatfloat.h"
3435

3536
/***********************************************************************
@@ -82,7 +83,6 @@ static inline int fp_isless1(float x) { union floatbits fb = {x}; return fb.u <
8283
#define FPROUND_TO_ONE 0.999999999995
8384
#define FPDECEXP 256
8485
#define FPMIN_BUF_SIZE 7 // +9e+199
85-
#include <math.h>
8686
#define fp_signbit(x) signbit(x)
8787
#define fp_isspecial(x) 1
8888
#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
122122
}
123123
return buf_size >= 2;
124124
}
125-
if (fp_signbit(f)) {
125+
if (fp_signbit(f) && !isnan(f)) {
126126
*s++ = '-';
127127
f = -f;
128128
} else {

tests/float/complex1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
# check printing of inf/nan
6161
print(float('nan') * 1j)
62+
print(float('-nan') * 1j)
6263
print(float('inf') * (1 + 1j))
6364
print(float('-inf') * (1 + 1j))
6465

tests/float/float1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
print(float("infinity"))
2222
print(float("INFINITY"))
2323
print(float("nan"))
24+
print(float("-nan"))
2425
print(float("NaN"))
2526
try:
2627
float("")

0 commit comments

Comments
 (0)