test_strtod.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. # Tests for the correctly-rounded string -> float conversions
  2. # introduced in Python 2.7 and 3.1.
  3. import random
  4. import unittest
  5. import re
  6. import sys
  7. import test.support
  8. if getattr(sys, 'float_repr_style', '') != 'short':
  9. raise unittest.SkipTest('correctly-rounded string->float conversions '
  10. 'not available on this system')
  11. # Correctly rounded str -> float in pure Python, for comparison.
  12. strtod_parser = re.compile(r""" # A numeric string consists of:
  13. (?P<sign>[-+])? # an optional sign, followed by
  14. (?=\d|\.\d) # a number with at least one digit
  15. (?P<int>\d*) # having a (possibly empty) integer part
  16. (?:\.(?P<frac>\d*))? # followed by an optional fractional part
  17. (?:E(?P<exp>[-+]?\d+))? # and an optional exponent
  18. \Z
  19. """, re.VERBOSE | re.IGNORECASE).match
  20. # Pure Python version of correctly rounded string->float conversion.
  21. # Avoids any use of floating-point by returning the result as a hex string.
  22. def strtod(s, mant_dig=53, min_exp = -1021, max_exp = 1024):
  23. """Convert a finite decimal string to a hex string representing an
  24. IEEE 754 binary64 float. Return 'inf' or '-inf' on overflow.
  25. This function makes no use of floating-point arithmetic at any
  26. stage."""
  27. # parse string into a pair of integers 'a' and 'b' such that
  28. # abs(decimal value) = a/b, along with a boolean 'negative'.
  29. m = strtod_parser(s)
  30. if m is None:
  31. raise ValueError('invalid numeric string')
  32. fraction = m.group('frac') or ''
  33. intpart = int(m.group('int') + fraction)
  34. exp = int(m.group('exp') or '0') - len(fraction)
  35. negative = m.group('sign') == '-'
  36. a, b = intpart*10**max(exp, 0), 10**max(0, -exp)
  37. # quick return for zeros
  38. if not a:
  39. return '-0x0.0p+0' if negative else '0x0.0p+0'
  40. # compute exponent e for result; may be one too small in the case
  41. # that the rounded value of a/b lies in a different binade from a/b
  42. d = a.bit_length() - b.bit_length()
  43. d += (a >> d if d >= 0 else a << -d) >= b
  44. e = max(d, min_exp) - mant_dig
  45. # approximate a/b by number of the form q * 2**e; adjust e if necessary
  46. a, b = a << max(-e, 0), b << max(e, 0)
  47. q, r = divmod(a, b)
  48. if 2*r > b or 2*r == b and q & 1:
  49. q += 1
  50. if q.bit_length() == mant_dig+1:
  51. q //= 2
  52. e += 1
  53. # double check that (q, e) has the right form
  54. assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig
  55. assert q.bit_length() == mant_dig or e == min_exp - mant_dig
  56. # check for overflow and underflow
  57. if e + q.bit_length() > max_exp:
  58. return '-inf' if negative else 'inf'
  59. if not q:
  60. return '-0x0.0p+0' if negative else '0x0.0p+0'
  61. # for hex representation, shift so # bits after point is a multiple of 4
  62. hexdigs = 1 + (mant_dig-2)//4
  63. shift = 3 - (mant_dig-2)%4
  64. q, e = q << shift, e - shift
  65. return '{}0x{:x}.{:0{}x}p{:+d}'.format(
  66. '-' if negative else '',
  67. q // 16**hexdigs,
  68. q % 16**hexdigs,
  69. hexdigs,
  70. e + 4*hexdigs)
  71. TEST_SIZE = 10
  72. class StrtodTests(unittest.TestCase):
  73. def check_strtod(self, s):
  74. """Compare the result of Python's builtin correctly rounded
  75. string->float conversion (using float) to a pure Python
  76. correctly rounded string->float implementation. Fail if the
  77. two methods give different results."""
  78. try:
  79. fs = float(s)
  80. except OverflowError:
  81. got = '-inf' if s[0] == '-' else 'inf'
  82. except MemoryError:
  83. got = 'memory error'
  84. else:
  85. got = fs.hex()
  86. expected = strtod(s)
  87. self.assertEqual(expected, got,
  88. "Incorrectly rounded str->float conversion for {}: "
  89. "expected {}, got {}".format(s, expected, got))
  90. def test_short_halfway_cases(self):
  91. # exact halfway cases with a small number of significant digits
  92. for k in 0, 5, 10, 15, 20:
  93. # upper = smallest integer >= 2**54/5**k
  94. upper = -(-2**54//5**k)
  95. # lower = smallest odd number >= 2**53/5**k
  96. lower = -(-2**53//5**k)
  97. if lower % 2 == 0:
  98. lower += 1
  99. for i in range(TEST_SIZE):
  100. # Select a random odd n in [2**53/5**k,
  101. # 2**54/5**k). Then n * 10**k gives a halfway case
  102. # with small number of significant digits.
  103. n, e = random.randrange(lower, upper, 2), k
  104. # Remove any additional powers of 5.
  105. while n % 5 == 0:
  106. n, e = n // 5, e + 1
  107. assert n % 10 in (1, 3, 7, 9)
  108. # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0,
  109. # until n * 2**p2 has more than 20 significant digits.
  110. digits, exponent = n, e
  111. while digits < 10**20:
  112. s = '{}e{}'.format(digits, exponent)
  113. self.check_strtod(s)
  114. # Same again, but with extra trailing zeros.
  115. s = '{}e{}'.format(digits * 10**40, exponent - 40)
  116. self.check_strtod(s)
  117. digits *= 2
  118. # Try numbers of the form n * 5**p2 * 10**(e - p5), p5
  119. # >= 0, with n * 5**p5 < 10**20.
  120. digits, exponent = n, e
  121. while digits < 10**20:
  122. s = '{}e{}'.format(digits, exponent)
  123. self.check_strtod(s)
  124. # Same again, but with extra trailing zeros.
  125. s = '{}e{}'.format(digits * 10**40, exponent - 40)
  126. self.check_strtod(s)
  127. digits *= 5
  128. exponent -= 1
  129. def test_halfway_cases(self):
  130. # test halfway cases for the round-half-to-even rule
  131. for i in range(100 * TEST_SIZE):
  132. # bit pattern for a random finite positive (or +0.0) float
  133. bits = random.randrange(2047*2**52)
  134. # convert bit pattern to a number of the form m * 2**e
  135. e, m = divmod(bits, 2**52)
  136. if e:
  137. m, e = m + 2**52, e - 1
  138. e -= 1074
  139. # add 0.5 ulps
  140. m, e = 2*m + 1, e - 1
  141. # convert to a decimal string
  142. if e >= 0:
  143. digits = m << e
  144. exponent = 0
  145. else:
  146. # m * 2**e = (m * 5**-e) * 10**e
  147. digits = m * 5**-e
  148. exponent = e
  149. s = '{}e{}'.format(digits, exponent)
  150. self.check_strtod(s)
  151. def test_boundaries(self):
  152. # boundaries expressed as triples (n, e, u), where
  153. # n*10**e is an approximation to the boundary value and
  154. # u*10**e is 1ulp
  155. boundaries = [
  156. (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0)
  157. (17976931348623159077, 289, 1995), # overflow boundary (2.**1024)
  158. (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022)
  159. (0, -327, 4941), # zero
  160. ]
  161. for n, e, u in boundaries:
  162. for j in range(1000):
  163. digits = n + random.randrange(-3*u, 3*u)
  164. exponent = e
  165. s = '{}e{}'.format(digits, exponent)
  166. self.check_strtod(s)
  167. n *= 10
  168. u *= 10
  169. e -= 1
  170. def test_underflow_boundary(self):
  171. # test values close to 2**-1075, the underflow boundary; similar
  172. # to boundary_tests, except that the random error doesn't scale
  173. # with n
  174. for exponent in range(-400, -320):
  175. base = 10**-exponent // 2**1075
  176. for j in range(TEST_SIZE):
  177. digits = base + random.randrange(-1000, 1000)
  178. s = '{}e{}'.format(digits, exponent)
  179. self.check_strtod(s)
  180. def test_bigcomp(self):
  181. for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50:
  182. dig10 = 10**ndigs
  183. for i in range(10 * TEST_SIZE):
  184. digits = random.randrange(dig10)
  185. exponent = random.randrange(-400, 400)
  186. s = '{}e{}'.format(digits, exponent)
  187. self.check_strtod(s)
  188. def test_parsing(self):
  189. # make '0' more likely to be chosen than other digits
  190. digits = '000000123456789'
  191. signs = ('+', '-', '')
  192. # put together random short valid strings
  193. # \d*[.\d*]?e
  194. for i in range(1000):
  195. for j in range(TEST_SIZE):
  196. s = random.choice(signs)
  197. intpart_len = random.randrange(5)
  198. s += ''.join(random.choice(digits) for _ in range(intpart_len))
  199. if random.choice([True, False]):
  200. s += '.'
  201. fracpart_len = random.randrange(5)
  202. s += ''.join(random.choice(digits)
  203. for _ in range(fracpart_len))
  204. else:
  205. fracpart_len = 0
  206. if random.choice([True, False]):
  207. s += random.choice(['e', 'E'])
  208. s += random.choice(signs)
  209. exponent_len = random.randrange(1, 4)
  210. s += ''.join(random.choice(digits)
  211. for _ in range(exponent_len))
  212. if intpart_len + fracpart_len:
  213. self.check_strtod(s)
  214. else:
  215. try:
  216. float(s)
  217. except ValueError:
  218. pass
  219. else:
  220. assert False, "expected ValueError"
  221. @test.support.bigmemtest(size=test.support._2G+10, memuse=3, dry_run=False)
  222. def test_oversized_digit_strings(self, maxsize):
  223. # Input string whose length doesn't fit in an INT.
  224. s = "1." + "1" * maxsize
  225. with self.assertRaises(ValueError):
  226. float(s)
  227. del s
  228. s = "0." + "0" * maxsize + "1"
  229. with self.assertRaises(ValueError):
  230. float(s)
  231. del s
  232. def test_large_exponents(self):
  233. # Verify that the clipping of the exponent in strtod doesn't affect the
  234. # output values.
  235. def positive_exp(n):
  236. """ Long string with value 1.0 and exponent n"""
  237. return '0.{}1e+{}'.format('0'*(n-1), n)
  238. def negative_exp(n):
  239. """ Long string with value 1.0 and exponent -n"""
  240. return '1{}e-{}'.format('0'*n, n)
  241. self.assertEqual(float(positive_exp(10000)), 1.0)
  242. self.assertEqual(float(positive_exp(20000)), 1.0)
  243. self.assertEqual(float(positive_exp(30000)), 1.0)
  244. self.assertEqual(float(negative_exp(10000)), 1.0)
  245. self.assertEqual(float(negative_exp(20000)), 1.0)
  246. self.assertEqual(float(negative_exp(30000)), 1.0)
  247. def test_particular(self):
  248. # inputs that produced crashes or incorrectly rounded results with
  249. # previous versions of dtoa.c, for various reasons
  250. test_strings = [
  251. # issue 7632 bug 1, originally reported failing case
  252. '2183167012312112312312.23538020374420446192e-370',
  253. # 5 instances of issue 7632 bug 2
  254. '12579816049008305546974391768996369464963024663104e-357',
  255. '17489628565202117263145367596028389348922981857013e-357',
  256. '18487398785991994634182916638542680759613590482273e-357',
  257. '32002864200581033134358724675198044527469366773928e-358',
  258. '94393431193180696942841837085033647913224148539854e-358',
  259. '73608278998966969345824653500136787876436005957953e-358',
  260. '64774478836417299491718435234611299336288082136054e-358',
  261. '13704940134126574534878641876947980878824688451169e-357',
  262. '46697445774047060960624497964425416610480524760471e-358',
  263. # failing case for bug introduced by METD in r77451 (attempted
  264. # fix for issue 7632, bug 2), and fixed in r77482.
  265. '28639097178261763178489759107321392745108491825303e-311',
  266. # two numbers demonstrating a flaw in the bigcomp 'dig == 0'
  267. # correction block (issue 7632, bug 3)
  268. '1.00000000000000001e44',
  269. '1.0000000000000000100000000000000000000001e44',
  270. # dtoa.c bug for numbers just smaller than a power of 2 (issue
  271. # 7632, bug 4)
  272. '99999999999999994487665465554760717039532578546e-47',
  273. # failing case for off-by-one error introduced by METD in
  274. # r77483 (dtoa.c cleanup), fixed in r77490
  275. '965437176333654931799035513671997118345570045914469' #...
  276. '6213413350821416312194420007991306908470147322020121018368e0',
  277. # incorrect lsb detection for round-half-to-even when
  278. # bc->scale != 0 (issue 7632, bug 6).
  279. '104308485241983990666713401708072175773165034278685' #...
  280. '682646111762292409330928739751702404658197872319129' #...
  281. '036519947435319418387839758990478549477777586673075' #...
  282. '945844895981012024387992135617064532141489278815239' #...
  283. '849108105951619997829153633535314849999674266169258' #...
  284. '928940692239684771590065027025835804863585454872499' #...
  285. '320500023126142553932654370362024104462255244034053' #...
  286. '203998964360882487378334860197725139151265590832887' #...
  287. '433736189468858614521708567646743455601905935595381' #...
  288. '852723723645799866672558576993978025033590728687206' #...
  289. '296379801363024094048327273913079612469982585674824' #...
  290. '156000783167963081616214710691759864332339239688734' #...
  291. '656548790656486646106983450809073750535624894296242' #...
  292. '072010195710276073042036425579852459556183541199012' #...
  293. '652571123898996574563824424330960027873516082763671875e-1075',
  294. # demonstration that original fix for issue 7632 bug 1 was
  295. # buggy; the exit condition was too strong
  296. '247032822920623295e-341',
  297. # demonstrate similar problem to issue 7632 bug1: crash
  298. # with 'oversized quotient in quorem' message.
  299. '99037485700245683102805043437346965248029601286431e-373',
  300. '99617639833743863161109961162881027406769510558457e-373',
  301. '98852915025769345295749278351563179840130565591462e-372',
  302. '99059944827693569659153042769690930905148015876788e-373',
  303. '98914979205069368270421829889078356254059760327101e-372',
  304. # issue 7632 bug 5: the following 2 strings convert differently
  305. '1000000000000000000000000000000000000000e-16',
  306. '10000000000000000000000000000000000000000e-17',
  307. # issue 7632 bug 7
  308. '991633793189150720000000000000000000000000000000000000000e-33',
  309. # And another, similar, failing halfway case
  310. '4106250198039490000000000000000000000000000000000000000e-38',
  311. # issue 7632 bug 8: the following produced 10.0
  312. '10.900000000000000012345678912345678912345',
  313. # two humongous values from issue 7743
  314. '116512874940594195638617907092569881519034793229385' #...
  315. '228569165191541890846564669771714896916084883987920' #...
  316. '473321268100296857636200926065340769682863349205363' #...
  317. '349247637660671783209907949273683040397979984107806' #...
  318. '461822693332712828397617946036239581632976585100633' #...
  319. '520260770761060725403904123144384571612073732754774' #...
  320. '588211944406465572591022081973828448927338602556287' #...
  321. '851831745419397433012491884869454462440536895047499' #...
  322. '436551974649731917170099387762871020403582994193439' #...
  323. '761933412166821484015883631622539314203799034497982' #...
  324. '130038741741727907429575673302461380386596501187482' #...
  325. '006257527709842179336488381672818798450229339123527' #...
  326. '858844448336815912020452294624916993546388956561522' #...
  327. '161875352572590420823607478788399460162228308693742' #...
  328. '05287663441403533948204085390898399055004119873046875e-1075',
  329. '525440653352955266109661060358202819561258984964913' #...
  330. '892256527849758956045218257059713765874251436193619' #...
  331. '443248205998870001633865657517447355992225852945912' #...
  332. '016668660000210283807209850662224417504752264995360' #...
  333. '631512007753855801075373057632157738752800840302596' #...
  334. '237050247910530538250008682272783660778181628040733' #...
  335. '653121492436408812668023478001208529190359254322340' #...
  336. '397575185248844788515410722958784640926528544043090' #...
  337. '115352513640884988017342469275006999104519620946430' #...
  338. '818767147966495485406577703972687838176778993472989' #...
  339. '561959000047036638938396333146685137903018376496408' #...
  340. '319705333868476925297317136513970189073693314710318' #...
  341. '991252811050501448326875232850600451776091303043715' #...
  342. '157191292827614046876950225714743118291034780466325' #...
  343. '085141343734564915193426994587206432697337118211527' #...
  344. '278968731294639353354774788602467795167875117481660' #...
  345. '4738791256853675690543663283782215866825e-1180',
  346. # exercise exit conditions in bigcomp comparison loop
  347. '2602129298404963083833853479113577253105939995688e2',
  348. '260212929840496308383385347911357725310593999568896e0',
  349. '26021292984049630838338534791135772531059399956889601e-2',
  350. '260212929840496308383385347911357725310593999568895e0',
  351. '260212929840496308383385347911357725310593999568897e0',
  352. '260212929840496308383385347911357725310593999568996e0',
  353. '260212929840496308383385347911357725310593999568866e0',
  354. # 2**53
  355. '9007199254740992.00',
  356. # 2**1024 - 2**970: exact overflow boundary. All values
  357. # smaller than this should round to something finite; any value
  358. # greater than or equal to this one overflows.
  359. '179769313486231580793728971405303415079934132710037' #...
  360. '826936173778980444968292764750946649017977587207096' #...
  361. '330286416692887910946555547851940402630657488671505' #...
  362. '820681908902000708383676273854845817711531764475730' #...
  363. '270069855571366959622842914819860834936475292719074' #...
  364. '168444365510704342711559699508093042880177904174497792',
  365. # 2**1024 - 2**970 - tiny
  366. '179769313486231580793728971405303415079934132710037' #...
  367. '826936173778980444968292764750946649017977587207096' #...
  368. '330286416692887910946555547851940402630657488671505' #...
  369. '820681908902000708383676273854845817711531764475730' #...
  370. '270069855571366959622842914819860834936475292719074' #...
  371. '168444365510704342711559699508093042880177904174497791.999',
  372. # 2**1024 - 2**970 + tiny
  373. '179769313486231580793728971405303415079934132710037' #...
  374. '826936173778980444968292764750946649017977587207096' #...
  375. '330286416692887910946555547851940402630657488671505' #...
  376. '820681908902000708383676273854845817711531764475730' #...
  377. '270069855571366959622842914819860834936475292719074' #...
  378. '168444365510704342711559699508093042880177904174497792.001',
  379. # 1 - 2**-54, +-tiny
  380. '999999999999999944488848768742172978818416595458984375e-54',
  381. '9999999999999999444888487687421729788184165954589843749999999e-54',
  382. '9999999999999999444888487687421729788184165954589843750000001e-54',
  383. # Value found by Rick Regan that gives a result of 2**-968
  384. # under Gay's dtoa.c (as of Nov 04, 2010); since fixed.
  385. # (Fixed some time ago in Python's dtoa.c.)
  386. '0.0000000000000000000000000000000000000000100000000' #...
  387. '000000000576129113423785429971690421191214034235435' #...
  388. '087147763178149762956868991692289869941246658073194' #...
  389. '51982237978882039897143840789794921875',
  390. ]
  391. for s in test_strings:
  392. self.check_strtod(s)
  393. if __name__ == "__main__":
  394. unittest.main()