circuitpython/tests/float/float_array.py
Yoctopuce dev e57aa7e70a py/obj: Fix nan handling in object REPR_C and REPR_D.
CPython math.nan is positive with regards to copysign.  The signaling bit
(aka sign flag) was incorrectly set.

In addition, REPR_C and REPR_D should only use the _true_ nan to prevent
system crash in case of hand-crafted floats.  For instance, with REPR_C,
any nan-like float following the pattern
`01111111 1xxxxxxx xxxxxxxx xxxxx1xx` would be switched to an immediate
object or a qstr string.  When the qstr index is too large, this would
cause a crash.

This commit fixes the issue, and adds the relevant test cases.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-06-24 00:30:08 +10:00

28 lines
578 B
Python

try:
from array import array
except ImportError:
print("SKIP")
raise SystemExit
def test(a):
print(a)
a.append(1.2)
print(len(a), "%.3f" % a[0])
a.append(1)
a.append(False)
print(len(a), "%.3f %.3f" % (a[1], a[2]))
a[-1] = 3.45
print("%.3f" % a[-1])
test(array("f"))
test(array("d"))
# hand-crafted floats, including non-standard nan
for float_hex in (0x3DCCCCCC, 0x7F800024, 0x7FC00004):
f = array("f", bytes(array("I", [float_hex])))[0]
if type(f) is float:
print("{:.4e}".format(f))
else:
print(f)