Index: Objects/floatobject.c =================================================================== --- Objects/floatobject.c (revision 64700) +++ Objects/floatobject.c (working copy) @@ -10,6 +10,9 @@ #include #include +#undef MAX +#define MAX(x, y) ((x) < (y) ? (y) : (x)) + #ifdef HAVE_IEEEFP_H #include #endif @@ -1106,7 +1109,263 @@ return v; } +static int +hex_from_char(char c) { + /* This assumes ASCII, or at least something that corresponds to ASCII + for character values < 127. In other character sets it may not be + true that 'a' through 'f' (or 'A' through 'F') are consecutive. */ + assert(isxdigit(c)); + return ('0' <= c && c <= '9' ? (int)(c - '0') : + 'a' <= c && c <= 'f' ? (int)(c - 'a') + 10 : + (int)(c - 'A') + 10); +} + static PyObject * +float_fromhex(PyObject *cls, PyObject *arg) +{ + double x = 0.0; + PyObject *result_as_float, *result; + long exp, base_exp, top_exp, lsb, dropped_digits; + char *s, *coeff_start, *dot_pos = NULL, *coeff_end, *exp_start; + char *key_digit, *s_start, c, c2; + int half, kd=0, round_up, sign=1; + Py_ssize_t length, ndigits; + + if (PyString_AsStringAndSize(arg, &s_start, &length)) + return NULL; + + /* parse leading whitespace and optional sign */ + s = s_start; + while (isspace(*s)) + s++; + if (*s == '-') { + s++; + sign = -1.0; + } + else if (*s == '+') + s++; + + /* deal with specials */ + if (PyOS_mystrnicmp(s, "nan", 4) == 0) { + x = Py_NAN; + goto finished; + } + if (PyOS_mystrnicmp(s, "inf", 4) == 0 || + PyOS_mystrnicmp(s, "infinity", 9) == 0) { + x = sign*Py_HUGE_VAL; + goto finished; + } + + /* parse nonspecial. s[coeff_start : coeff_end] contains coefficient. + If there's a dot, dot_pos is its position; otherwise dot_pos == + coeff_end. */ + if (*s++ != '0' || (tolower(*s++) != (int)'x')) + goto parse_error; + coeff_start = s; + while (isxdigit(*s)) + s++; + dot_pos = s; + if (*s == '.') { + s++; + while (isxdigit(*s)) + s++; + } + coeff_end = s; + ndigits = coeff_end - coeff_start; + if (dot_pos != coeff_end) + /* compensate for hexadecimal point */ + ndigits--; + if (ndigits == 0) + goto parse_error; + if (tolower(*s++) != (int)'p') + goto parse_error; + exp_start = s; + if (*s == '-' || *s == '+') + s++; + if (!isdigit(*s++)) + goto parse_error; + while (isdigit(*s)) + s++; + while (isspace(*s)) + s++; + if ((Py_ssize_t)(s - s_start) != length) + goto parse_error; + + /* + * For the sake of simplicity and correctness, we impose an artificial + * limit on the total number of hex digits in the input string. The + * limit is chosen to ensure that, writing exp for the exponent, + * + * (1) if exp > LONG_MAX/2 then the value of the hex string is + * guaranteed to overflow (provided it's nonzero) + * + * (2) if exp < LONG_MIN/2 then the value of the hex string is + * guaranteed to underflow to 0. + * + * (3) if LONG_MIN/2 <= exp <= LONG_MAX/2 then there's no danger of + * overflow in the various exponent calculations below. + */ + if (ndigits > (LONG_MAX/2 + 1 - + MAX(DBL_MANT_DIG-DBL_MIN_EXP, DBL_MAX_EXP))/4) + goto insane_length_error; + + /* skip leading zeros in the coefficient */ + for (s = coeff_start; s < coeff_end; s++) { + if (*s == '.') + continue; + if (*s != '0') { + break; + } + } + if (s == coeff_end) { + /* entire coefficient consists of zeros; return +-0 */ + x = sign * 0.0; + goto finished; + } + + exp = strtol(exp_start, NULL, 10); + if (exp < LONG_MIN/2) { + x = sign * 0.0; + goto finished; + } + if (exp > LONG_MAX/2) + goto overflow_error; + + /* + * base_exp is the exponent of the least significant bit of the + * coefficient, taking the exponent into account. top_exp is 1 more + * than the exponent of the most significant bit of the coefficient. + * In other words, top_exp - base_exp is the number of bits in the + * coefficient, and the absolute value of the string is in the + * interval [2**(top_exp-1), 2**top_exp). + */ + if (dot_pos == coeff_end) + base_exp = (long)exp; + else + base_exp = (long)exp - 4*(long)(coeff_end - dot_pos - 1); + + if (s < dot_pos) + top_exp = (long)exp + 4*(long)(dot_pos - s - 1); + else + top_exp = (long)exp + 4*(long)(dot_pos - s); + kd = hex_from_char(*s); + while (kd != 0) { + kd /= 2; + top_exp += 1; + } + + if (top_exp < DBL_MIN_EXP - DBL_MANT_DIG) { + x = sign * 0.0; + goto finished; + } + if (top_exp > DBL_MAX_EXP) + goto overflow_error; + + /* lsb = exponent of least significant bit of *rounded* value. This is + usually top_exp - DBL_MANT_DIG, but may be greater than this for + subnormal numbers. */ + lsb = MAX(top_exp, (long)DBL_MIN_EXP) - DBL_MANT_DIG; + + x = 0.0; + if (base_exp >= lsb) { + /* no rounding required here. */ + while (s < coeff_end) { + c = *s++; + if (c == '.') + continue; + x = 16.0*x + hex_from_char(c); + } + } + else { + /* there's at least one bit too many in the coefficient, so we + need to round using round-half-even. */ + assert(lsb - base_exp - 1 >= 0); + half = 1 << (int)((lsb - base_exp - 1) % 4); + dropped_digits = (lsb - base_exp - 1) / 4; + base_exp += 4*dropped_digits; + + /* key_digit is a pointer to the hex digit that contains the + first bit that's going to be rounded away. This is the bit + that usually determines the rounding direction. */ + key_digit = coeff_end - 1 - dropped_digits; + if (key_digit <= dot_pos && dot_pos != coeff_end) { + key_digit -= 1; + } + + /* accumulate value from digits before the key digit */ + while (s < key_digit) { + c = *s++; + if (c == '.') + continue; + x = 16.0*x + hex_from_char(c); + } + /* add in value from key digit, suitably masked */ + c2 = *s++; + if (c2 == '.') + c2 = *s++; + kd = hex_from_char(c2); + if (half == 8) + kd += 16*hex_from_char(c); + x = 16.0*x + (double)(kd & (16-2*half)); + /* do the rounding */ + if ((kd & half) != 0) { + /* rounding bit is a 1, so round up unless bit lsb is + 0 and all bits following the rounding bit are 0. */ + round_up = 0; + if ((kd & (3*half-1)) != 0) + round_up = 1; + else + while (s < coeff_end) { + c = *s++; + if (c == '.') + continue; + if (hex_from_char(c) != 0) { + round_up = 1; + break; + } + } + if (round_up == 1) { + double eps = (double)(2*half); + x += eps; + /* overflow corner case */ + if (top_exp == DBL_MAX_EXP && + x == ldexp(eps, DBL_MANT_DIG)) + goto overflow_error; + } + } + } + x = sign * ldexp(x, (int)(base_exp)); + + finished: + result_as_float = Py_BuildValue("(d)", x); + if (result_as_float == NULL) + return NULL; + result = PyObject_CallObject(cls, result_as_float); + Py_DECREF(result_as_float); + return result; + + parse_error: + PyErr_SetString(PyExc_ValueError, + "invalid hexadecimal floating-point string"); + return NULL; + + insane_length_error: + PyErr_SetString(PyExc_ValueError, + "hexadecimal string too long to convert"); + return NULL; + + overflow_error: + PyErr_SetString(PyExc_OverflowError, + "hexadecimal value too large to represent as a float"); + return NULL; + +} + + + + + +static PyObject * float_as_integer_ratio(PyObject *v, PyObject *unused) { double self; @@ -1430,6 +1689,8 @@ "Returns the Integral closest to x between 0 and x."}, {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio_doc}, + {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, + "Creates a float from a hexadecimal string."}, {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, "Returns True if the float is an integer."}, #if 0 Index: Lib/test/test_float.py =================================================================== --- Lib/test/test_float.py (revision 64700) +++ Lib/test/test_float.py (working copy) @@ -3,7 +3,7 @@ import os from test import test_support import math -from math import isinf, isnan +from math import isinf, isnan, copysign, ldexp import operator INF = float("inf") @@ -327,7 +327,357 @@ self.failIf(NAN.is_inf()) self.failIf((0.).is_inf()) +fromHex = float.fromhex +# temporary substitute for float.hex +def toHex(x): + """Express a float as a hexadecimal floating-point string.""" + from math import frexp, copysign + import sys + mant_dig = sys.float_info.mant_dig + min_exp = sys.float_info.min_exp + nbits = mant_dig + (1-mant_dig) % 4 + if isnan(x) or isinf(x): + return str(x) + sign = '-' if copysign(1., x) < 0 else '' + if not x: + return sign + '0x0p+0' + m, e = frexp(abs(x)) + shift = nbits - max(min_exp - e, 0) + c = '%%0%dx' % ((nbits + 3)//4) % int(m*2**shift) + return '%s0x%s.%sp%+d' % (sign, c[0], c[1:], e - 1 - shift + nbits) + +class HexFloatTestCase(unittest.TestCase): + MAX = fromHex('0x.fffffffffffff8p+1024') # max normal + MIN = fromHex('0x1p-1022') # min normal + TINY = fromHex('0x0.0000000000001p-1022') # min subnormal + EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up + + def identical(self, x, y): + # check that floats x and y are identical, or that both + # are NaNs + if isnan(x) or isnan(y): + if isnan(x) == isnan(y): + return + elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)): + return + self.fail('%r not identical to %r' % (x, y)) + + def test_ends(self): + self.identical(self.MIN, 2.**-1022) + self.identical(self.TINY, 2.**-1074) + self.identical(self.EPS, 2.**-52) + self.identical(self.MAX, 2.*(2.**1023 - 2.**970)) + + def test_invalid_inputs(self): + invalid_inputs = [ + 'infi', # misspelt infinities and nans + '-Infinit', + '++inf', + '-+Inf', + '--nan', + '+-NaN', + 'snan', + 'NaNs', + 'nna', + '0xnan', + '0x1.0', # no exponent + '1.0p0', # no hex indicator + 'x1.0p0', + '0xX1.0p0', + '+ 0x1.0p0', # internal whitespace + '- 0x1.0p0', + '0 x1.0p0', + '0x 1.0p0', + '0x1 2.0p0', + '+0x1 .0p0', + '0x1. 0p0', + '-0x1.0 1p0', + '-0x1.0 p0', + '+0x1.0p +0', + '0x1.0p -0', + '0x1.0p 0', + '+0x1.0p+ 0', + '-0x1.0p- 0', + '++0x1.0p-0', # double signs + '--0x1.0p0', + '+-0x1.0p+0', + '-+0x1.0p0', + '0x1.0p++0', + '+0x1.0p+-0', + '-0x1.0p-+0', + '0x1.0p--0', + '0x1.0.p0', + '0x.p0', # no hex digits before or after point + '0x1pa', + u'0x1p\uff10', # fullwidth Unicode digits + u'\uff10x1p0', + u'0x\uff11p0', + u'0x1.\uff10p0', + '0x1p0 \n 0x2p0', + '0x1p0\0 0x1p0', # embedded null byte is not end of string + ] + for x in invalid_inputs: + self.assertRaises(ValueError, fromHex, x) + + + def test_from_hex(self): + MIN = self.MIN; + MAX = self.MAX; + TINY = self.TINY; + EPS = self.EPS; + + # two spellings of infinity, with optional signs; case-insensitive + self.identical(fromHex('inf'), INF) + self.identical(fromHex('+Inf'), INF) + self.identical(fromHex('-INF'), -INF) + self.identical(fromHex('iNf'), INF) + self.identical(fromHex('Infinity'), INF) + self.identical(fromHex('+INFINITY'), INF) + self.identical(fromHex('-infinity'), -INF) + self.identical(fromHex('-iNFiNitY'), -INF) + + # nans with optional sign; case insensitive + self.identical(fromHex('nan'), NAN) + self.identical(fromHex('+NaN'), NAN) + self.identical(fromHex('-NaN'), NAN) + self.identical(fromHex('-nAN'), NAN) + + # variations in input format + self.identical(fromHex('0x1p0'), 1.0) + self.identical(fromHex('0X1p0'), 1.0) + self.identical(fromHex('0X1P0'), 1.0) + self.identical(fromHex('0x1P0'), 1.0) + self.identical(fromHex('0x1.p0'), 1.0) + self.identical(fromHex('0x1.0p0'), 1.0) + self.identical(fromHex('0x.1p4'), 1.0) + self.identical(fromHex('0x.1p04'), 1.0) + self.identical(fromHex('0x.1p004'), 1.0) + self.identical(fromHex('0x1p+0'), 1.0) + self.identical(fromHex('0x1P-0'), 1.0) + self.identical(fromHex('+0x1p0'), 1.0) + self.identical(fromHex('0x01p0'), 1.0) + self.identical(fromHex('0x1p00'), 1.0) + self.identical(fromHex(u'0x1p0'), 1.0) + self.identical(fromHex(' 0x1p0 '), 1.0) + self.identical(fromHex('\n 0x1p0'), 1.0) + self.identical(fromHex('0x1p0 \t'), 1.0) + self.identical(fromHex('0xap0'), 10.0) + self.identical(fromHex('0xAp0'), 10.0) + self.identical(fromHex('0xaP0'), 10.0) + self.identical(fromHex('0xAP0'), 10.0) + self.identical(fromHex('0xbep0'), 190.0) + self.identical(fromHex('0xBep0'), 190.0) + self.identical(fromHex('0xbEp0'), 190.0) + self.identical(fromHex('0XBE0P-4'), 190.0) + self.identical(fromHex('0xBEp0'), 190.0) + self.identical(fromHex('0xB.Ep4'), 190.0) + self.identical(fromHex('0x.BEp8'), 190.0) + self.identical(fromHex('0x.0BEp12'), 190.0) + + # results that should overflow... + self.assertRaises(OverflowError, fromHex, '-0x1p1024') + self.assertRaises(OverflowError, fromHex, '0x1p+1025') + self.assertRaises(OverflowError, fromHex, '+0X1p1030') + self.assertRaises(OverflowError, fromHex, '-0x1p+1100') + self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789') + self.assertRaises(OverflowError, fromHex, '+0X.8p+1025') + self.assertRaises(OverflowError, fromHex, '+0x0.8p1025') + self.assertRaises(OverflowError, fromHex, '-0x0.4p1026') + self.assertRaises(OverflowError, fromHex, '0X2p+1023') + self.assertRaises(OverflowError, fromHex, '0x2.p1023') + self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023') + self.assertRaises(OverflowError, fromHex, '+0X4p+1022') + self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023') + self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023') + self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023') + self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022') + self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970') + self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960') + self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960') + + # ...and those that round to +-max float + self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX) + self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX) + self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX) + + # zeros + self.identical(fromHex('0x0p0'), 0.0) + self.identical(fromHex('0x0p1000'), 0.0) + self.identical(fromHex('-0x0p1023'), -0.0) + self.identical(fromHex('0X0p1024'), 0.0) + self.identical(fromHex('-0x0p1025'), -0.0) + self.identical(fromHex('0X0p2000'), 0.0) + self.identical(fromHex('0x0p123456789123456789'), 0.0) + self.identical(fromHex('-0X0p-0'), -0.0) + self.identical(fromHex('-0X0p-1000'), -0.0) + self.identical(fromHex('0x0p-1023'), 0.0) + self.identical(fromHex('-0X0p-1024'), -0.0) + self.identical(fromHex('-0x0p-1025'), -0.0) + self.identical(fromHex('-0x0p-1072'), -0.0) + self.identical(fromHex('0X0p-1073'), 0.0) + self.identical(fromHex('-0x0p-1074'), -0.0) + self.identical(fromHex('0x0p-1075'), 0.0) + self.identical(fromHex('0X0p-1076'), 0.0) + self.identical(fromHex('-0X0p-2000'), -0.0) + self.identical(fromHex('-0x0p-123456789123456789'), -0.0) + + # values that should underflow to 0 + self.identical(fromHex('0X1p-1075'), 0.0) + self.identical(fromHex('-0X1p-1075'), -0.0) + self.identical(fromHex('-0x1p-123456789123456789'), -0.0) + self.identical(fromHex('0x1.00000000000000001p-1075'), TINY) + self.identical(fromHex('-0x1.1p-1075'), -TINY) + self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY) + + # check round-half-even is working correctly near 0 ... + self.identical(fromHex('0x1p-1076'), 0.0) + self.identical(fromHex('0X2p-1076'), 0.0) + self.identical(fromHex('0X3p-1076'), TINY) + self.identical(fromHex('0x4p-1076'), TINY) + self.identical(fromHex('0X5p-1076'), TINY) + self.identical(fromHex('0X6p-1076'), 2*TINY) + self.identical(fromHex('0x7p-1076'), 2*TINY) + self.identical(fromHex('0X8p-1076'), 2*TINY) + self.identical(fromHex('0X9p-1076'), 2*TINY) + self.identical(fromHex('0xap-1076'), 2*TINY) + self.identical(fromHex('0Xbp-1076'), 3*TINY) + self.identical(fromHex('0xcp-1076'), 3*TINY) + self.identical(fromHex('0Xdp-1076'), 3*TINY) + self.identical(fromHex('0Xep-1076'), 4*TINY) + self.identical(fromHex('0xfp-1076'), 4*TINY) + self.identical(fromHex('0x10p-1076'), 4*TINY) + self.identical(fromHex('-0x1p-1076'), -0.0) + self.identical(fromHex('-0X2p-1076'), -0.0) + self.identical(fromHex('-0x3p-1076'), -TINY) + self.identical(fromHex('-0X4p-1076'), -TINY) + self.identical(fromHex('-0x5p-1076'), -TINY) + self.identical(fromHex('-0x6p-1076'), -2*TINY) + self.identical(fromHex('-0X7p-1076'), -2*TINY) + self.identical(fromHex('-0X8p-1076'), -2*TINY) + self.identical(fromHex('-0X9p-1076'), -2*TINY) + self.identical(fromHex('-0Xap-1076'), -2*TINY) + self.identical(fromHex('-0xbp-1076'), -3*TINY) + self.identical(fromHex('-0xcp-1076'), -3*TINY) + self.identical(fromHex('-0Xdp-1076'), -3*TINY) + self.identical(fromHex('-0xep-1076'), -4*TINY) + self.identical(fromHex('-0Xfp-1076'), -4*TINY) + self.identical(fromHex('-0X10p-1076'), -4*TINY) + + # ... and near MIN ... + self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY) + self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY) + self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY) + self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY) + self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY) + self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY) + self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY) + self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY) + self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY) + self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN) + self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN) + self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN) + self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN) + self.identical(fromHex('0x1.00000000000000p-1022'), MIN) + self.identical(fromHex('0x1.00000000000002p-1022'), MIN) + self.identical(fromHex('0x1.00000000000004p-1022'), MIN) + self.identical(fromHex('0x1.00000000000006p-1022'), MIN) + self.identical(fromHex('0x1.00000000000008p-1022'), MIN) + self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY) + self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY) + self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY) + self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY) + self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY) + self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY) + self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY) + self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY) + + # ... and near 1.0. + self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS) + self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS) + self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS) + self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS) + self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS) + self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2) + self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2) + self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2) + self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2) + self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2) + self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2) + self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2) + self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0) + self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0) + self.identical(fromHex('0X0.fffffffffffffep0'), 1.0) + self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0) + self.identical(fromHex('0X1.00000000000000p0'), 1.0) + self.identical(fromHex('0X1.00000000000001p0'), 1.0) + self.identical(fromHex('0x1.00000000000002p0'), 1.0) + self.identical(fromHex('0X1.00000000000003p0'), 1.0) + self.identical(fromHex('0x1.00000000000004p0'), 1.0) + self.identical(fromHex('0X1.00000000000005p0'), 1.0) + self.identical(fromHex('0X1.00000000000006p0'), 1.0) + self.identical(fromHex('0X1.00000000000007p0'), 1.0) + self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'), 1.0) + self.identical(fromHex('0x1.00000000000008p0'), 1.0) + self.identical(fromHex('0x1.00000000000008000000000000000001p0'), 1+EPS) + self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS) + self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS) + self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS) + self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS) + self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS) + self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS) + self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS) + self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS) + self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS) + self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS) + self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'), 1.0+EPS) + self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS) + self.identical(fromHex('0X1.00000000000018000000000000000001p0'), 1+2*EPS) + self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS) + self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS) + self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS) + self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS) + self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS) + self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS) + self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS) + self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS) + + def test_roundtrip(self): + def roundtrip(x): + return fromHex(toHex(x)) + + for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]: + self.identical(x, roundtrip(x)) + self.identical(-x, roundtrip(-x)) + + # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x. + import random + for i in xrange(10000): + e = random.randrange(-1200, 1200) + m = random.random() + s = random.choice([1.0, -1.0]) + try: + x = s*ldexp(m, e) + except OverflowError: + pass + else: + self.identical(x, fromHex(toHex(x))) + + pass + def test_main(): test_support.run_unittest( FormatFunctionsTestCase, @@ -335,6 +685,7 @@ IEEEFormatTestCase, ReprTestCase, InfNanTest, + HexFloatTestCase, ) if __name__ == '__main__':