How to keep Signaling NaN unchanged when convert hex to single-precision float ?

April 26, 2021
10:20 p.m.
Hi, Code snippet 1 and 2 convert an hex to float, but the SNaN is changed to QNaN automatically. It follows IEEE 754 behavior. While Code snippet 3 converts an hex to double, and the SNaN keeps. Is there any solution to keep SNaN float unchanged in Code snippet 1 and 2? 1. >>> i = int('7f800001', 16)
1. >>> f = struct.unpack('!f', bytes.fromhex('7f800001'))[0]
f # nan
hex(struct.unpack('<I', struct.pack('<f', f))[0])
'0x7fc00001' 1. >>> i = int('7FF0000000000001', 16)
cp = pointer(c_uint64(i))
fp = cast(cp, POINTER(c_double))
print(fp.contents.value)
nan
print(struct.pack(">d", fp.contents.value).hex())
7ff0000000000001 Thank you in advance!
1397
Age (days ago)
1397
Last active (days ago)
0 comments
1 participants
participants (1)
-
Huang, Yang