Issue 3008: hex/oct/bin can show floats exactly.
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 15d80a3..8c46ab8 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -631,6 +631,15 @@
self.assertEqual(hex(-16), '-0x10')
self.assertEqual(hex(-16L), '-0x10L')
self.assertRaises(TypeError, hex, {})
+ self.assertEqual(hex(3.125), '0x19 * 2.0 ** -3')
+ self.assertEqual(hex(0.0), '0x0 * 2.0 ** 0')
+ for sv in float('nan'), float('inf'), float('-inf'):
+ self.assertEqual(hex(sv), repr(sv))
+ for i in range(100):
+ x = random.expovariate(.05)
+ self.assertEqual(eval(hex(x)), x, (x, hex(x), eval(hex(x))))
+ self.assertEqual(eval(hex(-x)), -x)
+ self.assertEqual(hex(-x), ('-' + hex(x)))
def test_id(self):
id(None)
@@ -914,6 +923,15 @@
self.assertEqual(oct(-100), '-0144')
self.assertEqual(oct(-100L), '-0144L')
self.assertRaises(TypeError, oct, ())
+ self.assertEqual(oct(3.125), '031 * 2.0 ** -3')
+ self.assertEqual(oct(0.0), '0 * 2.0 ** 0')
+ for sv in float('nan'), float('inf'), float('-inf'):
+ self.assertEqual(oct(sv), repr(sv))
+ for i in range(100):
+ x = random.expovariate(.05)
+ self.assertEqual(eval(oct(x)), x)
+ self.assertEqual(eval(oct(-x)), -x)
+ self.assertEqual(oct(-x), ('-' + oct(x)))
def write_testfile(self):
# NB the first 4 lines are also used to test input and raw_input, below
@@ -1466,6 +1484,15 @@
self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
+ self.assertEqual(bin(3.125), '0b11001 * 2.0 ** -3')
+ self.assertEqual(bin(0.0), '0b0 * 2.0 ** 0')
+ for sv in float('nan'), float('inf'), float('-inf'):
+ self.assertEqual(bin(sv), repr(sv))
+ for i in range(100):
+ x = random.expovariate(.05)
+ self.assertEqual(eval(bin(x)), x)
+ self.assertEqual(eval(bin(-x)), -x)
+ self.assertEqual(bin(-x), ('-' + bin(x)))
class TestSorted(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
index 96d45af..2825198 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@
slice(None, 10, -1).indices(10) returns (9, 9, -1) instead of (9,
10, -1).
+- Issue 3008: hex(), oct(), and bin() can now create exact reprs
+ for floats.
+
- Make bin() implementation parallel oct() and hex().
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 32e7cc8..bbe8a19 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1204,6 +1204,62 @@
">>> (-.25).as_integer_ratio()\n"
"(-1, 4)");
+static PyObject *
+_float_to_base(PyFloatObject *v, unaryfunc int_to_base)
+{
+ PyObject *mant, *conv, *result;
+ double x, fr;
+ int i, exp, n;
+ char *conv_str;
+
+ CONVERT_TO_DOUBLE(((PyObject *)v), x);
+ if (!Py_IS_FINITE(x))
+ return PyObject_Repr((PyObject *)v);
+ fr = frexp(x, &exp);
+ for (i=0; i<300 && fr != floor(fr) ; i++) {
+ fr *= 2.0;
+ exp--;
+ }
+ mant = PyLong_FromDouble(floor(fr));
+ if (mant == NULL)
+ return NULL;
+ conv = int_to_base(mant);
+ Py_DECREF(mant);
+ if (conv== NULL)
+ return NULL;
+ n = PyString_GET_SIZE(conv);
+ conv_str = PyString_AS_STRING(conv);
+ /* Remove the trailing 'L' if present */
+ if (n && conv_str[n-1] == 'L') {
+ PyObject *newconv = PySequence_GetSlice(conv, 0, -1);
+ Py_DECREF(conv);
+ if (newconv == NULL)
+ return NULL;
+ conv = newconv;
+ conv_str = PyString_AS_STRING(conv);
+ }
+ result = PyString_FromFormat("%s * 2.0 ** %d", conv_str, exp);
+ Py_DECREF(conv);
+ return result;
+}
+
+static PyObject *
+float_hex(PyFloatObject *v)
+{
+ return _float_to_base(v, PyLong_Type.tp_as_number->nb_hex);
+}
+
+static PyObject *
+float_oct(PyFloatObject *v)
+{
+ return _float_to_base(v, PyLong_Type.tp_as_number->nb_oct);
+}
+
+static PyObject *
+float_bin(PyFloatObject *v)
+{
+ return _float_to_base(v, PyLong_Type.tp_as_number->nb_bin);
+}
static PyObject *
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
@@ -1490,8 +1546,8 @@
float_trunc, /*nb_int*/
float_trunc, /*nb_long*/
float_float, /*nb_float*/
- 0, /* nb_oct */
- 0, /* nb_hex */
+ (unaryfunc)float_oct, /* nb_oct */
+ (unaryfunc)float_hex, /* nb_hex */
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
@@ -1507,6 +1563,8 @@
float_div, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
+ 0, /* nb_index */
+ (unaryfunc)float_bin, /* nb_bin */
};
PyTypeObject PyFloat_Type = {