Index: Objects/floatobject.c =================================================================== --- Objects/floatobject.c (revision 64402) +++ Objects/floatobject.c (working copy) @@ -1204,8 +1204,45 @@ ">>> (-.25).as_integer_ratio()\n" "(-1, 4)"); +static PyObject * +_float_to_base(PyFloatObject *v, unaryfunc int_to_base) +{ + PyObject *mant, *converted, *result; + double x, fr; + int i, exponent; + CONVERT_TO_DOUBLE(((PyObject *)v), x); + if (!Py_IS_FINITE(x)) + return PyObject_Repr((PyObject *)v); + fr = frexp(x, &exponent); + for (i=0; i<300 && fr != floor(fr) ; i++) { + fr *= 2.0; + exponent--; + } + mant = PyLong_FromDouble(floor(fr)); + if (mant == NULL) + return NULL; + converted = int_to_base(mant); + Py_DECREF(mant); + result = PyString_FromFormat("%s * 2.0 ** %d", + PyString_AS_STRING(converted), exponent); + Py_DECREF(converted); + 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_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject * @@ -1490,8 +1527,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 */ Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (revision 64402) +++ Lib/test/test_builtin.py (working copy) @@ -631,6 +631,10 @@ self.assertEqual(hex(-16), '-0x10') self.assertEqual(hex(-16L), '-0x10L') self.assertRaises(TypeError, hex, {}) + self.assertEqual(hex(3.125), '0x19L * 2.0 ** -3') + for i in range(100): + x = random.expovariate(.05) + self.assertEqual(eval(hex(x)), x, (x, hex(x), eval(hex(x)))) def test_id(self): id(None) @@ -914,6 +918,10 @@ self.assertEqual(oct(-100), '-0144') self.assertEqual(oct(-100L), '-0144L') self.assertRaises(TypeError, oct, ()) + self.assertEqual(oct(3.125), '031L * 2.0 ** -3') + for i in range(100): + x = random.expovariate(.05) + self.assertEqual(eval(oct(x)), x) def write_testfile(self): # NB the first 4 lines are also used to test input and raw_input, below