+++ /dev/null
-'''\
-This module implements rational numbers.
-
-The entry point of this module is the function
- rat(numerator, denominator)
-If either numerator or denominator is of an integral or rational type,
-the result is a rational number, else, the result is the simplest of
-the types float and complex which can hold numerator/denominator.
-If denominator is omitted, it defaults to 1.
-Rational numbers can be used in calculations with any other numeric
-type. The result of the calculation will be rational if possible.
-
-There is also a test function with calling sequence
- test()
-The documentation string of the test function contains the expected
-output.
-'''
-
-# Contributed by Sjoerd Mullender
-
-from types import *
-
-def gcd(a, b):
- '''Calculate the Greatest Common Divisor.'''
- while b:
- a, b = b, a%b
- return a
-
-def rat(num, den = 1):
- # must check complex before float
- if isinstance(num, complex) or isinstance(den, complex):
- # numerator or denominator is complex: return a complex
- return complex(num) / complex(den)
- if isinstance(num, float) or isinstance(den, float):
- # numerator or denominator is float: return a float
- return float(num) / float(den)
- # otherwise return a rational
- return Rat(num, den)
-
-class Rat:
- '''This class implements rational numbers.'''
-
- def __init__(self, num, den = 1):
- if den == 0:
- raise ZeroDivisionError('rat(x, 0)')
-
- # normalize
-
- # must check complex before float
- if (isinstance(num, complex) or
- isinstance(den, complex)):
- # numerator or denominator is complex:
- # normalized form has denominator == 1+0j
- self.__num = complex(num) / complex(den)
- self.__den = complex(1)
- return
- if isinstance(num, float) or isinstance(den, float):
- # numerator or denominator is float:
- # normalized form has denominator == 1.0
- self.__num = float(num) / float(den)
- self.__den = 1.0
- return
- if (isinstance(num, self.__class__) or
- isinstance(den, self.__class__)):
- # numerator or denominator is rational
- new = num / den
- if not isinstance(new, self.__class__):
- self.__num = new
- if isinstance(new, complex):
- self.__den = complex(1)
- else:
- self.__den = 1.0
- else:
- self.__num = new.__num
- self.__den = new.__den
- else:
- # make sure numerator and denominator don't
- # have common factors
- # this also makes sure that denominator > 0
- g = gcd(num, den)
- self.__num = num / g
- self.__den = den / g
- # try making numerator and denominator of IntType if they fit
- try:
- numi = int(self.__num)
- deni = int(self.__den)
- except (OverflowError, TypeError):
- pass
- else:
- if self.__num == numi and self.__den == deni:
- self.__num = numi
- self.__den = deni
-
- def __repr__(self):
- return 'Rat(%s,%s)' % (self.__num, self.__den)
-
- def __str__(self):
- if self.__den == 1:
- return str(self.__num)
- else:
- return '(%s/%s)' % (str(self.__num), str(self.__den))
-
- # a + b
- def __add__(a, b):
- try:
- return rat(a.__num * b.__den + b.__num * a.__den,
- a.__den * b.__den)
- except OverflowError:
- return rat(int(a.__num) * int(b.__den) +
- int(b.__num) * int(a.__den),
- int(a.__den) * int(b.__den))
-
- def __radd__(b, a):
- return Rat(a) + b
-
- # a - b
- def __sub__(a, b):
- try:
- return rat(a.__num * b.__den - b.__num * a.__den,
- a.__den * b.__den)
- except OverflowError:
- return rat(int(a.__num) * int(b.__den) -
- int(b.__num) * int(a.__den),
- int(a.__den) * int(b.__den))
-
- def __rsub__(b, a):
- return Rat(a) - b
-
- # a * b
- def __mul__(a, b):
- try:
- return rat(a.__num * b.__num, a.__den * b.__den)
- except OverflowError:
- return rat(int(a.__num) * int(b.__num),
- int(a.__den) * int(b.__den))
-
- def __rmul__(b, a):
- return Rat(a) * b
-
- # a / b
- def __div__(a, b):
- try:
- return rat(a.__num * b.__den, a.__den * b.__num)
- except OverflowError:
- return rat(int(a.__num) * int(b.__den),
- int(a.__den) * int(b.__num))
-
- def __rdiv__(b, a):
- return Rat(a) / b
-
- # a % b
- def __mod__(a, b):
- div = a / b
- try:
- div = int(div)
- except OverflowError:
- div = int(div)
- return a - b * div
-
- def __rmod__(b, a):
- return Rat(a) % b
-
- # a ** b
- def __pow__(a, b):
- if b.__den != 1:
- if isinstance(a.__num, complex):
- a = complex(a)
- else:
- a = float(a)
- if isinstance(b.__num, complex):
- b = complex(b)
- else:
- b = float(b)
- return a ** b
- try:
- return rat(a.__num ** b.__num, a.__den ** b.__num)
- except OverflowError:
- return rat(int(a.__num) ** b.__num,
- int(a.__den) ** b.__num)
-
- def __rpow__(b, a):
- return Rat(a) ** b
-
- # -a
- def __neg__(a):
- try:
- return rat(-a.__num, a.__den)
- except OverflowError:
- # a.__num == sys.maxint
- return rat(-int(a.__num), a.__den)
-
- # abs(a)
- def __abs__(a):
- return rat(abs(a.__num), a.__den)
-
- # int(a)
- def __int__(a):
- return int(a.__num / a.__den)
-
- # long(a)
- def __long__(a):
- return int(a.__num) / int(a.__den)
-
- # float(a)
- def __float__(a):
- return float(a.__num) / float(a.__den)
-
- # complex(a)
- def __complex__(a):
- return complex(a.__num) / complex(a.__den)
-
- # cmp(a,b)
- def __cmp__(a, b):
- diff = Rat(a - b)
- if diff.__num < 0:
- return -1
- elif diff.__num > 0:
- return 1
- else:
- return 0
-
- def __rcmp__(b, a):
- return cmp(Rat(a), b)
-
- # a != 0
- def __bool__(a):
- return a.__num != 0
-
-def test():
- '''\
- Test function for rat module.
-
- The expected output is (module some differences in floating
- precission):
- -1
- -1
- 0 0L 0.1 (0.1+0j)
- [Rat(1,2), Rat(-3,10), Rat(1,25), Rat(1,4)]
- [Rat(-3,10), Rat(1,25), Rat(1,4), Rat(1,2)]
- 0
- (11/10)
- (11/10)
- 1.1
- OK
- 2 1.5 (3/2) (1.5+1.5j) (15707963/5000000)
- 2 2 2.0 (2+0j)
-
- 4 0 4 1 4 0
- 3.5 0.5 3.0 1.33333333333 2.82842712475 1
- (7/2) (1/2) 3 (4/3) 2.82842712475 1
- (3.5+1.5j) (0.5-1.5j) (3+3j) (0.666666666667-0.666666666667j) (1.43248815986+2.43884761145j) 1
- 1.5 1 1.5 (1.5+0j)
-
- 3.5 -0.5 3.0 0.75 2.25 -1
- 3.0 0.0 2.25 1.0 1.83711730709 0
- 3.0 0.0 2.25 1.0 1.83711730709 1
- (3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1
- (3/2) 1 1.5 (1.5+0j)
-
- (7/2) (-1/2) 3 (3/4) (9/4) -1
- 3.0 0.0 2.25 1.0 1.83711730709 -1
- 3 0 (9/4) 1 1.83711730709 0
- (3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1
- (1.5+1.5j) (1.5+1.5j)
-
- (3.5+1.5j) (-0.5+1.5j) (3+3j) (0.75+0.75j) 4.5j -1
- (3+1.5j) 1.5j (2.25+2.25j) (1+1j) (1.18235814075+2.85446505899j) 1
- (3+1.5j) 1.5j (2.25+2.25j) (1+1j) (1.18235814075+2.85446505899j) 1
- (3+3j) 0j 4.5j (1+0j) (-0.638110484918+0.705394566962j) 0
- '''
- print(rat(-1, 1))
- print(rat(1, -1))
- a = rat(1, 10)
- print(int(a), int(a), float(a), complex(a))
- b = rat(2, 5)
- l = [a+b, a-b, a*b, a/b]
- print(l)
- l.sort()
- print(l)
- print(rat(0, 1))
- print(a+1)
- print(a+1)
- print(a+1.0)
- try:
- print(rat(1, 0))
- raise SystemError('should have been ZeroDivisionError')
- except ZeroDivisionError:
- print('OK')
- print(rat(2), rat(1.5), rat(3, 2), rat(1.5+1.5j), rat(31415926,10000000))
- list = [2, 1.5, rat(3,2), 1.5+1.5j]
- for i in list:
- print(i, end=' ')
- if not isinstance(i, complex):
- print(int(i), float(i), end=' ')
- print(complex(i))
- print()
- for j in list:
- print(i + j, i - j, i * j, i / j, i ** j, end=' ')
- if not (isinstance(i, complex) or
- isinstance(j, complex)):
- print(cmp(i, j))
- print()
-
-
-if __name__ == '__main__':
- test()
also :term:`immutable`.
named tuple
- Any tuple-like class whose indexable fields are also accessible with
+ Any tuple subclass whose indexable fields are also accessible with
named attributes (for example, :func:`time.localtime` returns a
tuple-like object where the *year* is accessible either with an
index such as ``t[0]`` or with a named attribute like ``t.tm_year``).
math.rst
cmath.rst
decimal.rst
+ rational.rst
random.rst
itertools.rst
functools.rst
.. method:: Queue.qsize()
- Return the approximate size of the queue. Because of multithreading semantics,
- this number is not reliable.
+ Return the approximate size of the queue. Note, qsize() > 0 doesn't
+ guarantee that a subsequent get() will not block, nor will qsize() < maxsize
+ guarantee that put() will not block.
.. method:: Queue.put(item[, block[, timeout]])
--- /dev/null
+
+:mod:`rational` --- Rational numbers
+====================================
+
+.. module:: rational
+ :synopsis: Rational numbers.
+.. moduleauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
+.. sectionauthor:: Jeffrey Yasskin <jyasskin at gmail.com>
+.. versionadded:: 2.6
+
+
+The :mod:`rational` module defines an immutable, infinite-precision
+Rational number class.
+
+
+.. class:: Rational(numerator=0, denominator=1)
+ Rational(other_rational)
+
+ The first version requires that *numerator* and *denominator* are
+ instances of :class:`numbers.Integral` and returns a new
+ ``Rational`` representing ``numerator/denominator``. If
+ *denominator* is :const:`0`, raises a :exc:`ZeroDivisionError`. The
+ second version requires that *other_rational* is an instance of
+ :class:`numbers.Rational` and returns an instance of
+ :class:`Rational` with the same value.
+
+ Implements all of the methods and operations from
+ :class:`numbers.Rational` and is hashable.
+
+
+.. method:: Rational.from_float(flt)
+
+ This classmethod constructs a :class:`Rational` representing the
+ exact value of *flt*, which must be a :class:`float`. Beware that
+ ``Rational.from_float(0.3)`` is not the same value as ``Rational(3,
+ 10)``
+
+
+.. method:: Rational.__floor__()
+
+ Returns the greatest :class:`int` ``<= self``. Will be accessible
+ through :func:`math.floor` in Py3k.
+
+
+.. method:: Rational.__ceil__()
+
+ Returns the least :class:`int` ``>= self``. Will be accessible
+ through :func:`math.ceil` in Py3k.
+
+
+.. method:: Rational.__round__()
+ Rational.__round__(ndigits)
+
+ The first version returns the nearest :class:`int` to ``self``,
+ rounding half to even. The second version rounds ``self`` to the
+ nearest multiple of ``Rational(1, 10**ndigits)`` (logically, if
+ ``ndigits`` is negative), again rounding half toward even. Will be
+ accessible through :func:`round` in Py3k.
+
+
+.. seealso::
+
+ Module :mod:`numbers`
+ The abstract base classes making up the numeric tower.
+
+------------------------------+------------------------------------------+
| :const:`no_site` | -S |
+------------------------------+------------------------------------------+
- | :const:`ingnore_environment` | -E |
+ | :const:`ignore_environment` | -E |
+------------------------------+------------------------------------------+
| :const:`tabcheck` | -t or -tt |
+------------------------------+------------------------------------------+
This is a context manager than runs the :keyword:`with` statement body using
a :class:`StringIO.StringIO` object as sys.stdout. That object can be
- retrieved using the ``as`` clause of the with statement.
+ retrieved using the ``as`` clause of the :keyword:`with` statement.
Example use::
that implements the :option:`-m` switch now does this, so relative imports
can now be used in scripts running from inside a package.
+.. ======================================================================
+
+.. ::
+
+ .. _pep-0370:
+
+ PEP 370: XXX
+ =====================================================
+
+ When you run Python, the module search page ``sys.modules`` usually
+ includes a directory whose path ends in ``"site-packages"``. This
+ directory is intended to hold locally-installed packages available to
+ all users on a machine or using a particular site installation.
+
+ Python 2.6 introduces a convention for user-specific site directories.
+
+ .. seealso::
+
+ :pep:`370` - XXX
+
+ PEP written by XXX; implemented by Christian Heimes.
+
+
.. ======================================================================
.. _pep-3110:
Optimizations
-------------
+* Type objects now have a cache of methods that can reduce
+ the amount of work required to find the correct method implementation
+ for a particular class; once cached, the interpreter doesn't need to
+ traverse base classes to figure out the right method to call.
+ The cache is cleared if a base class or the class itself is modified,
+ so the cache should remain correct even in the face of Python's dynamic
+ nature.
+ (Original optimization implemented by Armin Rigo, updated for
+ Python 2.6 by Kevin Jacobs.)
+
+ .. % Patch 1700288
+
* All of the functions in the :mod:`struct` module have been rewritten in
C, thanks to work at the Need For Speed sprint.
(Contributed by Raymond Hettinger.)
>>> v2
variable(id=1, name='amplitude', type='int', size=4)
+ Where the new :class:`namedtuple` type proved suitable, the standard
+ library has been modified to return them. For example,
+ the :meth:`Decimal.as_tuple` method now returns a named tuple with
+ :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
+
(Contributed by Raymond Hettinger.)
* Another change to the :mod:`collections` module is that the
>>> Decimal(1000).log10()
Decimal("3")
- (Implemented by Facundo Batista and Mark Dickinson.)
+ The :meth:`as_tuple` method of :class:`Decimal` objects now returns a
+ named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields.
+
+ (Implemented by Facundo Batista and Mark Dickinson. Named tuple
+ support added by Raymond Hettinger.)
+
+* The :mod:`difflib` module's :class:`SequenceMatcher` class
+ now returns named tuples representing matches.
+ In addition to behaving like tuples, the returned values
+ also have :attr:`a`, :attr:`b`, and :attr:`size` attributes.
+ (Contributed by Raymond Hettinger.)
* An optional ``timeout`` parameter was added to the
:class:`ftplib.FTP` class constructor as well as the :meth:`connect`
class constructors, specifying a timeout measured in seconds.
(Added by Facundo Batista.)
+* Most of the :mod:`inspect` module's functions, such as
+ :func:`getmoduleinfo` and :func:`getargs`, now return named tuples.
+ In addition to behaving like tuples, the elements of the return value
+ can also be accessed as attributes.
+ (Contributed by Raymond Hettinger.)
+
* A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2,
...[, fillvalue])`` makes tuples from each of the elements; if some of the
iterables are shorter than others, the missing values are set to *fillvalue*.
.. Issue 1727780
+* Long regular expression searches carried out by the :mod:`re`
+ module will now check for signals being delivered, so especially
+ long searches can now be interrupted.
+ (Contributed by Josh Hoyt and Ralf Schmitt.)
+
+ .. % Patch 846388
+
* The :mod:`rgbimg` module has been removed.
* The :mod:`sets` module has been deprecated; it's better to
.. Patch #957003
+* The :mod:`socket` module now supports TIPC (http://tipc.sf.net),
+ a high-performance non-IP-based protocol designed for use in clustered
+ environments. TIPC addresses are 4- or 5-tuples.
+ (Contributed by Alberto Bertogli.)
+
+ .. Patch #1646
+
* A new variable in the :mod:`sys` module,
- :attr:`float_info`, is a dictionary
+ :attr:`float_info`, is an object
containing information about the platform's floating-point support
- derived from the :file:`float.h` file. Key/value pairs
- in this dictionary include
- ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"``
+ derived from the :file:`float.h` file. Attributes of this object
+ include
+ :attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon`
(smallest difference between 1.0 and the next largest value
representable), and several others. (Contributed by Christian Heimes.)
.. Patch 1534
+ Another new variable, :attr:`dont_write_bytecode`, controls whether Python
+ writes any :file:`.pyc` or :file:`.pyo` files on importing a module.
+ If this variable is true, the compiled files are not written. The
+ variable is initially set on start-up by supplying the :option:`-B`
+ switch to the Python interpreter, or by setting the
+ :envvar:`PYTHONDONTWRITEBYTECODE` environment variable before
+ running the interpreter. Python code can subsequently
+ change the value of this variable to control whether bytecode files
+ are written or not.
+ (Contributed by Neal Norwitz and Georg Brandl.)
+
+ Information about the command-line arguments supplied to the Python
+ interpreter are available as attributes of a ``sys.flags`` named
+ tuple. For example, the :attr:`verbose` attribute is true if Python
+ was executed in verbose mode, :attr:`debug` is true in debugging mode, etc.
+ These attributes are all read-only.
+ (Contributed by Christian Heimes.)
+
* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and
POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar
format that was already supported. The default format
information. (Contributed by Alan McIntyre as part of his
project for Google's Summer of Code 2007.)
+* The :mod:`zipfile` module's :class:`ZipFile` class now has
+ :meth:`extract` and :meth:`extractall` methods that will unpack
+ a single file or all the files in the archive to the current directory, or
+ to a specified directory::
+
+ z = zipfile.ZipFile('python-251.zip')
+
+ # Unpack a single file, writing it relative to the /tmp directory.
+ z.extract('Python/sysmodule.c', '/tmp')
+
+ # Unpack all the files in the archive.
+ z.extractall()
+
+ (Contributed by Alan McIntyre.)
+ .. % Patch 467924
+
.. ======================================================================
.. whole new modules get described in subsections here
value, as does the :func:`getwche` function. The :func:`putwch` function
takes a Unicode character and writes it to the console.
-Platform-specific changes go here.
+* The :mod:`_winreg` module now has a function,
+ :func:`ExpandEnvironmentStrings`,
+ that expands environment variable references such as ``%NAME%``
+ in an input string. The handle objects provided by this
+ module now support the context protocol, so they can be used
+ in :keyword:`with` statements.
.. ======================================================================
Other Changes and Fixes
=======================
-As usual, there were a bunch of other improvements and bugfixes scattered
-throughout the source tree. A search through the change logs finds there were
-XXX patches applied and YYY bugs fixed between Python 2.5 and 2.6. Both figures
-are likely to be underestimates.
+As usual, there were a bunch of other improvements and bugfixes
+scattered throughout the source tree. A search through the change
+logs finds there were XXX patches applied and YYY bugs fixed between
+Python 2.5 and 2.6. Both figures are likely to be underestimates.
Some of the more notable changes are:
-* Details will go here.
+* It's now possible to prevent Python from writing any :file:`.pyc`
+ or :file:`.pyo` files by either supplying the :option:`-B` switch
+ or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable
+ to any non-empty string when running the Python interpreter. These
+ are also used to set the :data:`sys.dont_write_bytecode` attribute;
+ Python code can change this variable to control whether bytecode
+ files are subsequently written.
+ (Contributed by Neal Norwitz and Georg Brandl.)
.. ======================================================================
before adding elements from the iterable. This change makes the
behavior match that of ``list.__init__()``.
+* The :class:`Decimal` constructor now accepts leading and trailing
+ whitespace when passed a string. Previously it would raise an
+ :exc:`InvalidOperation` exception. On the other hand, the
+ :meth:`create_decimal` method of :class:`Context` objects now
+ explicitly disallows extra whitespace, raising a
+ :exc:`ConversionSyntax` exception.
+
+* Due to an implementation accident, if you passed a file path to
+ the built-in :func:`__import__` function, it would actually import
+ the specified file. This was never intended to work, however, and
+ the implementation now explicitly checks for this case and raises
+ an :exc:`ImportError`.
+
* The :mod:`socket` module exception :exc:`socket.error` now inherits
from :exc:`IOError`. Previously it wasn't a subclass of
:exc:`StandardError` but now it is, through :exc:`IOError`.
TODO: Fill out more detailed documentation on the operators."""
+from __future__ import division
from abc import ABCMeta, abstractmethod, abstractproperty
__all__ = ["Number", "Exact", "Inexact",
def __complex__(self):
"""Return a builtin complex instance. Called for complex(self)."""
- def __bool__(self):
+ # Will be __bool__ in 3.0.
+ def __nonzero__(self):
"""True if self != 0. Called for bool(self)."""
return self != 0
"""-self"""
raise NotImplementedError
+ @abstractmethod
def __pos__(self):
"""+self"""
raise NotImplementedError
@abstractmethod
def __div__(self, other):
- """self / other; should promote to float or complex when necessary."""
+ """self / other without __future__ division
+
+ May promote to float.
+ """
raise NotImplementedError
@abstractmethod
def __rdiv__(self, other):
- """other / self"""
+ """other / self without __future__ division"""
+ raise NotImplementedError
+
+ @abstractmethod
+ def __truediv__(self, other):
+ """self / other with __future__ division.
+
+ Should promote to float when necessary.
+ """
+ raise NotImplementedError
+
+ @abstractmethod
+ def __rtruediv__(self, other):
+ """other / self with __future__ division"""
raise NotImplementedError
@abstractmethod
globals = self.curframe.f_globals
try:
code = compile(line + '\n', '<stdin>', 'single')
- exec(code, globals, locals)
+ try:
+ sys.stdin = self.stdin
+ sys.stdout = self.stdout
+ exec(code, globals, locals)
+ finally:
+ sys.stdout = save_stdout
+ sys.stdin = save_stdin
except:
t, v = sys.exc_info()[:2]
if type(t) == type(''):
sys.settrace(None)
globals = self.curframe.f_globals
locals = self.curframe.f_locals
- p = Pdb()
+ p = Pdb(self.completekey, self.stdin, self.stdout)
p.prompt = "(%s) " % self.prompt.strip()
print("ENTERING RECURSIVE DEBUGGER", file=self.stdout)
sys.call_tracing(p.run, (arg, globals, locals))
--- /dev/null
+# Originally contributed by Sjoerd Mullender.
+# Significantly modified by Jeffrey Yasskin <jyasskin at gmail.com>.
+
+"""Rational, infinite-precision, real numbers."""
+
+from __future__ import division
+import math
+import numbers
+import operator
+
+__all__ = ["Rational"]
+
+RationalAbc = numbers.Rational
+
+
+def _gcd(a, b):
+ """Calculate the Greatest Common Divisor.
+
+ Unless b==0, the result will have the same sign as b (so that when
+ b is divided by it, the result comes out positive).
+ """
+ while b:
+ a, b = b, a%b
+ 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)
+ """
+
+ 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)
+
+
+class Rational(RationalAbc):
+ """This class implements rational numbers.
+
+ Rational(8, 6) will produce a rational number equivalent to
+ 4/3. Both arguments must be Integral. The numerator defaults to 0
+ and the denominator defaults to 1 so that Rational(3) == 3 and
+ Rational() == 0.
+
+ """
+
+ __slots__ = ('_numerator', '_denominator')
+
+ def __init__(self, numerator=0, denominator=1):
+ if (not isinstance(numerator, numbers.Integral) and
+ isinstance(numerator, RationalAbc) and
+ denominator == 1):
+ # Handle copies from other rationals.
+ other_rational = numerator
+ numerator = other_rational.numerator
+ denominator = other_rational.denominator
+
+ if (not isinstance(numerator, numbers.Integral) or
+ not isinstance(denominator, numbers.Integral)):
+ raise TypeError("Rational(%(numerator)s, %(denominator)s):"
+ " Both arguments must be integral." % locals())
+
+ if denominator == 0:
+ raise ZeroDivisionError('Rational(%s, 0)' % numerator)
+
+ g = _gcd(numerator, denominator)
+ self._numerator = int(numerator // g)
+ self._denominator = int(denominator // g)
+
+ @classmethod
+ def from_float(cls, f):
+ """Converts a float to a rational number, exactly."""
+ if not isinstance(f, float):
+ raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
+ (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))
+
+ @property
+ def numerator(a):
+ return a._numerator
+
+ @property
+ def denominator(a):
+ return a._denominator
+
+ def __repr__(self):
+ """repr(self)"""
+ return ('rational.Rational(%r,%r)' %
+ (self.numerator, self.denominator))
+
+ def __str__(self):
+ """str(self)"""
+ if self.denominator == 1:
+ return str(self.numerator)
+ else:
+ return '(%s/%s)' % (self.numerator, self.denominator)
+
+ def _operator_fallbacks(monomorphic_operator, fallback_operator):
+ """Generates forward and reverse operators given a purely-rational
+ operator and a function from the operator module.
+
+ Use this like:
+ __op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op)
+
+ """
+ def forward(a, b):
+ if isinstance(b, RationalAbc):
+ # Includes ints.
+ return monomorphic_operator(a, b)
+ elif isinstance(b, float):
+ return fallback_operator(float(a), b)
+ elif isinstance(b, complex):
+ return fallback_operator(complex(a), b)
+ else:
+ return NotImplemented
+ forward.__name__ = '__' + fallback_operator.__name__ + '__'
+ forward.__doc__ = monomorphic_operator.__doc__
+
+ def reverse(b, a):
+ if isinstance(a, RationalAbc):
+ # Includes ints.
+ return monomorphic_operator(a, b)
+ elif isinstance(a, numbers.Real):
+ return fallback_operator(float(a), float(b))
+ elif isinstance(a, numbers.Complex):
+ return fallback_operator(complex(a), complex(b))
+ else:
+ return NotImplemented
+ reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
+ reverse.__doc__ = monomorphic_operator.__doc__
+
+ return forward, reverse
+
+ def _add(a, b):
+ """a + b"""
+ return Rational(a.numerator * b.denominator +
+ b.numerator * a.denominator,
+ a.denominator * b.denominator)
+
+ __add__, __radd__ = _operator_fallbacks(_add, operator.add)
+
+ def _sub(a, b):
+ """a - b"""
+ return Rational(a.numerator * b.denominator -
+ b.numerator * a.denominator,
+ a.denominator * b.denominator)
+
+ __sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub)
+
+ def _mul(a, b):
+ """a * b"""
+ return Rational(a.numerator * b.numerator, a.denominator * b.denominator)
+
+ __mul__, __rmul__ = _operator_fallbacks(_mul, operator.mul)
+
+ def _div(a, b):
+ """a / b"""
+ return Rational(a.numerator * b.denominator,
+ a.denominator * b.numerator)
+
+ __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
+ __div__, __rdiv__ = _operator_fallbacks(_div, operator.truediv)
+
+ @classmethod
+ def _floordiv(cls, a, b):
+ div = a / b
+ if isinstance(div, RationalAbc):
+ # trunc(math.floor(div)) doesn't work if the rational is
+ # more precise than a float because the intermediate
+ # rounding may cross an integer boundary.
+ return div.numerator // div.denominator
+ else:
+ return math.floor(div)
+
+ def __floordiv__(a, b):
+ """a // b"""
+ # Will be math.floor(a / b) in 3.0.
+ return a._floordiv(a, b)
+
+ def __rfloordiv__(b, a):
+ """a // b"""
+ # Will be math.floor(a / b) in 3.0.
+ return b._floordiv(a, b)
+
+ @classmethod
+ def _mod(cls, a, b):
+ div = a // b
+ return a - b * div
+
+ def __mod__(a, b):
+ """a % b"""
+ return a._mod(a, b)
+
+ def __rmod__(b, a):
+ """a % b"""
+ return b._mod(a, b)
+
+ def __pow__(a, b):
+ """a ** b
+
+ If b is not an integer, the result will be a float or complex
+ since roots are generally irrational. If b is an integer, the
+ result will be rational.
+
+ """
+ if isinstance(b, RationalAbc):
+ if b.denominator == 1:
+ power = b.numerator
+ if power >= 0:
+ return Rational(a.numerator ** power,
+ a.denominator ** power)
+ else:
+ return Rational(a.denominator ** -power,
+ a.numerator ** -power)
+ else:
+ # A fractional power will generally produce an
+ # irrational number.
+ return float(a) ** float(b)
+ else:
+ return float(a) ** b
+
+ def __rpow__(b, a):
+ """a ** b"""
+ if b.denominator == 1 and b.numerator >= 0:
+ # If a is an int, keep it that way if possible.
+ return a ** b.numerator
+
+ if isinstance(a, RationalAbc):
+ return Rational(a.numerator, a.denominator) ** b
+
+ if b.denominator == 1:
+ return a ** b.numerator
+
+ return a ** float(b)
+
+ def __pos__(a):
+ """+a: Coerces a subclass instance to Rational"""
+ return Rational(a.numerator, a.denominator)
+
+ def __neg__(a):
+ """-a"""
+ return Rational(-a.numerator, a.denominator)
+
+ def __abs__(a):
+ """abs(a)"""
+ return Rational(abs(a.numerator), a.denominator)
+
+ def __trunc__(a):
+ """trunc(a)"""
+ if a.numerator < 0:
+ return -(-a.numerator // a.denominator)
+ else:
+ return a.numerator // a.denominator
+
+ def __floor__(a):
+ """Will be math.floor(a) in 3.0."""
+ return a.numerator // a.denominator
+
+ def __ceil__(a):
+ """Will be math.ceil(a) in 3.0."""
+ # The negations cleverly convince floordiv to return the ceiling.
+ return -(-a.numerator // a.denominator)
+
+ def __round__(self, ndigits=None):
+ """Will be round(self, ndigits) in 3.0.
+
+ Rounds half toward even.
+ """
+ if ndigits is None:
+ floor, remainder = divmod(self.numerator, self.denominator)
+ if remainder * 2 < self.denominator:
+ return floor
+ elif remainder * 2 > self.denominator:
+ return floor + 1
+ # Deal with the half case:
+ elif floor % 2 == 0:
+ return floor
+ else:
+ return floor + 1
+ shift = 10**abs(ndigits)
+ # See _operator_fallbacks.forward to check that the results of
+ # these operations will always be Rational and therefore have
+ # __round__().
+ if ndigits > 0:
+ return Rational((self * shift).__round__(), shift)
+ else:
+ return Rational((self / shift).__round__() * shift)
+
+ def __hash__(self):
+ """hash(self)
+
+ Tricky because values that are exactly representable as a
+ float must have the same hash as that float.
+
+ """
+ if self.denominator == 1:
+ # Get integers right.
+ return hash(self.numerator)
+ # Expensive check, but definitely correct.
+ if self == float(self):
+ return hash(float(self))
+ else:
+ # Use tuple's hash to avoid a high collision rate on
+ # simple fractions.
+ return hash((self.numerator, self.denominator))
+
+ def __eq__(a, b):
+ """a == b"""
+ if isinstance(b, RationalAbc):
+ return (a.numerator == b.numerator and
+ a.denominator == b.denominator)
+ if isinstance(b, numbers.Complex) and b.imag == 0:
+ b = b.real
+ if isinstance(b, float):
+ return a == a.from_float(b)
+ else:
+ # XXX: If b.__eq__ is implemented like this method, it may
+ # give the wrong answer after float(a) changes a's
+ # value. Better ways of doing this are welcome.
+ return float(a) == b
+
+ def _subtractAndCompareToZero(a, b, op):
+ """Helper function for comparison operators.
+
+ Subtracts b from a, exactly if possible, and compares the
+ result with 0 using op, in such a way that the comparison
+ won't recurse. If the difference raises a TypeError, returns
+ NotImplemented instead.
+
+ """
+ if isinstance(b, numbers.Complex) and b.imag == 0:
+ b = b.real
+ if isinstance(b, float):
+ b = a.from_float(b)
+ try:
+ # XXX: If b <: Real but not <: RationalAbc, this is likely
+ # to fall back to a float. If the actual values differ by
+ # less than MIN_FLOAT, this could falsely call them equal,
+ # which would make <= inconsistent with ==. Better ways of
+ # doing this are welcome.
+ diff = a - b
+ except TypeError:
+ return NotImplemented
+ if isinstance(diff, RationalAbc):
+ return op(diff.numerator, 0)
+ return op(diff, 0)
+
+ def __lt__(a, b):
+ """a < b"""
+ return a._subtractAndCompareToZero(b, operator.lt)
+
+ def __gt__(a, b):
+ """a > b"""
+ return a._subtractAndCompareToZero(b, operator.gt)
+
+ def __le__(a, b):
+ """a <= b"""
+ return a._subtractAndCompareToZero(b, operator.le)
+
+ def __ge__(a, b):
+ """a >= b"""
+ return a._subtractAndCompareToZero(b, operator.ge)
+
+ def __bool__(a):
+ """a != 0"""
+ return a.numerator != 0
--- /dev/null
+"""Tests for Lib/rational.py."""
+
+from decimal import Decimal
+from test.test_support import run_unittest, verbose
+import math
+import operator
+import rational
+import unittest
+R = rational.Rational
+
+def _components(r):
+ return (r.numerator, r.denominator)
+
+class RationalTest(unittest.TestCase):
+
+ def assertTypedEquals(self, expected, actual):
+ """Asserts that both the types and values are the same."""
+ self.assertEquals(type(expected), type(actual))
+ self.assertEquals(expected, actual)
+
+ def assertRaisesMessage(self, exc_type, message,
+ callable, *args, **kwargs):
+ """Asserts that callable(*args, **kwargs) raises exc_type(message)."""
+ try:
+ callable(*args, **kwargs)
+ except exc_type as e:
+ self.assertEquals(message, str(e))
+ else:
+ self.fail("%s not raised" % exc_type.__name__)
+
+ def testInit(self):
+ self.assertEquals((0, 1), _components(R()))
+ self.assertEquals((7, 1), _components(R(7)))
+ self.assertEquals((7, 3), _components(R(R(7, 3))))
+
+ self.assertEquals((-1, 1), _components(R(-1, 1)))
+ self.assertEquals((-1, 1), _components(R(1, -1)))
+ self.assertEquals((1, 1), _components(R(-2, -2)))
+ self.assertEquals((1, 2), _components(R(5, 10)))
+ self.assertEquals((7, 15), _components(R(7, 15)))
+ self.assertEquals((10**23, 1), _components(R(10**23)))
+
+ self.assertRaisesMessage(ZeroDivisionError, "Rational(12, 0)",
+ R, 12, 0)
+ self.assertRaises(TypeError, R, 1.5)
+ self.assertRaises(TypeError, R, 1.5 + 3j)
+
+ def testFromFloat(self):
+ self.assertRaisesMessage(
+ TypeError, "Rational.from_float() only takes floats, not 3 (int)",
+ R.from_float, 3)
+
+ self.assertEquals((0, 1), _components(R.from_float(-0.0)))
+ self.assertEquals((10, 1), _components(R.from_float(10.0)))
+ self.assertEquals((-5, 2), _components(R.from_float(-2.5)))
+ self.assertEquals((99999999999999991611392, 1),
+ _components(R.from_float(1e23)))
+ self.assertEquals(float(10**23), float(R.from_float(1e23)))
+ self.assertEquals((3602879701896397, 1125899906842624),
+ _components(R.from_float(3.2)))
+ self.assertEquals(3.2, float(R.from_float(3.2)))
+
+ inf = 1e1000
+ nan = inf - inf
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert inf to Rational.",
+ R.from_float, inf)
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert -inf to Rational.",
+ R.from_float, -inf)
+ self.assertRaisesMessage(
+ TypeError, "Cannot convert nan to Rational.",
+ R.from_float, nan)
+
+ def testConversions(self):
+ self.assertTypedEquals(-1, trunc(R(-11, 10)))
+ self.assertTypedEquals(-2, R(-11, 10).__floor__())
+ self.assertTypedEquals(-1, R(-11, 10).__ceil__())
+ self.assertTypedEquals(-1, R(-10, 10).__ceil__())
+
+ self.assertTypedEquals(0, R(-1, 10).__round__())
+ self.assertTypedEquals(0, R(-5, 10).__round__())
+ self.assertTypedEquals(-2, R(-15, 10).__round__())
+ self.assertTypedEquals(-1, R(-7, 10).__round__())
+
+ self.assertEquals(False, bool(R(0, 1)))
+ self.assertEquals(True, bool(R(3, 2)))
+ self.assertTypedEquals(0.1, float(R(1, 10)))
+
+ # Check that __float__ isn't implemented by converting the
+ # numerator and denominator to float before dividing.
+ self.assertRaises(OverflowError, float, int('2'*400+'7'))
+ self.assertAlmostEquals(2.0/3,
+ float(R(int('2'*400+'7'), int('3'*400+'1'))))
+
+ self.assertTypedEquals(0.1+0j, complex(R(1,10)))
+
+ def testRound(self):
+ self.assertTypedEquals(R(-200), R(-150).__round__(-2))
+ self.assertTypedEquals(R(-200), R(-250).__round__(-2))
+ self.assertTypedEquals(R(30), R(26).__round__(-1))
+ self.assertTypedEquals(R(-2, 10), R(-15, 100).__round__(1))
+ self.assertTypedEquals(R(-2, 10), R(-25, 100).__round__(1))
+
+
+ def testArithmetic(self):
+ self.assertEquals(R(1, 2), R(1, 10) + R(2, 5))
+ self.assertEquals(R(-3, 10), R(1, 10) - R(2, 5))
+ self.assertEquals(R(1, 25), R(1, 10) * R(2, 5))
+ self.assertEquals(R(1, 4), R(1, 10) / R(2, 5))
+ self.assertTypedEquals(2, R(9, 10) // R(2, 5))
+ self.assertTypedEquals(10**23, R(10**23, 1) // R(1))
+ self.assertEquals(R(2, 3), R(-7, 3) % R(3, 2))
+ self.assertEquals(R(8, 27), R(2, 3) ** R(3))
+ self.assertEquals(R(27, 8), R(2, 3) ** R(-3))
+ self.assertTypedEquals(2.0, R(4) ** R(1, 2))
+ z = pow(R(-1), R(1, 2))
+ self.assertAlmostEquals(z.real, 0)
+ self.assertEquals(z.imag, 1)
+
+ def testMixedArithmetic(self):
+ self.assertTypedEquals(R(11, 10), R(1, 10) + 1)
+ self.assertTypedEquals(1.1, R(1, 10) + 1.0)
+ self.assertTypedEquals(1.1 + 0j, R(1, 10) + (1.0 + 0j))
+ self.assertTypedEquals(R(11, 10), 1 + R(1, 10))
+ self.assertTypedEquals(1.1, 1.0 + R(1, 10))
+ self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + R(1, 10))
+
+ self.assertTypedEquals(R(-9, 10), R(1, 10) - 1)
+ self.assertTypedEquals(-0.9, R(1, 10) - 1.0)
+ self.assertTypedEquals(-0.9 + 0j, R(1, 10) - (1.0 + 0j))
+ self.assertTypedEquals(R(9, 10), 1 - R(1, 10))
+ self.assertTypedEquals(0.9, 1.0 - R(1, 10))
+ self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - R(1, 10))
+
+ self.assertTypedEquals(R(1, 10), R(1, 10) * 1)
+ self.assertTypedEquals(0.1, R(1, 10) * 1.0)
+ self.assertTypedEquals(0.1 + 0j, R(1, 10) * (1.0 + 0j))
+ self.assertTypedEquals(R(1, 10), 1 * R(1, 10))
+ self.assertTypedEquals(0.1, 1.0 * R(1, 10))
+ self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * R(1, 10))
+
+ self.assertTypedEquals(R(1, 10), R(1, 10) / 1)
+ self.assertTypedEquals(0.1, R(1, 10) / 1.0)
+ self.assertTypedEquals(0.1 + 0j, R(1, 10) / (1.0 + 0j))
+ self.assertTypedEquals(R(10, 1), 1 / R(1, 10))
+ self.assertTypedEquals(10.0, 1.0 / R(1, 10))
+ self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / R(1, 10))
+
+ self.assertTypedEquals(0, R(1, 10) // 1)
+ # XXX Jeffrey: why does this fail?
+ ##self.assertTypedEquals(0.0, R(1, 10) // 1.0)
+ self.assertTypedEquals(10, 1 // R(1, 10))
+ self.assertTypedEquals(10**23, 10**22 // R(1, 10))
+ # XXX Jeffrey: why does this fail?
+ ##self.assertTypedEquals(10.0, 1.0 // R(1, 10))
+
+ self.assertTypedEquals(R(1, 10), R(1, 10) % 1)
+ self.assertTypedEquals(0.1, R(1, 10) % 1.0)
+ self.assertTypedEquals(R(0, 1), 1 % R(1, 10))
+ self.assertTypedEquals(0.0, 1.0 % R(1, 10))
+
+ # No need for divmod since we don't override it.
+
+ # ** has more interesting conversion rules.
+ self.assertTypedEquals(R(100, 1), R(1, 10) ** -2)
+ self.assertTypedEquals(R(100, 1), R(10, 1) ** 2)
+ self.assertTypedEquals(0.1, R(1, 10) ** 1.0)
+ self.assertTypedEquals(0.1 + 0j, R(1, 10) ** (1.0 + 0j))
+ self.assertTypedEquals(4 , 2 ** R(2, 1))
+ z = pow(-1, R(1, 2))
+ self.assertAlmostEquals(0, z.real)
+ self.assertEquals(1, z.imag)
+ self.assertTypedEquals(R(1, 4) , 2 ** R(-2, 1))
+ self.assertTypedEquals(2.0 , 4 ** R(1, 2))
+ self.assertTypedEquals(0.25, 2.0 ** R(-2, 1))
+ self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** R(1, 10))
+
+ def testMixingWithDecimal(self):
+ """Decimal refuses mixed comparisons."""
+ self.assertRaisesMessage(
+ TypeError,
+ "unsupported operand type(s) for +: 'Rational' and 'Decimal'",
+ operator.add, R(3,11), Decimal('3.1415926'))
+ self.assertNotEquals(R(5, 2), Decimal('2.5'))
+
+ def testComparisons(self):
+ self.assertTrue(R(1, 2) < R(2, 3))
+ self.assertFalse(R(1, 2) < R(1, 2))
+ self.assertTrue(R(1, 2) <= R(2, 3))
+ self.assertTrue(R(1, 2) <= R(1, 2))
+ self.assertFalse(R(2, 3) <= R(1, 2))
+ self.assertTrue(R(1, 2) == R(1, 2))
+ self.assertFalse(R(1, 2) == R(1, 3))
+
+ def testMixedLess(self):
+ self.assertTrue(2 < R(5, 2))
+ self.assertFalse(2 < R(4, 2))
+ self.assertTrue(R(5, 2) < 3)
+ self.assertFalse(R(4, 2) < 2)
+
+ self.assertTrue(R(1, 2) < 0.6)
+ self.assertFalse(R(1, 2) < 0.4)
+ self.assertTrue(0.4 < R(1, 2))
+ self.assertFalse(0.5 < R(1, 2))
+
+ def testMixedLessEqual(self):
+ self.assertTrue(0.5 <= R(1, 2))
+ self.assertFalse(0.6 <= R(1, 2))
+ self.assertTrue(R(1, 2) <= 0.5)
+ self.assertFalse(R(1, 2) <= 0.4)
+ self.assertTrue(2 <= R(4, 2))
+ self.assertFalse(2 <= R(3, 2))
+ self.assertTrue(R(4, 2) <= 2)
+ self.assertFalse(R(5, 2) <= 2)
+
+ def testBigFloatComparisons(self):
+ # Because 10**23 can't be represented exactly as a float:
+ self.assertFalse(R(10**23) == float(10**23))
+ # The first test demonstrates why these are important.
+ self.assertFalse(1e23 < float(R(trunc(1e23) + 1)))
+ self.assertTrue(1e23 < R(trunc(1e23) + 1))
+ self.assertFalse(1e23 <= R(trunc(1e23) - 1))
+ self.assertTrue(1e23 > R(trunc(1e23) - 1))
+ self.assertFalse(1e23 >= R(trunc(1e23) + 1))
+
+ def testBigComplexComparisons(self):
+ self.assertFalse(R(10**23) == complex(10**23))
+ self.assertTrue(R(10**23) > complex(10**23))
+ self.assertFalse(R(10**23) <= complex(10**23))
+
+ def testMixedEqual(self):
+ self.assertTrue(0.5 == R(1, 2))
+ self.assertFalse(0.6 == R(1, 2))
+ self.assertTrue(R(1, 2) == 0.5)
+ self.assertFalse(R(1, 2) == 0.4)
+ self.assertTrue(2 == R(4, 2))
+ self.assertFalse(2 == R(3, 2))
+ self.assertTrue(R(4, 2) == 2)
+ self.assertFalse(R(5, 2) == 2)
+
+ def testStringification(self):
+ self.assertEquals("rational.Rational(7,3)", repr(R(7, 3)))
+ self.assertEquals("(7/3)", str(R(7, 3)))
+ self.assertEquals("7", str(R(7, 1)))
+
+ def testHash(self):
+ self.assertEquals(hash(2.5), hash(R(5, 2)))
+ self.assertEquals(hash(10**50), hash(R(10**50)))
+ self.assertNotEquals(hash(float(10**23)), hash(R(10**23)))
+
+ def testApproximatePi(self):
+ # Algorithm borrowed from
+ # http://docs.python.org/lib/decimal-recipes.html
+ three = R(3)
+ lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
+ while abs(s - lasts) > R(1, 10**9):
+ lasts = s
+ n, na = n+na, na+8
+ d, da = d+da, da+32
+ t = (t * n) / d
+ s += t
+ self.assertAlmostEquals(math.pi, s)
+
+ def testApproximateCos1(self):
+ # Algorithm borrowed from
+ # http://docs.python.org/lib/decimal-recipes.html
+ x = R(1)
+ i, lasts, s, fact, num, sign = 0, 0, R(1), 1, 1, 1
+ while abs(s - lasts) > R(1, 10**9):
+ lasts = s
+ i += 2
+ fact *= i * (i-1)
+ num *= x * x
+ sign *= -1
+ s += num / fact * sign
+ self.assertAlmostEquals(math.cos(1), s)
+
+def test_main():
+ run_unittest(RationalTest)
+
+if __name__ == '__main__':
+ test_main()
self.failUnless(sys.flags)
attrs = ("debug", "division_warning",
"inspect", "interactive", "optimize", "dont_write_bytecode",
- "no_site", "ingnore_environment", "tabcheck", "verbose")
+ "no_site", "ignore_environment", "tabcheck", "verbose")
for attr in attrs:
self.assert_(hasattr(sys.flags, attr), attr)
self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"),
file=file)
for zinfo in self.filelist:
- date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time
+ date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size),
file=file)
data = data.encode("utf-8")
if not isinstance(zinfo_or_arcname, ZipInfo):
zinfo = ZipInfo(filename=zinfo_or_arcname,
- date_time=time.localtime(time.time()))
+ date_time=time.localtime(time.time())[:6])
zinfo.compress_type = self.compression
else:
zinfo = zinfo_or_arcname
else
echo "$as_me: WARNING: no configuration information is in $ac_dir" >&2
fi
- cd $ac_popdir
+ cd "$ac_popdir"
done
fi
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cris-*-*) TARGET=LIBFFI_CRIS; TARGETDIR=cris;;
s390-*-linux-*) TARGET=S390; TARGETDIR=s390;;
s390x-*-linux-*) TARGET=S390; TARGETDIR=s390;;
-x86_64-*-linux* | x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu) TARGET=X86_64; TARGETDIR=x86;;
+amd64-*-freebsd* | x86_64-*-linux* | x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu) TARGET=X86_64; TARGETDIR=x86;;
sh-*-linux* | sh[34]*-*-linux*) TARGET=SH; TARGETDIR=sh;;
sh-*-rtems*) TARGET=SH; TARGETDIR=sh;;
sh64-*-linux* | sh5*-*-linux*) TARGET=SH64; TARGETDIR=sh64;;
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
esac
else
if test "$cross_compiling" = yes; then
- { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&5
-echo "$as_me: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&2;}
+ { { echo "$as_me:$LINENO: error: internal error: not reached in cross-compile" >&5
+echo "$as_me: error: internal error: not reached in cross-compile" >&2;}
{ (exit 1); exit 1; }; }
else
cat >conftest.$ac_ext <<_ACEOF
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
esac
else
if test "$cross_compiling" = yes; then
- { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&5
-echo "$as_me: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&2;}
+ { { echo "$as_me:$LINENO: error: internal error: not reached in cross-compile" >&5
+echo "$as_me: error: internal error: not reached in cross-compile" >&2;}
{ (exit 1); exit 1; }; }
else
cat >conftest.$ac_ext <<_ACEOF
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
esac
else
if test "$cross_compiling" = yes; then
- { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&5
-echo "$as_me: error: cannot run test program while cross compiling
-See \`config.log' for more details." >&2;}
+ { { echo "$as_me:$LINENO: error: internal error: not reached in cross-compile" >&5
+echo "$as_me: error: internal error: not reached in cross-compile" >&2;}
{ (exit 1); exit 1; }; }
else
cat >conftest.$ac_ext <<_ACEOF
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
cat conftest.err >&5
echo "$as_me:$LINENO: \$? = $ac_status" >&5
(exit $ac_status); } &&
- { ac_try='test -z "$ac_c_werror_flag"
- || test ! -s conftest.err'
+ { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err'
{ (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
(eval $ac_try) 2>&5
ac_status=$?
- if test x"$ac_file" != x-; then
- { echo "$as_me:$LINENO: creating $ac_file" >&5
-echo "$as_me: creating $ac_file" >&6;}
- rm -f "$ac_file"
- fi
# Let's still pretend it is `configure' which instantiates (i.e., don't
# use $as_me), people would be surprised to read:
# /* config.h. Generated by config.status. */
fi;;
esac
done` || { (exit 1); exit 1; }
+
+ if test x"$ac_file" != x-; then
+ { echo "$as_me:$LINENO: creating $ac_file" >&5
+echo "$as_me: creating $ac_file" >&6;}
+ rm -f "$ac_file"
+ fi
_ACEOF
cat >>$CONFIG_STATUS <<_ACEOF
sed "$ac_vpsub
cris-*-*) TARGET=LIBFFI_CRIS; TARGETDIR=cris;;
s390-*-linux-*) TARGET=S390; TARGETDIR=s390;;
s390x-*-linux-*) TARGET=S390; TARGETDIR=s390;;
-x86_64-*-linux* | x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu) TARGET=X86_64; TARGETDIR=x86;;
+amd64-*-freebsd* | x86_64-*-linux* | x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu) TARGET=X86_64; TARGETDIR=x86;;
sh-*-linux* | sh[[34]]*-*-linux*) TARGET=SH; TARGETDIR=sh;;
sh-*-rtems*) TARGET=SH; TARGETDIR=sh;;
sh64-*-linux* | sh5*-*-linux*) TARGET=SH64; TARGETDIR=sh64;;
{"dont_write_bytecode", "-B"},
/* {"no_user_site", "-s"}, */
{"no_site", "-S"},
- {"ingnore_environment", "-E"},
+ {"ignore_environment", "-E"},
{"tabcheck", "-t or -tt"},
{"verbose", "-v"},
#ifdef RISCOS
- {"ricos_wimp", "???"},
+ {"riscos_wimp", "???"},
#endif
/* {"unbuffered", "-u"}, */
/* {"skip_first", "-x"}, */