Skip to content

Commit e0e7114

Browse files
committed
Issue 3008: hex/oct/bin can show floats exactly.
1 parent db53c1e commit e0e7114

3 files changed

Lines changed: 90 additions & 2 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,15 @@ def test_hex(self):
631631
self.assertEqual(hex(-16), '-0x10')
632632
self.assertEqual(hex(-16L), '-0x10L')
633633
self.assertRaises(TypeError, hex, {})
634+
self.assertEqual(hex(3.125), '0x19 * 2.0 ** -3')
635+
self.assertEqual(hex(0.0), '0x0 * 2.0 ** 0')
636+
for sv in float('nan'), float('inf'), float('-inf'):
637+
self.assertEqual(hex(sv), repr(sv))
638+
for i in range(100):
639+
x = random.expovariate(.05)
640+
self.assertEqual(eval(hex(x)), x, (x, hex(x), eval(hex(x))))
641+
self.assertEqual(eval(hex(-x)), -x)
642+
self.assertEqual(hex(-x), ('-' + hex(x)))
634643

635644
def test_id(self):
636645
id(None)
@@ -914,6 +923,15 @@ def test_oct(self):
914923
self.assertEqual(oct(-100), '-0144')
915924
self.assertEqual(oct(-100L), '-0144L')
916925
self.assertRaises(TypeError, oct, ())
926+
self.assertEqual(oct(3.125), '031 * 2.0 ** -3')
927+
self.assertEqual(oct(0.0), '0 * 2.0 ** 0')
928+
for sv in float('nan'), float('inf'), float('-inf'):
929+
self.assertEqual(oct(sv), repr(sv))
930+
for i in range(100):
931+
x = random.expovariate(.05)
932+
self.assertEqual(eval(oct(x)), x)
933+
self.assertEqual(eval(oct(-x)), -x)
934+
self.assertEqual(oct(-x), ('-' + oct(x)))
917935

918936
def write_testfile(self):
919937
# NB the first 4 lines are also used to test input and raw_input, below
@@ -1466,6 +1484,15 @@ def test_bin(self):
14661484
self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
14671485
self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
14681486
self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
1487+
self.assertEqual(bin(3.125), '0b11001 * 2.0 ** -3')
1488+
self.assertEqual(bin(0.0), '0b0 * 2.0 ** 0')
1489+
for sv in float('nan'), float('inf'), float('-inf'):
1490+
self.assertEqual(bin(sv), repr(sv))
1491+
for i in range(100):
1492+
x = random.expovariate(.05)
1493+
self.assertEqual(eval(bin(x)), x)
1494+
self.assertEqual(eval(bin(-x)), -x)
1495+
self.assertEqual(bin(-x), ('-' + bin(x)))
14691496

14701497
class TestSorted(unittest.TestCase):
14711498

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Core and Builtins
1717
slice(None, 10, -1).indices(10) returns (9, 9, -1) instead of (9,
1818
10, -1).
1919

20+
- Issue 3008: hex(), oct(), and bin() can now create exact reprs
21+
for floats.
22+
2023
- Make bin() implementation parallel oct() and hex().
2124

2225

Objects/floatobject.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,62 @@ PyDoc_STRVAR(float_as_integer_ratio_doc,
12041204
">>> (-.25).as_integer_ratio()\n"
12051205
"(-1, 4)");
12061206

1207+
static PyObject *
1208+
_float_to_base(PyFloatObject *v, unaryfunc int_to_base)
1209+
{
1210+
PyObject *mant, *conv, *result;
1211+
double x, fr;
1212+
int i, exp, n;
1213+
char *conv_str;
1214+
1215+
CONVERT_TO_DOUBLE(((PyObject *)v), x);
1216+
if (!Py_IS_FINITE(x))
1217+
return PyObject_Repr((PyObject *)v);
1218+
fr = frexp(x, &exp);
1219+
for (i=0; i<300 && fr != floor(fr) ; i++) {
1220+
fr *= 2.0;
1221+
exp--;
1222+
}
1223+
mant = PyLong_FromDouble(floor(fr));
1224+
if (mant == NULL)
1225+
return NULL;
1226+
conv = int_to_base(mant);
1227+
Py_DECREF(mant);
1228+
if (conv== NULL)
1229+
return NULL;
1230+
n = PyString_GET_SIZE(conv);
1231+
conv_str = PyString_AS_STRING(conv);
1232+
/* Remove the trailing 'L' if present */
1233+
if (n && conv_str[n-1] == 'L') {
1234+
PyObject *newconv = PySequence_GetSlice(conv, 0, -1);
1235+
Py_DECREF(conv);
1236+
if (newconv == NULL)
1237+
return NULL;
1238+
conv = newconv;
1239+
conv_str = PyString_AS_STRING(conv);
1240+
}
1241+
result = PyString_FromFormat("%s * 2.0 ** %d", conv_str, exp);
1242+
Py_DECREF(conv);
1243+
return result;
1244+
}
1245+
1246+
static PyObject *
1247+
float_hex(PyFloatObject *v)
1248+
{
1249+
return _float_to_base(v, PyLong_Type.tp_as_number->nb_hex);
1250+
}
1251+
1252+
static PyObject *
1253+
float_oct(PyFloatObject *v)
1254+
{
1255+
return _float_to_base(v, PyLong_Type.tp_as_number->nb_oct);
1256+
}
1257+
1258+
static PyObject *
1259+
float_bin(PyFloatObject *v)
1260+
{
1261+
return _float_to_base(v, PyLong_Type.tp_as_number->nb_bin);
1262+
}
12071263

12081264
static PyObject *
12091265
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
@@ -1490,8 +1546,8 @@ static PyNumberMethods float_as_number = {
14901546
float_trunc, /*nb_int*/
14911547
float_trunc, /*nb_long*/
14921548
float_float, /*nb_float*/
1493-
0, /* nb_oct */
1494-
0, /* nb_hex */
1549+
(unaryfunc)float_oct, /* nb_oct */
1550+
(unaryfunc)float_hex, /* nb_hex */
14951551
0, /* nb_inplace_add */
14961552
0, /* nb_inplace_subtract */
14971553
0, /* nb_inplace_multiply */
@@ -1507,6 +1563,8 @@ static PyNumberMethods float_as_number = {
15071563
float_div, /* nb_true_divide */
15081564
0, /* nb_inplace_floor_divide */
15091565
0, /* nb_inplace_true_divide */
1566+
0, /* nb_index */
1567+
(unaryfunc)float_bin, /* nb_bin */
15101568
};
15111569

15121570
PyTypeObject PyFloat_Type = {

0 commit comments

Comments
 (0)