+++ /dev/null
-# Tests for the correctly-rounded string -> float conversions\r
-# introduced in Python 2.7 and 3.1.\r
-\r
-import random\r
-import struct\r
-import unittest\r
-import re\r
-import sys\r
-from test import test_support\r
-\r
-if getattr(sys, 'float_repr_style', '') != 'short':\r
- raise unittest.SkipTest('correctly-rounded string->float conversions '\r
- 'not available on this system')\r
-\r
-# Correctly rounded str -> float in pure Python, for comparison.\r
-\r
-strtod_parser = re.compile(r""" # A numeric string consists of:\r
- (?P<sign>[-+])? # an optional sign, followed by\r
- (?=\d|\.\d) # a number with at least one digit\r
- (?P<int>\d*) # having a (possibly empty) integer part\r
- (?:\.(?P<frac>\d*))? # followed by an optional fractional part\r
- (?:E(?P<exp>[-+]?\d+))? # and an optional exponent\r
- \Z\r
-""", re.VERBOSE | re.IGNORECASE).match\r
-\r
-# Pure Python version of correctly rounded string->float conversion.\r
-# Avoids any use of floating-point by returning the result as a hex string.\r
-def strtod(s, mant_dig=53, min_exp = -1021, max_exp = 1024):\r
- """Convert a finite decimal string to a hex string representing an\r
- IEEE 754 binary64 float. Return 'inf' or '-inf' on overflow.\r
- This function makes no use of floating-point arithmetic at any\r
- stage."""\r
-\r
- # parse string into a pair of integers 'a' and 'b' such that\r
- # abs(decimal value) = a/b, along with a boolean 'negative'.\r
- m = strtod_parser(s)\r
- if m is None:\r
- raise ValueError('invalid numeric string')\r
- fraction = m.group('frac') or ''\r
- intpart = int(m.group('int') + fraction)\r
- exp = int(m.group('exp') or '0') - len(fraction)\r
- negative = m.group('sign') == '-'\r
- a, b = intpart*10**max(exp, 0), 10**max(0, -exp)\r
-\r
- # quick return for zeros\r
- if not a:\r
- return '-0x0.0p+0' if negative else '0x0.0p+0'\r
-\r
- # compute exponent e for result; may be one too small in the case\r
- # that the rounded value of a/b lies in a different binade from a/b\r
- d = a.bit_length() - b.bit_length()\r
- d += (a >> d if d >= 0 else a << -d) >= b\r
- e = max(d, min_exp) - mant_dig\r
-\r
- # approximate a/b by number of the form q * 2**e; adjust e if necessary\r
- a, b = a << max(-e, 0), b << max(e, 0)\r
- q, r = divmod(a, b)\r
- if 2*r > b or 2*r == b and q & 1:\r
- q += 1\r
- if q.bit_length() == mant_dig+1:\r
- q //= 2\r
- e += 1\r
-\r
- # double check that (q, e) has the right form\r
- assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig\r
- assert q.bit_length() == mant_dig or e == min_exp - mant_dig\r
-\r
- # check for overflow and underflow\r
- if e + q.bit_length() > max_exp:\r
- return '-inf' if negative else 'inf'\r
- if not q:\r
- return '-0x0.0p+0' if negative else '0x0.0p+0'\r
-\r
- # for hex representation, shift so # bits after point is a multiple of 4\r
- hexdigs = 1 + (mant_dig-2)//4\r
- shift = 3 - (mant_dig-2)%4\r
- q, e = q << shift, e - shift\r
- return '{}0x{:x}.{:0{}x}p{:+d}'.format(\r
- '-' if negative else '',\r
- q // 16**hexdigs,\r
- q % 16**hexdigs,\r
- hexdigs,\r
- e + 4*hexdigs)\r
-\r
-TEST_SIZE = 10\r
-\r
-class StrtodTests(unittest.TestCase):\r
- def check_strtod(self, s):\r
- """Compare the result of Python's builtin correctly rounded\r
- string->float conversion (using float) to a pure Python\r
- correctly rounded string->float implementation. Fail if the\r
- two methods give different results."""\r
-\r
- try:\r
- fs = float(s)\r
- except OverflowError:\r
- got = '-inf' if s[0] == '-' else 'inf'\r
- except MemoryError:\r
- got = 'memory error'\r
- else:\r
- got = fs.hex()\r
- expected = strtod(s)\r
- self.assertEqual(expected, got,\r
- "Incorrectly rounded str->float conversion for {}: "\r
- "expected {}, got {}".format(s, expected, got))\r
-\r
- def test_short_halfway_cases(self):\r
- # exact halfway cases with a small number of significant digits\r
- for k in 0, 5, 10, 15, 20:\r
- # upper = smallest integer >= 2**54/5**k\r
- upper = -(-2**54//5**k)\r
- # lower = smallest odd number >= 2**53/5**k\r
- lower = -(-2**53//5**k)\r
- if lower % 2 == 0:\r
- lower += 1\r
- for i in xrange(TEST_SIZE):\r
- # Select a random odd n in [2**53/5**k,\r
- # 2**54/5**k). Then n * 10**k gives a halfway case\r
- # with small number of significant digits.\r
- n, e = random.randrange(lower, upper, 2), k\r
-\r
- # Remove any additional powers of 5.\r
- while n % 5 == 0:\r
- n, e = n // 5, e + 1\r
- assert n % 10 in (1, 3, 7, 9)\r
-\r
- # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0,\r
- # until n * 2**p2 has more than 20 significant digits.\r
- digits, exponent = n, e\r
- while digits < 10**20:\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
- # Same again, but with extra trailing zeros.\r
- s = '{}e{}'.format(digits * 10**40, exponent - 40)\r
- self.check_strtod(s)\r
- digits *= 2\r
-\r
- # Try numbers of the form n * 5**p2 * 10**(e - p5), p5\r
- # >= 0, with n * 5**p5 < 10**20.\r
- digits, exponent = n, e\r
- while digits < 10**20:\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
- # Same again, but with extra trailing zeros.\r
- s = '{}e{}'.format(digits * 10**40, exponent - 40)\r
- self.check_strtod(s)\r
- digits *= 5\r
- exponent -= 1\r
-\r
- def test_halfway_cases(self):\r
- # test halfway cases for the round-half-to-even rule\r
- for i in xrange(100 * TEST_SIZE):\r
- # bit pattern for a random finite positive (or +0.0) float\r
- bits = random.randrange(2047*2**52)\r
-\r
- # convert bit pattern to a number of the form m * 2**e\r
- e, m = divmod(bits, 2**52)\r
- if e:\r
- m, e = m + 2**52, e - 1\r
- e -= 1074\r
-\r
- # add 0.5 ulps\r
- m, e = 2*m + 1, e - 1\r
-\r
- # convert to a decimal string\r
- if e >= 0:\r
- digits = m << e\r
- exponent = 0\r
- else:\r
- # m * 2**e = (m * 5**-e) * 10**e\r
- digits = m * 5**-e\r
- exponent = e\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
-\r
- def test_boundaries(self):\r
- # boundaries expressed as triples (n, e, u), where\r
- # n*10**e is an approximation to the boundary value and\r
- # u*10**e is 1ulp\r
- boundaries = [\r
- (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0)\r
- (17976931348623159077, 289, 1995), # overflow boundary (2.**1024)\r
- (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022)\r
- (0, -327, 4941), # zero\r
- ]\r
- for n, e, u in boundaries:\r
- for j in xrange(1000):\r
- digits = n + random.randrange(-3*u, 3*u)\r
- exponent = e\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
- n *= 10\r
- u *= 10\r
- e -= 1\r
-\r
- def test_underflow_boundary(self):\r
- # test values close to 2**-1075, the underflow boundary; similar\r
- # to boundary_tests, except that the random error doesn't scale\r
- # with n\r
- for exponent in xrange(-400, -320):\r
- base = 10**-exponent // 2**1075\r
- for j in xrange(TEST_SIZE):\r
- digits = base + random.randrange(-1000, 1000)\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
-\r
- def test_bigcomp(self):\r
- for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50:\r
- dig10 = 10**ndigs\r
- for i in xrange(10 * TEST_SIZE):\r
- digits = random.randrange(dig10)\r
- exponent = random.randrange(-400, 400)\r
- s = '{}e{}'.format(digits, exponent)\r
- self.check_strtod(s)\r
-\r
- def test_parsing(self):\r
- # make '0' more likely to be chosen than other digits\r
- digits = '000000123456789'\r
- signs = ('+', '-', '')\r
-\r
- # put together random short valid strings\r
- # \d*[.\d*]?e\r
- for i in xrange(1000):\r
- for j in xrange(TEST_SIZE):\r
- s = random.choice(signs)\r
- intpart_len = random.randrange(5)\r
- s += ''.join(random.choice(digits) for _ in xrange(intpart_len))\r
- if random.choice([True, False]):\r
- s += '.'\r
- fracpart_len = random.randrange(5)\r
- s += ''.join(random.choice(digits)\r
- for _ in xrange(fracpart_len))\r
- else:\r
- fracpart_len = 0\r
- if random.choice([True, False]):\r
- s += random.choice(['e', 'E'])\r
- s += random.choice(signs)\r
- exponent_len = random.randrange(1, 4)\r
- s += ''.join(random.choice(digits)\r
- for _ in xrange(exponent_len))\r
-\r
- if intpart_len + fracpart_len:\r
- self.check_strtod(s)\r
- else:\r
- try:\r
- float(s)\r
- except ValueError:\r
- pass\r
- else:\r
- assert False, "expected ValueError"\r
-\r
- def test_particular(self):\r
- # inputs that produced crashes or incorrectly rounded results with\r
- # previous versions of dtoa.c, for various reasons\r
- test_strings = [\r
- # issue 7632 bug 1, originally reported failing case\r
- '2183167012312112312312.23538020374420446192e-370',\r
- # 5 instances of issue 7632 bug 2\r
- '12579816049008305546974391768996369464963024663104e-357',\r
- '17489628565202117263145367596028389348922981857013e-357',\r
- '18487398785991994634182916638542680759613590482273e-357',\r
- '32002864200581033134358724675198044527469366773928e-358',\r
- '94393431193180696942841837085033647913224148539854e-358',\r
- '73608278998966969345824653500136787876436005957953e-358',\r
- '64774478836417299491718435234611299336288082136054e-358',\r
- '13704940134126574534878641876947980878824688451169e-357',\r
- '46697445774047060960624497964425416610480524760471e-358',\r
- # failing case for bug introduced by METD in r77451 (attempted\r
- # fix for issue 7632, bug 2), and fixed in r77482.\r
- '28639097178261763178489759107321392745108491825303e-311',\r
- # two numbers demonstrating a flaw in the bigcomp 'dig == 0'\r
- # correction block (issue 7632, bug 3)\r
- '1.00000000000000001e44',\r
- '1.0000000000000000100000000000000000000001e44',\r
- # dtoa.c bug for numbers just smaller than a power of 2 (issue\r
- # 7632, bug 4)\r
- '99999999999999994487665465554760717039532578546e-47',\r
- # failing case for off-by-one error introduced by METD in\r
- # r77483 (dtoa.c cleanup), fixed in r77490\r
- '965437176333654931799035513671997118345570045914469' #...\r
- '6213413350821416312194420007991306908470147322020121018368e0',\r
- # incorrect lsb detection for round-half-to-even when\r
- # bc->scale != 0 (issue 7632, bug 6).\r
- '104308485241983990666713401708072175773165034278685' #...\r
- '682646111762292409330928739751702404658197872319129' #...\r
- '036519947435319418387839758990478549477777586673075' #...\r
- '945844895981012024387992135617064532141489278815239' #...\r
- '849108105951619997829153633535314849999674266169258' #...\r
- '928940692239684771590065027025835804863585454872499' #...\r
- '320500023126142553932654370362024104462255244034053' #...\r
- '203998964360882487378334860197725139151265590832887' #...\r
- '433736189468858614521708567646743455601905935595381' #...\r
- '852723723645799866672558576993978025033590728687206' #...\r
- '296379801363024094048327273913079612469982585674824' #...\r
- '156000783167963081616214710691759864332339239688734' #...\r
- '656548790656486646106983450809073750535624894296242' #...\r
- '072010195710276073042036425579852459556183541199012' #...\r
- '652571123898996574563824424330960027873516082763671875e-1075',\r
- # demonstration that original fix for issue 7632 bug 1 was\r
- # buggy; the exit condition was too strong\r
- '247032822920623295e-341',\r
- # demonstrate similar problem to issue 7632 bug1: crash\r
- # with 'oversized quotient in quorem' message.\r
- '99037485700245683102805043437346965248029601286431e-373',\r
- '99617639833743863161109961162881027406769510558457e-373',\r
- '98852915025769345295749278351563179840130565591462e-372',\r
- '99059944827693569659153042769690930905148015876788e-373',\r
- '98914979205069368270421829889078356254059760327101e-372',\r
- # issue 7632 bug 5: the following 2 strings convert differently\r
- '1000000000000000000000000000000000000000e-16',\r
- '10000000000000000000000000000000000000000e-17',\r
- # issue 7632 bug 7\r
- '991633793189150720000000000000000000000000000000000000000e-33',\r
- # And another, similar, failing halfway case\r
- '4106250198039490000000000000000000000000000000000000000e-38',\r
- # issue 7632 bug 8: the following produced 10.0\r
- '10.900000000000000012345678912345678912345',\r
-\r
- # two humongous values from issue 7743\r
- '116512874940594195638617907092569881519034793229385' #...\r
- '228569165191541890846564669771714896916084883987920' #...\r
- '473321268100296857636200926065340769682863349205363' #...\r
- '349247637660671783209907949273683040397979984107806' #...\r
- '461822693332712828397617946036239581632976585100633' #...\r
- '520260770761060725403904123144384571612073732754774' #...\r
- '588211944406465572591022081973828448927338602556287' #...\r
- '851831745419397433012491884869454462440536895047499' #...\r
- '436551974649731917170099387762871020403582994193439' #...\r
- '761933412166821484015883631622539314203799034497982' #...\r
- '130038741741727907429575673302461380386596501187482' #...\r
- '006257527709842179336488381672818798450229339123527' #...\r
- '858844448336815912020452294624916993546388956561522' #...\r
- '161875352572590420823607478788399460162228308693742' #...\r
- '05287663441403533948204085390898399055004119873046875e-1075',\r
-\r
- '525440653352955266109661060358202819561258984964913' #...\r
- '892256527849758956045218257059713765874251436193619' #...\r
- '443248205998870001633865657517447355992225852945912' #...\r
- '016668660000210283807209850662224417504752264995360' #...\r
- '631512007753855801075373057632157738752800840302596' #...\r
- '237050247910530538250008682272783660778181628040733' #...\r
- '653121492436408812668023478001208529190359254322340' #...\r
- '397575185248844788515410722958784640926528544043090' #...\r
- '115352513640884988017342469275006999104519620946430' #...\r
- '818767147966495485406577703972687838176778993472989' #...\r
- '561959000047036638938396333146685137903018376496408' #...\r
- '319705333868476925297317136513970189073693314710318' #...\r
- '991252811050501448326875232850600451776091303043715' #...\r
- '157191292827614046876950225714743118291034780466325' #...\r
- '085141343734564915193426994587206432697337118211527' #...\r
- '278968731294639353354774788602467795167875117481660' #...\r
- '4738791256853675690543663283782215866825e-1180',\r
-\r
- # exercise exit conditions in bigcomp comparison loop\r
- '2602129298404963083833853479113577253105939995688e2',\r
- '260212929840496308383385347911357725310593999568896e0',\r
- '26021292984049630838338534791135772531059399956889601e-2',\r
- '260212929840496308383385347911357725310593999568895e0',\r
- '260212929840496308383385347911357725310593999568897e0',\r
- '260212929840496308383385347911357725310593999568996e0',\r
- '260212929840496308383385347911357725310593999568866e0',\r
- # 2**53\r
- '9007199254740992.00',\r
- # 2**1024 - 2**970: exact overflow boundary. All values\r
- # smaller than this should round to something finite; any value\r
- # greater than or equal to this one overflows.\r
- '179769313486231580793728971405303415079934132710037' #...\r
- '826936173778980444968292764750946649017977587207096' #...\r
- '330286416692887910946555547851940402630657488671505' #...\r
- '820681908902000708383676273854845817711531764475730' #...\r
- '270069855571366959622842914819860834936475292719074' #...\r
- '168444365510704342711559699508093042880177904174497792',\r
- # 2**1024 - 2**970 - tiny\r
- '179769313486231580793728971405303415079934132710037' #...\r
- '826936173778980444968292764750946649017977587207096' #...\r
- '330286416692887910946555547851940402630657488671505' #...\r
- '820681908902000708383676273854845817711531764475730' #...\r
- '270069855571366959622842914819860834936475292719074' #...\r
- '168444365510704342711559699508093042880177904174497791.999',\r
- # 2**1024 - 2**970 + tiny\r
- '179769313486231580793728971405303415079934132710037' #...\r
- '826936173778980444968292764750946649017977587207096' #...\r
- '330286416692887910946555547851940402630657488671505' #...\r
- '820681908902000708383676273854845817711531764475730' #...\r
- '270069855571366959622842914819860834936475292719074' #...\r
- '168444365510704342711559699508093042880177904174497792.001',\r
- # 1 - 2**-54, +-tiny\r
- '999999999999999944488848768742172978818416595458984375e-54',\r
- '9999999999999999444888487687421729788184165954589843749999999e-54',\r
- '9999999999999999444888487687421729788184165954589843750000001e-54',\r
- ]\r
- for s in test_strings:\r
- self.check_strtod(s)\r
-\r
-def test_main():\r
- test_support.run_unittest(StrtodTests)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r