summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2008-02-03 16:51:08 (GMT)
committerChristian Heimes <christian@cheimes.de>2008-02-03 16:51:08 (GMT)
commit292d351fc1a7dc9feccd412843832808680a631f (patch)
tree918a0dea0d70345e0279b9b9649c6e427a076118 /Objects
parentec17d204f963f37da02e6457f676c57fb66db406 (diff)
downloadcpython-292d351fc1a7dc9feccd412843832808680a631f.zip
cpython-292d351fc1a7dc9feccd412843832808680a631f.tar.gz
cpython-292d351fc1a7dc9feccd412843832808680a631f.tar.bz2
Merged revisions 60481,60485,60489-60520,60523-60527,60530-60533,60535-60538,60540-60551 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk NOTE: I blocked the following revisions: svnmerge.py block -r 60521,60522,60528,60529,60534,60539 The new tests must be merged with lots of manual work. ........ r60493 | georg.brandl | 2008-02-01 12:59:08 +0100 (Fri, 01 Feb 2008) | 2 lines Update IPv6 RFC number. ........ r60497 | georg.brandl | 2008-02-01 16:50:15 +0100 (Fri, 01 Feb 2008) | 2 lines Add link checker builder, written for GHOP by Thomas Lamb. ........ r60500 | georg.brandl | 2008-02-01 19:08:09 +0100 (Fri, 01 Feb 2008) | 2 lines Rename batch file. ........ r60504 | christian.heimes | 2008-02-01 19:49:26 +0100 (Fri, 01 Feb 2008) | 1 line More int -> pid_t. ........ r60507 | georg.brandl | 2008-02-01 20:24:01 +0100 (Fri, 01 Feb 2008) | 2 lines Wording nit. ........ r60510 | georg.brandl | 2008-02-01 21:45:33 +0100 (Fri, 01 Feb 2008) | 2 lines Update for latest sphinx latex writer. ........ r60511 | raymond.hettinger | 2008-02-01 22:30:23 +0100 (Fri, 01 Feb 2008) | 1 line Issue #1996: float.as_integer_ratio() should return fraction in lowest terms. ........ r60512 | raymond.hettinger | 2008-02-01 23:15:52 +0100 (Fri, 01 Feb 2008) | 1 line Integer ratio should return ints instead of longs whereever possible. ........ r60513 | raymond.hettinger | 2008-02-01 23:22:50 +0100 (Fri, 01 Feb 2008) | 1 line labs() takes a long for an input. ........ r60514 | raymond.hettinger | 2008-02-01 23:42:59 +0100 (Fri, 01 Feb 2008) | 1 line Test round-trip on float.as_integer_ratio() and float.__truediv__(). ........ r60515 | marc-andre.lemburg | 2008-02-01 23:58:17 +0100 (Fri, 01 Feb 2008) | 3 lines Bump distutils version number to match Python version. ........ r60516 | raymond.hettinger | 2008-02-02 00:12:19 +0100 (Sat, 02 Feb 2008) | 1 line Fix int/long typecase. Add check for non-binary floating point. ........ r60517 | raymond.hettinger | 2008-02-02 00:45:44 +0100 (Sat, 02 Feb 2008) | 1 line Add protection from weirdness while scaling the mantissa to an integer. ........ r60518 | raymond.hettinger | 2008-02-02 06:11:40 +0100 (Sat, 02 Feb 2008) | 1 line Simpler solution to handling non-IEEE 754 environments. ........ r60519 | raymond.hettinger | 2008-02-02 06:24:44 +0100 (Sat, 02 Feb 2008) | 1 line Neaten-up a bit. ........ r60520 | georg.brandl | 2008-02-02 10:56:20 +0100 (Sat, 02 Feb 2008) | 2 lines Amendments to the urllib2 docs, written for GHOP by Thomas Lamb. ........ r60525 | georg.brandl | 2008-02-02 11:49:58 +0100 (Sat, 02 Feb 2008) | 3 lines Add email example how to send a multipart message. Written for GHOP by Martin Matejek. ........ r60526 | georg.brandl | 2008-02-02 12:05:00 +0100 (Sat, 02 Feb 2008) | 2 lines Rewrite test_socketserver as unittest, written for GHOP by Benjamin Petersen. ........ r60527 | georg.brandl | 2008-02-02 12:05:34 +0100 (Sat, 02 Feb 2008) | 2 lines Add GHOP contributor. ........ r60530 | mark.dickinson | 2008-02-02 18:16:13 +0100 (Sat, 02 Feb 2008) | 2 lines Make the Rational constructor accept '3.' and '.2' as well as '3.2'. ........ r60531 | neal.norwitz | 2008-02-02 19:52:51 +0100 (Sat, 02 Feb 2008) | 1 line Update the leaky tests (ie, ignore these tests if they report leaks). This version has been running for a while. ........ r60533 | skip.montanaro | 2008-02-02 20:11:57 +0100 (Sat, 02 Feb 2008) | 7 lines Split the refleak mail body into two parts, the first being those failing tests which are deemed more important issues, the second those which are known to have difficult to solve problems and are generally expected to leak. Hopefully this doesn't break the script... ........ r60535 | georg.brandl | 2008-02-03 01:04:50 +0100 (Sun, 03 Feb 2008) | 3 lines Wait for a delay before reaping children -- this should fix the test_socketserver failures on several platforms. ........ r60536 | brett.cannon | 2008-02-03 03:07:55 +0100 (Sun, 03 Feb 2008) | 2 lines Fix a minor typo. ........ r60537 | brett.cannon | 2008-02-03 03:08:45 +0100 (Sun, 03 Feb 2008) | 3 lines Directories from CPPFLAGS and LDFLAGS were being added in the reverse order for searches as to how they were listed in the environment variable. ........ r60538 | brett.cannon | 2008-02-03 03:34:14 +0100 (Sun, 03 Feb 2008) | 2 lines Remove extra tick marks and add a missing closing parenthesis. ........ r60540 | andrew.macintyre | 2008-02-03 07:58:06 +0100 (Sun, 03 Feb 2008) | 2 lines Update OS/2 EMX build bits for 2.6. ........ r60541 | andrew.macintyre | 2008-02-03 08:01:11 +0100 (Sun, 03 Feb 2008) | 2 lines Rename module definition file to reflect v2.6. ........ r60542 | andrew.macintyre | 2008-02-03 08:07:31 +0100 (Sun, 03 Feb 2008) | 6 lines The wrapper function is supposed to be for spawnvpe() so that's what we should call [this wrapper only available on OS/2]. Backport candidate to 2.5. ........ r60544 | gregory.p.smith | 2008-02-03 08:20:53 +0100 (Sun, 03 Feb 2008) | 6 lines Merge this fix from the pybsddb tree: r293 | jcea | 2008-01-31 01:08:19 -0800 (Thu, 31 Jan 2008) | 4 lines Solved memory leak when using cursors with databases without environment. ........ r60546 | gregory.p.smith | 2008-02-03 09:01:46 +0100 (Sun, 03 Feb 2008) | 2 lines remove a repeated occurance of a hardcoded berkeleydb library version number ........ r60549 | brett.cannon | 2008-02-03 10:59:21 +0100 (Sun, 03 Feb 2008) | 2 lines Add an entry for r60537. ........ r60550 | georg.brandl | 2008-02-03 13:29:00 +0100 (Sun, 03 Feb 2008) | 2 lines #2003: fix sentence. ........ r60551 | christian.heimes | 2008-02-03 15:34:18 +0100 (Sun, 03 Feb 2008) | 2 lines Fixed paths to Windows build directories in build_ext.py Use vsbuild instead of devenv in build.bat and _bsddb.vcproj ........
Diffstat (limited to 'Objects')
-rw-r--r--Objects/floatobject.c117
1 files changed, 29 insertions, 88 deletions
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 5cb3201..9b499ba 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1066,20 +1066,19 @@ float_float(PyObject *v)
}
static PyObject *
-float_as_integer_ratio(PyObject *v)
+float_as_integer_ratio(PyObject *v, PyObject *unused)
{
double self;
double float_part;
int exponent;
- int is_negative;
- const int chunk_size = 28;
+ int i;
+
PyObject *prev;
- PyObject *py_chunk = NULL;
PyObject *py_exponent = NULL;
PyObject *numerator = NULL;
PyObject *denominator = NULL;
PyObject *result_pair = NULL;
- PyNumberMethods *long_methods;
+ PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
#define INPLACE_UPDATE(obj, call) \
prev = obj; \
@@ -1101,93 +1100,31 @@ float_as_integer_ratio(PyObject *v)
}
#endif
- if (self == 0) {
- numerator = PyLong_FromLong(0);
- if (numerator == NULL) goto error;
- denominator = PyLong_FromLong(1);
- if (denominator == NULL) goto error;
- result_pair = PyTuple_Pack(2, numerator, denominator);
- /* Hand ownership over to the tuple. If the tuple
- wasn't created successfully, we want to delete the
- ints anyway. */
- Py_DECREF(numerator);
- Py_DECREF(denominator);
- return result_pair;
- }
-
- /* XXX: Could perhaps handle FLT_RADIX!=2 by using ilogb and
- scalbn, but those may not be in C89. */
PyFPE_START_PROTECT("as_integer_ratio", goto error);
- float_part = frexp(self, &exponent);
- is_negative = 0;
- if (float_part < 0) {
- float_part = -float_part;
- is_negative = 1;
- /* 0.5 <= float_part < 1.0 */
- }
+ float_part = frexp(self, &exponent); /* self == float_part * 2**exponent exactly */
PyFPE_END_PROTECT(float_part);
- /* abs(self) == float_part * 2**exponent exactly */
-
- /* Suck up chunk_size bits at a time; 28 is enough so that we
- suck up all bits in 2 iterations for all known binary
- double-precision formats, and small enough to fit in a
- long. */
- numerator = PyLong_FromLong(0);
+
+ for (i=0; i<300 && float_part != floor(float_part) ; i++) {
+ float_part *= 2.0;
+ exponent--;
+ }
+ /* self == float_part * 2**exponent exactly and float_part is integral.
+ If FLT_RADIX != 2, the 300 steps may leave a tiny fractional part
+ to be truncated by PyLong_FromDouble(). */
+
+ numerator = PyLong_FromDouble(float_part);
if (numerator == NULL) goto error;
- long_methods = PyLong_Type.tp_as_number;
-
- py_chunk = PyLong_FromLong(chunk_size);
- if (py_chunk == NULL) goto error;
-
- while (float_part != 0) {
- /* invariant: abs(self) ==
- (numerator + float_part) * 2**exponent exactly */
- long digit;
- PyObject *py_digit;
-
- PyFPE_START_PROTECT("as_integer_ratio", goto error);
- /* Pull chunk_size bits out of float_part, into digits. */
- float_part = ldexp(float_part, chunk_size);
- digit = (long)float_part;
- float_part -= digit;
- /* 0 <= float_part < 1 */
- exponent -= chunk_size;
- PyFPE_END_PROTECT(float_part);
-
- /* Shift digits into numerator. */
- // numerator <<= chunk_size
- INPLACE_UPDATE(numerator,
- long_methods->nb_lshift(numerator, py_chunk));
- if (numerator == NULL) goto error;
-
- // numerator |= digit
- py_digit = PyLong_FromLong(digit);
- if (py_digit == NULL) goto error;
- INPLACE_UPDATE(numerator,
- long_methods->nb_or(numerator, py_digit));
- Py_DECREF(py_digit);
- if (numerator == NULL) goto error;
- }
-
- /* Add in the sign bit. */
- if (is_negative) {
- INPLACE_UPDATE(numerator,
- long_methods->nb_negative(numerator));
- if (numerator == NULL) goto error;
- }
-
- /* now self = numerator * 2**exponent exactly; fold in 2**exponent */
+ /* fold in 2**exponent */
denominator = PyLong_FromLong(1);
- py_exponent = PyLong_FromLong(labs(exponent));
+ py_exponent = PyLong_FromLong(labs((long)exponent));
if (py_exponent == NULL) goto error;
INPLACE_UPDATE(py_exponent,
long_methods->nb_lshift(denominator, py_exponent));
if (py_exponent == NULL) goto error;
if (exponent > 0) {
INPLACE_UPDATE(numerator,
- long_methods->nb_multiply(numerator,
- py_exponent));
+ long_methods->nb_multiply(numerator, py_exponent));
if (numerator == NULL) goto error;
}
else {
@@ -1196,12 +1133,17 @@ float_as_integer_ratio(PyObject *v)
py_exponent = NULL;
}
+ /* Returns ints instead of longs where possible */
+ INPLACE_UPDATE(numerator, PyNumber_Int(numerator));
+ if (numerator == NULL) goto error;
+ INPLACE_UPDATE(denominator, PyNumber_Int(denominator));
+ if (denominator == NULL) goto error;
+
result_pair = PyTuple_Pack(2, numerator, denominator);
#undef INPLACE_UPDATE
error:
Py_XDECREF(py_exponent);
- Py_XDECREF(py_chunk);
Py_XDECREF(denominator);
Py_XDECREF(numerator);
return result_pair;
@@ -1210,17 +1152,16 @@ error:
PyDoc_STRVAR(float_as_integer_ratio_doc,
"float.as_integer_ratio() -> (int, int)\n"
"\n"
-"Returns a pair of integers, not necessarily in lowest terms, whose\n"
-"ratio is exactly equal to the original float. This method raises an\n"
-"OverflowError on infinities and a ValueError on nans. The resulting\n"
-"denominator will be positive.\n"
+"Returns a pair of integers, whose ratio is exactly equal to the original\n"
+"float and with a positive denominator.\n"
+"Raises OverflowError on infinities and a ValueError on nans.\n"
"\n"
">>> (10.0).as_integer_ratio()\n"
-"(167772160L, 16777216L)\n"
+"(10, 1)\n"
">>> (0.0).as_integer_ratio()\n"
"(0, 1)\n"
">>> (-.25).as_integer_ratio()\n"
-"(-134217728L, 536870912L)");
+"(-1, 4)");
static PyObject *