string_format_modulo2_intbig.py 802 B

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