Moved Rational._binary_float_to_ratio() to float.as_integer_ratio() because
it's useful outside of rational numbers.

This is my first C code that had to do anything significant. Please be more
careful when looking over it.
diff --git a/Lib/rational.py b/Lib/rational.py
index 8ee38ba..99c5ff6 100755
--- a/Lib/rational.py
+++ b/Lib/rational.py
@@ -25,60 +25,6 @@
     return a
 
 
-def _binary_float_to_ratio(x):
-    """x -> (top, bot), a pair of ints s.t. x = top/bot.
-
-    The conversion is done exactly, without rounding.
-    bot > 0 guaranteed.
-    Some form of binary fp is assumed.
-    Pass NaNs or infinities at your own risk.
-
-    >>> _binary_float_to_ratio(10.0)
-    (10, 1)
-    >>> _binary_float_to_ratio(0.0)
-    (0, 1)
-    >>> _binary_float_to_ratio(-.25)
-    (-1, 4)
-    """
-    # XXX Move this to floatobject.c with a name like
-    # float.as_integer_ratio()
-
-    if x == 0:
-        return 0, 1
-    f, e = math.frexp(x)
-    signbit = 1
-    if f < 0:
-        f = -f
-        signbit = -1
-    assert 0.5 <= f < 1.0
-    # x = signbit * f * 2**e exactly
-
-    # Suck up CHUNK 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 an int.
-    CHUNK = 28
-    top = 0
-    # invariant: x = signbit * (top + f) * 2**e exactly
-    while f:
-        f = math.ldexp(f, CHUNK)
-        digit = trunc(f)
-        assert digit >> CHUNK == 0
-        top = (top << CHUNK) | digit
-        f = f - digit
-        assert 0.0 <= f < 1.0
-        e = e - CHUNK
-    assert top
-
-    # Add in the sign bit.
-    top = signbit * top
-
-    # now x = top * 2**e exactly; fold in 2**e
-    if e>0:
-        return (top * 2**e, 1)
-    else:
-        return (top, 2 ** -e)
-
-
 _RATIONAL_FORMAT = re.compile(
     r'^\s*(?P<sign>[-+]?)(?P<num>\d+)'
     r'(?:/(?P<denom>\d+)|\.(?P<decimal>\d+))?\s*$')
@@ -163,7 +109,7 @@
                             (cls.__name__, f, type(f).__name__))
         if math.isnan(f) or math.isinf(f):
             raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
-        return cls(*_binary_float_to_ratio(f))
+        return cls(*f.as_integer_ratio())
 
     @classmethod
     def from_decimal(cls, dec):
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index f7b7c0c..a1e2a12 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -5,7 +5,7 @@
                               run_unittest, run_with_locale
 from operator import neg
 
-import sys, warnings, cStringIO, random, UserDict
+import sys, warnings, cStringIO, random, rational, UserDict
 warnings.filterwarnings("ignore", "hex../oct.. of negative int",
                         FutureWarning, __name__)
 warnings.filterwarnings("ignore", "integer argument expected",
@@ -688,6 +688,25 @@
         self.assertAlmostEqual(float(Foo3(21)), 42.)
         self.assertRaises(TypeError, float, Foo4(42))
 
+    def test_floatasratio(self):
+        R = rational.Rational
+        self.assertEqual(R(0, 1),
+                         R(*float(0.0).as_integer_ratio()))
+        self.assertEqual(R(5, 2),
+                         R(*float(2.5).as_integer_ratio()))
+        self.assertEqual(R(1, 2),
+                         R(*float(0.5).as_integer_ratio()))
+        self.assertEqual(R(4728779608739021, 2251799813685248),
+                         R(*float(2.1).as_integer_ratio()))
+        self.assertEqual(R(-4728779608739021, 2251799813685248),
+                         R(*float(-2.1).as_integer_ratio()))
+        self.assertEqual(R(-2100, 1),
+                         R(*float(-2100.0).as_integer_ratio()))
+
+        self.assertRaises(OverflowError, float('inf').as_integer_ratio)
+        self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
+        self.assertRaises(ValueError, float('nan').as_integer_ratio)
+
     def test_getattr(self):
         import sys
         self.assert_(getattr(sys, 'stdout') is sys.stdout)