1# test formatting floats with large precision, that it doesn't overflow the buffer 2 3 4def test(num, num_str): 5 if num == float("inf") or num == 0.0 and num_str != "0.0": 6 # skip numbers that overflow or underflow the FP precision 7 return 8 for kind in ("e", "f", "g"): 9 # check precision either side of the size of the buffer (32 bytes) 10 for prec in range(23, 36, 2): 11 fmt = "%." + "%d" % prec + kind 12 s = fmt % num 13 check = abs(float(s) - num) 14 if num > 1: 15 check /= num 16 if check > 1e-6: 17 print("FAIL", num_str, fmt, s, len(s), check) 18 19 20# check pure zero 21test(0.0, "0.0") 22 23# check some powers of 10, making sure to include exponents with 3 digits 24for e in range(-8, 8): 25 num = pow(10, e) 26 test(num, "1e%d" % e) 27