Skip to content

Commit 05a1528

Browse files
committed
cleanup & comments
1 parent 092d729 commit 05a1528

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Lib/test/test_capi/test_float.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def test_pack_unpack_roundtrip_for_nans(self):
203203
data1 = data if endian == BIG_ENDIAN else data[::-1]
204204
value = unpack(data1, endian)
205205
if signaling and sys.platform == 'win32':
206+
# On this platform sNaN becomes qNaN when returned
207+
# from function. That's a known bug, e.g.
208+
# https://developercommunity.visualstudio.com/t/155064
206209
value = _testcapi.float_set_snan(value)
207210
data2 = pack(size, value, endian)
208211
self.assertTrue(math.isnan(value))

Objects/floatobject.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ PyFloat_Pack2(double x, char *data, int le)
20282028

20292029
memcpy(&v, &x, sizeof(v));
20302030
v &= 0xffc0000000000ULL;
2031-
bits = (unsigned short)(v >> 42); /* NaN's payload */
2031+
bits = (unsigned short)(v >> 42); /* NaN's type & payload */
20322032
}
20332033
else {
20342034
sign = (x < 0.0);
@@ -2192,15 +2192,15 @@ PyFloat_Pack4(double x, char *data, int le)
21922192
if (isinf(y) && !isinf(x))
21932193
goto Overflow;
21942194

2195-
/* correct y if x was a sNaN, transformed to qNaN by assignment */
2195+
/* correct y if x was a sNaN, transformed to qNaN by conversion */
21962196
if (isnan(x)) {
21972197
uint64_t v;
21982198

21992199
memcpy(&v, &x, 8);
22002200
if ((v & (1ULL << 51)) == 0) {
22012201
uint32_t *py = (uint32_t *)&y;
22022202

2203-
*py -= 1 << 22; /* make sNaN */
2203+
*py &= ~(1 << 22); /* make sNaN */
22042204
}
22052205
}
22062206

@@ -2388,7 +2388,7 @@ PyFloat_Unpack2(const char *data, int le)
23882388
/* NaN */
23892389
uint64_t v = sign ? 0xfff0000000000000ULL : 0x7ff0000000000000ULL;
23902390

2391-
v += (uint64_t)f << 42; /* add NaN's payload */
2391+
v += (uint64_t)f << 42; /* add NaN's type & payload */
23922392
memcpy(&x, &v, sizeof(v));
23932393
return x;
23942394
}
@@ -2491,11 +2491,11 @@ PyFloat_Unpack4(const char *data, int le)
24912491
uint32_t v;
24922492

24932493
memcpy(&v, &x, 4);
2494-
if ((v & (1<<22)) == 0) {
2494+
if ((v & (1 << 22)) == 0) {
24952495
double y = x; /* will make qNaN double */
24962496
uint64_t *py = (uint64_t *)&y;
24972497

2498-
*py -= (1ULL<<51); /* make sNaN */
2498+
*py &= ~(1ULL << 51); /* make sNaN */
24992499
return y;
25002500
}
25012501
}

0 commit comments

Comments
 (0)