2016-03-29 21:07:15 +00:00
|
|
|
# test formatting floats with large precision, that it doesn't overflow the buffer
|
|
|
|
|
2020-03-23 02:26:08 +00:00
|
|
|
|
2016-03-29 21:07:15 +00:00
|
|
|
def test(num, num_str):
|
2020-03-23 02:26:08 +00:00
|
|
|
if num == float("inf") or num == 0.0 and num_str != "0.0":
|
2016-03-29 21:07:15 +00:00
|
|
|
# skip numbers that overflow or underflow the FP precision
|
|
|
|
return
|
2020-03-23 02:26:08 +00:00
|
|
|
for kind in ("e", "f", "g"):
|
2016-03-29 21:07:15 +00:00
|
|
|
# check precision either side of the size of the buffer (32 bytes)
|
|
|
|
for prec in range(23, 36, 2):
|
2020-03-23 02:26:08 +00:00
|
|
|
fmt = "%." + "%d" % prec + kind
|
2016-03-29 21:07:15 +00:00
|
|
|
s = fmt % num
|
|
|
|
check = abs(float(s) - num)
|
|
|
|
if num > 1:
|
|
|
|
check /= num
|
|
|
|
if check > 1e-6:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("FAIL", num_str, fmt, s, len(s), check)
|
|
|
|
|
2016-03-29 21:07:15 +00:00
|
|
|
|
|
|
|
# check pure zero
|
2020-03-23 02:26:08 +00:00
|
|
|
test(0.0, "0.0")
|
2016-03-29 21:07:15 +00:00
|
|
|
|
2017-03-06 23:13:36 +00:00
|
|
|
# check some powers of 10, making sure to include exponents with 3 digits
|
|
|
|
for e in range(-8, 8):
|
2016-03-29 21:07:15 +00:00
|
|
|
num = pow(10, e)
|
2020-03-23 02:26:08 +00:00
|
|
|
test(num, "1e%d" % e)
|