diff options
author | Raymond Hettinger <python@rcn.com> | 2008-06-21 06:39:53 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-06-21 06:39:53 (GMT) |
commit | e0e711446ba62b8b07bc7a14eb53674c9b0a4862 (patch) | |
tree | fab7f820773931d3d4a6c81948afe63ce1d6746a /Objects/floatobject.c | |
parent | db53c1eb68ff19db167e80bcf02cd9984d25f022 (diff) | |
download | cpython-e0e711446ba62b8b07bc7a14eb53674c9b0a4862.zip cpython-e0e711446ba62b8b07bc7a14eb53674c9b0a4862.tar.gz cpython-e0e711446ba62b8b07bc7a14eb53674c9b0a4862.tar.bz2 |
Issue 3008: hex/oct/bin can show floats exactly.
Diffstat (limited to 'Objects/floatobject.c')
-rw-r--r-- | Objects/floatobject.c | 62 |
1 files changed, 60 insertions, 2 deletions
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 @@ PyDoc_STRVAR(float_as_integer_ratio_doc, ">>> (-.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 @@ static PyNumberMethods float_as_number = { 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 @@ static PyNumberMethods float_as_number = { 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 = { |