Mark Dickinson | 647ed91 | 2010-01-14 15:22:33 +0000 | [diff] [blame] | 1 | # Tests for the correctly-rounded string -> float conversions |
| 2 | # introduced in Python 2.7 and 3.1. |
| 3 | |
| 4 | import random |
| 5 | import struct |
| 6 | import unittest |
| 7 | import re |
| 8 | import sys |
| 9 | from test import test_support |
| 10 | |
| 11 | # Correctly rounded str -> float in pure Python, for comparison. |
| 12 | |
| 13 | strtod_parser = re.compile(r""" # A numeric string consists of: |
| 14 | (?P<sign>[-+])? # an optional sign, followed by |
| 15 | (?=\d|\.\d) # a number with at least one digit |
| 16 | (?P<int>\d*) # having a (possibly empty) integer part |
| 17 | (?:\.(?P<frac>\d*))? # followed by an optional fractional part |
| 18 | (?:E(?P<exp>[-+]?\d+))? # and an optional exponent |
| 19 | \Z |
| 20 | """, re.VERBOSE | re.IGNORECASE).match |
| 21 | |
| 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 | |
| 28 | # parse string into a pair of integers 'a' and 'b' such that |
| 29 | # abs(decimal value) = a/b, along with a boolean 'negative'. |
| 30 | m = strtod_parser(s) |
| 31 | if m is None: |
| 32 | raise ValueError('invalid numeric string') |
| 33 | fraction = m.group('frac') or '' |
| 34 | intpart = int(m.group('int') + fraction) |
| 35 | exp = int(m.group('exp') or '0') - len(fraction) |
| 36 | negative = m.group('sign') == '-' |
| 37 | a, b = intpart*10**max(exp, 0), 10**max(0, -exp) |
| 38 | |
| 39 | # quick return for zeros |
| 40 | if not a: |
| 41 | return '-0x0.0p+0' if negative else '0x0.0p+0' |
| 42 | |
| 43 | # compute exponent e for result; may be one too small in the case |
| 44 | # that the rounded value of a/b lies in a different binade from a/b |
| 45 | d = a.bit_length() - b.bit_length() |
| 46 | d += (a >> d if d >= 0 else a << -d) >= b |
| 47 | e = max(d, min_exp) - mant_dig |
| 48 | |
| 49 | # approximate a/b by number of the form q * 2**e; adjust e if necessary |
| 50 | a, b = a << max(-e, 0), b << max(e, 0) |
| 51 | q, r = divmod(a, b) |
| 52 | if 2*r > b or 2*r == b and q & 1: |
| 53 | q += 1 |
| 54 | if q.bit_length() == mant_dig+1: |
| 55 | q //= 2 |
| 56 | e += 1 |
| 57 | |
| 58 | # double check that (q, e) has the right form |
| 59 | assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig |
| 60 | assert q.bit_length() == mant_dig or e == min_exp - mant_dig |
| 61 | |
| 62 | # check for overflow and underflow |
| 63 | if e + q.bit_length() > max_exp: |
| 64 | return '-inf' if negative else 'inf' |
| 65 | if not q: |
| 66 | return '-0x0.0p+0' if negative else '0x0.0p+0' |
| 67 | |
| 68 | # for hex representation, shift so # bits after point is a multiple of 4 |
| 69 | hexdigs = 1 + (mant_dig-2)//4 |
| 70 | shift = 3 - (mant_dig-2)%4 |
| 71 | q, e = q << shift, e - shift |
| 72 | return '{}0x{:x}.{:0{}x}p{:+d}'.format( |
| 73 | '-' if negative else '', |
| 74 | q // 16**hexdigs, |
| 75 | q % 16**hexdigs, |
| 76 | hexdigs, |
| 77 | e + 4*hexdigs) |
| 78 | |
| 79 | TEST_SIZE = 10 |
| 80 | |
| 81 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 82 | "applies only when using short float repr style") |
| 83 | class StrtodTests(unittest.TestCase): |
| 84 | def check_strtod(self, s): |
| 85 | """Compare the result of Python's builtin correctly rounded |
| 86 | string->float conversion (using float) to a pure Python |
| 87 | correctly rounded string->float implementation. Fail if the |
| 88 | two methods give different results.""" |
| 89 | |
| 90 | try: |
| 91 | fs = float(s) |
| 92 | except OverflowError: |
| 93 | got = '-inf' if s[0] == '-' else 'inf' |
| 94 | else: |
| 95 | got = fs.hex() |
| 96 | expected = strtod(s) |
| 97 | self.assertEqual(expected, got, |
| 98 | "Incorrectly rounded str->float conversion for {}: " |
| 99 | "expected {}, got {}".format(s, expected, got)) |
| 100 | |
| 101 | def test_halfway_cases(self): |
| 102 | # test halfway cases for the round-half-to-even rule |
| 103 | for i in xrange(1000): |
| 104 | for j in xrange(TEST_SIZE): |
| 105 | # bit pattern for a random finite positive (or +0.0) float |
| 106 | bits = random.randrange(2047*2**52) |
| 107 | |
| 108 | # convert bit pattern to a number of the form m * 2**e |
| 109 | e, m = divmod(bits, 2**52) |
| 110 | if e: |
| 111 | m, e = m + 2**52, e - 1 |
| 112 | e -= 1074 |
| 113 | |
| 114 | # add 0.5 ulps |
| 115 | m, e = 2*m + 1, e - 1 |
| 116 | |
| 117 | # convert to a decimal string |
| 118 | if e >= 0: |
| 119 | digits = m << e |
| 120 | exponent = 0 |
| 121 | else: |
| 122 | # m * 2**e = (m * 5**-e) * 10**e |
| 123 | digits = m * 5**-e |
| 124 | exponent = e |
| 125 | s = '{}e{}'.format(digits, exponent) |
| 126 | |
| 127 | # for the moment, ignore errors from trailing zeros |
| 128 | if digits % 10 == 0: |
| 129 | continue |
| 130 | self.check_strtod(s) |
| 131 | |
| 132 | # get expected answer via struct, to triple check |
| 133 | #fs = struct.unpack('<d', struct.pack('<Q', bits + (bits&1)))[0] |
| 134 | #self.assertEqual(fs, float(s)) |
| 135 | |
| 136 | def test_boundaries(self): |
| 137 | # boundaries expressed as triples (n, e, u), where |
| 138 | # n*10**e is an approximation to the boundary value and |
| 139 | # u*10**e is 1ulp |
| 140 | boundaries = [ |
| 141 | (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0) |
| 142 | (17976931348623159077, 289, 1995), # overflow boundary (2.**1024) |
| 143 | (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022) |
| 144 | (0, -327, 4941), # zero |
| 145 | ] |
| 146 | for n, e, u in boundaries: |
| 147 | for j in xrange(1000): |
| 148 | for i in xrange(TEST_SIZE): |
| 149 | digits = n + random.randrange(-3*u, 3*u) |
| 150 | exponent = e |
| 151 | s = '{}e{}'.format(digits, exponent) |
| 152 | self.check_strtod(s) |
| 153 | n *= 10 |
| 154 | u *= 10 |
| 155 | e -= 1 |
| 156 | |
| 157 | def test_underflow_boundary(self): |
| 158 | # test values close to 2**-1075, the underflow boundary; similar |
| 159 | # to boundary_tests, except that the random error doesn't scale |
| 160 | # with n |
| 161 | for exponent in xrange(-400, -320): |
| 162 | base = 10**-exponent // 2**1075 |
| 163 | for j in xrange(TEST_SIZE): |
| 164 | digits = base + random.randrange(-1000, 1000) |
| 165 | s = '{}e{}'.format(digits, exponent) |
| 166 | self.check_strtod(s) |
| 167 | |
| 168 | def test_bigcomp(self): |
| 169 | DIG10 = 10**50 |
| 170 | for i in xrange(1000): |
| 171 | for j in xrange(TEST_SIZE): |
| 172 | digits = random.randrange(DIG10) |
| 173 | exponent = random.randrange(-400, 400) |
| 174 | s = '{}e{}'.format(digits, exponent) |
| 175 | self.check_strtod(s) |
| 176 | |
| 177 | def test_parsing(self): |
| 178 | digits = tuple(map(str, xrange(10))) |
| 179 | signs = ('+', '-', '') |
| 180 | |
| 181 | # put together random short valid strings |
| 182 | # \d*[.\d*]?e |
| 183 | for i in xrange(1000): |
| 184 | for j in xrange(TEST_SIZE): |
| 185 | s = random.choice(signs) |
| 186 | intpart_len = random.randrange(5) |
| 187 | s += ''.join(random.choice(digits) for _ in xrange(intpart_len)) |
| 188 | if random.choice([True, False]): |
| 189 | s += '.' |
| 190 | fracpart_len = random.randrange(5) |
| 191 | s += ''.join(random.choice(digits) |
| 192 | for _ in xrange(fracpart_len)) |
| 193 | else: |
| 194 | fracpart_len = 0 |
| 195 | if random.choice([True, False]): |
| 196 | s += random.choice(['e', 'E']) |
| 197 | s += random.choice(signs) |
| 198 | exponent_len = random.randrange(1, 4) |
| 199 | s += ''.join(random.choice(digits) |
| 200 | for _ in xrange(exponent_len)) |
| 201 | |
| 202 | if intpart_len + fracpart_len: |
| 203 | self.check_strtod(s) |
| 204 | else: |
| 205 | try: |
| 206 | float(s) |
| 207 | except ValueError: |
| 208 | pass |
| 209 | else: |
| 210 | assert False, "expected ValueError" |
| 211 | |
| 212 | def test_particular(self): |
| 213 | # inputs that produced crashes or incorrectly rounded results with |
| 214 | # previous versions of dtoa.c, for various reasons |
| 215 | test_strings = [ |
| 216 | # issue 7632 bug 1, originally reported failing case |
| 217 | '2183167012312112312312.23538020374420446192e-370', |
| 218 | # 5 instances of issue 7632 bug 2 |
| 219 | '12579816049008305546974391768996369464963024663104e-357', |
| 220 | '17489628565202117263145367596028389348922981857013e-357', |
| 221 | '18487398785991994634182916638542680759613590482273e-357', |
| 222 | '32002864200581033134358724675198044527469366773928e-358', |
| 223 | '94393431193180696942841837085033647913224148539854e-358', |
| 224 | # failing case for bug introduced by METD in r77451 (attempted |
| 225 | # fix for issue 7632, bug 2), and fixed in r77482. |
| 226 | '28639097178261763178489759107321392745108491825303e-311', |
| 227 | # two numbers demonstrating a flaw in the bigcomp 'dig == 0' |
| 228 | # correction block (issue 7632, bug 3) |
| 229 | '1.00000000000000001e44', |
| 230 | '1.0000000000000000100000000000000000000001e44', |
| 231 | # dtoa.c bug for numbers just smaller than a power of 2 (issue |
| 232 | # 7632, bug 4) |
| 233 | '99999999999999994487665465554760717039532578546e-47', |
| 234 | # failing case for off-by-one error introduced by METD in |
| 235 | # r77483 (dtoa.c cleanup), fixed in r77490 |
| 236 | '965437176333654931799035513671997118345570045914469' #... |
| 237 | '6213413350821416312194420007991306908470147322020121018368e0', |
| 238 | # incorrect lsb detection for round-half-to-even when |
| 239 | # bc->scale != 0 (issue 7632, bug 6). |
| 240 | '104308485241983990666713401708072175773165034278685' #... |
| 241 | '682646111762292409330928739751702404658197872319129' #... |
| 242 | '036519947435319418387839758990478549477777586673075' #... |
| 243 | '945844895981012024387992135617064532141489278815239' #... |
| 244 | '849108105951619997829153633535314849999674266169258' #... |
| 245 | '928940692239684771590065027025835804863585454872499' #... |
| 246 | '320500023126142553932654370362024104462255244034053' #... |
| 247 | '203998964360882487378334860197725139151265590832887' #... |
| 248 | '433736189468858614521708567646743455601905935595381' #... |
| 249 | '852723723645799866672558576993978025033590728687206' #... |
| 250 | '296379801363024094048327273913079612469982585674824' #... |
| 251 | '156000783167963081616214710691759864332339239688734' #... |
| 252 | '656548790656486646106983450809073750535624894296242' #... |
| 253 | '072010195710276073042036425579852459556183541199012' #... |
| 254 | '652571123898996574563824424330960027873516082763671875e-1075', |
| 255 | # demonstration that original fix for issue 7632 bug 1 was |
| 256 | # buggy; the exit condition was too strong |
| 257 | '247032822920623295e-341', |
| 258 | # issue 7632 bug 5: the following 2 strings convert differently |
| 259 | '1000000000000000000000000000000000000000e-16', |
| 260 | #'10000000000000000000000000000000000000000e-17', |
Mark Dickinson | 476279f | 2010-01-16 10:44:00 +0000 | [diff] [blame^] | 261 | # issue 7632 bug 8: the following produced 10.0 |
| 262 | '10.900000000000000012345678912345678912345', |
Mark Dickinson | 647ed91 | 2010-01-14 15:22:33 +0000 | [diff] [blame] | 263 | ] |
| 264 | for s in test_strings: |
| 265 | self.check_strtod(s) |
| 266 | |
| 267 | def test_main(): |
| 268 | test_support.run_unittest(StrtodTests) |
| 269 | |
| 270 | if __name__ == "__main__": |
| 271 | test_main() |