float2int_doubleprec_intbig.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # check cases converting float to int, requiring double precision float
  2. try:
  3. import ustruct as struct
  4. except:
  5. import struct
  6. import sys
  7. maxsize_bits = 0
  8. maxsize = sys.maxsize
  9. while maxsize:
  10. maxsize >>= 1
  11. maxsize_bits += 1
  12. # work out configuration values
  13. is_64bit = maxsize_bits > 32
  14. # 0 = none, 1 = long long, 2 = mpz
  15. ll_type = None
  16. if is_64bit:
  17. if maxsize_bits < 63:
  18. ll_type = 0
  19. else:
  20. if maxsize_bits < 31:
  21. ll_type = 0
  22. if ll_type is None:
  23. one = 1
  24. if one << 65 < one << 62:
  25. ll_type = 1
  26. else:
  27. ll_type = 2
  28. # This case occurs with time.time() values
  29. if ll_type != 0:
  30. print(int(1418774543.))
  31. print("%d" % 1418774543.)
  32. if ll_type == 3:
  33. print(int(2.**100))
  34. print("%d" % 2.**100)
  35. else:
  36. print(int(1073741823.))
  37. print("%d" % 1073741823.)
  38. testpass = True
  39. p2_rng = ((30,63,1024),(62,63,1024))[is_64bit][ll_type]
  40. for i in range(0,p2_rng):
  41. bitcnt = len(bin(int(2.**i))) - 3;
  42. if i != bitcnt:
  43. print('fail: 2**%u was %u bits long' % (i, bitcnt));
  44. testpass = False
  45. print("power of 2 test: %s" % (testpass and 'passed' or 'failed'))
  46. testpass = True
  47. p10_rng = ((9,18,23),(18,18,23))[is_64bit][ll_type]
  48. for i in range(0,p10_rng):
  49. digcnt = len(str(int(10.**i))) - 1;
  50. if i != digcnt:
  51. print('fail: 10**%u was %u digits long' % (i, digcnt));
  52. testpass = False
  53. print("power of 10 test: %s" % (testpass and 'passed' or 'failed'))
  54. def fp2int_test(num, name, should_fail):
  55. try:
  56. x = int(num)
  57. passed = ~should_fail
  58. except:
  59. passed = should_fail
  60. print('%s: %s' % (name, passed and 'passed' or 'failed'))
  61. if ll_type != 2:
  62. if ll_type == 0:
  63. if is_64bit:
  64. neg_bad_fp = -1.00000005*2.**62.
  65. pos_bad_fp = 2.**62.
  66. neg_good_fp = -2.**62.
  67. pos_good_fp = 0.99999993*2.**62.
  68. else:
  69. neg_bad_fp = -1.00000005*2.**30.
  70. pos_bad_fp = 2.**30.
  71. neg_good_fp = -2.**30.
  72. pos_good_fp = 0.9999999499*2.**30.
  73. else:
  74. neg_bad_fp = -0.51*2.**64.
  75. pos_bad_fp = 2.**63.
  76. neg_good_fp = -2.**63.
  77. pos_good_fp = 1.9999998*2.**62.
  78. fp2int_test(neg_bad_fp, 'neg bad', True)
  79. fp2int_test(pos_bad_fp, 'pos bad', True)
  80. fp2int_test(neg_good_fp, 'neg good', False)
  81. fp2int_test(pos_good_fp, 'pos good', False)
  82. else:
  83. fp2int_test(-1.9999999999999981*2.**1023., 'large neg', False)
  84. fp2int_test(1.9999999999999981*2.**1023., 'large pos', False)
  85. fp2int_test(float('inf'), 'inf test', True)
  86. fp2int_test(float('nan'), 'NaN test', True)
  87. # test numbers < 1 (this used to fail; see issue #1044)
  88. fp2int_test(0.0001, 'small num', False)
  89. struct.pack('I', int(1/2))