"fractions" — 有理数
********************

バージョン 2.6 で追加.

**ソースコード:** Lib/fractions.py

======================================================================

"fractions" モジュールは有理数計算のサポートを提供します。

Fraction インスタンスは一対の整数、他の有理数、または文字列から生成さ
れます。

class fractions.Fraction(numerator=0, denominator=1)
class fractions.Fraction(other_fraction)
class fractions.Fraction(float)
class fractions.Fraction(decimal)
class fractions.Fraction(string)

   最初のバージョンは *numerator* と *denominator* が
   "numbers.Rational" のインスタンスであることを要求し、
   "numerator/denominator" の値を持つ新しい "Fraction" インスタンスを
   返します。 *denominator* が "0" ならば、 "ZeroDivisionError" を送出
   します。二番目のバージョンは *other_fraction* が "numbers.Rational"
   のインスタンスであることを要求し、同じ値を持つ新しい "Fraction" イ
   ンスタンスを返します。その次の二つのバージョンは、 "float" と
   "decimal.Decimal" インスタンスを受け付け、それとちょうど同じ値を持
   つ "Fraction" インスタンスを返します。なお、二進浮動小数点数にお決
   まりの問題 (浮動小数点演算、その問題と制限 参照) のため、
   "Fraction(1.1)" の引数は 11/10 と正確に等しいとは言えないので、
   "Fraction(1.1)" は予期した通りの "Fraction(11, 10)" を返 *しません*
   。(ただし、以下の "limit_denominator()" メソッドのドキュメントを参
   照してください。)最後のバージョンは、文字列またはユニコードのインス
   タンスを渡されることを想定します。このインスタンスは、通常、次のよ
   うな形式です:

      [sign] numerator ['/' denominator]

   ここで、オプションの "sign" は 『+』 か 『-『 のどちらかであり、
   "numerator" および (存在する場合) "denominator" は十進数の数字の文
   字列です。さらに、 "float" コンストラクタで受け付けられる有限の値を
   表す文字列は、"Fraction" コンストラクタでも受け付けられます。どちら
   の形式でも、入力される文字列は前後に空白があって構いません。以下に
   、いくつかの例を示します:

      >>> from fractions import Fraction
      >>> Fraction(16, -10)
      Fraction(-8, 5)
      >>> Fraction(123)
      Fraction(123, 1)
      >>> Fraction()
      Fraction(0, 1)
      >>> Fraction('3/7')
      Fraction(3, 7)
      >>> Fraction(' -3/7 ')
      Fraction(-3, 7)
      >>> Fraction('1.414213 \t\n')
      Fraction(1414213, 1000000)
      >>> Fraction('-.125')
      Fraction(-1, 8)
      >>> Fraction('7e-6')
      Fraction(7, 1000000)
      >>> Fraction(2.25)
      Fraction(9, 4)
      >>> Fraction(1.1)
      Fraction(2476979795053773, 2251799813685248)
      >>> from decimal import Decimal
      >>> Fraction(Decimal('1.1'))
      Fraction(11, 10)

   The "Fraction" class inherits from the abstract base class
   "numbers.Rational", and implements all of the methods and
   operations from that class.  "Fraction" instances are hashable, and
   should be treated as immutable.  In addition, "Fraction" has the
   following methods:

   バージョン 2.7 で変更: "Fraction" のコンストラクタが "float" および
   "decimal.Decimal" インスタンスを受け付けるようになりました。

   from_float(flt)

      このクラスメソッドは "float" である *flt* の正確な値を表す
      "Fraction" を構築します。 "Fraction.from_float(0.3)" と
      "Fraction(3, 10)" の値は同じでないことに注意してください 。

      注釈: From Python 2.7 onwards, you can also construct a
        "Fraction" instance directly from a "float".

   from_decimal(dec)

      This class method constructs a "Fraction" representing the exact
      value of *dec*, which must be a "decimal.Decimal".

      注釈: From Python 2.7 onwards, you can also construct a
        "Fraction" instance directly from a "decimal.Decimal"
        instance.

   limit_denominator(max_denominator=1000000)

      分母が高々 max_denominator である、 "self" に最も近い "Fraction"
      を見付けて返します。このメソッドは与えられた浮動小数点数の有理数
      近似を見つけるのに役立ちます:

      >>> from fractions import Fraction
      >>> Fraction('3.1415926535897932').limit_denominator(1000)
      Fraction(355, 113)

      あるいは float で表された有理数を元に戻すのにも使えます:

      >>> from math import pi, cos
      >>> Fraction(cos(pi/3))
      Fraction(4503599627370497, 9007199254740992)
      >>> Fraction(cos(pi/3)).limit_denominator()
      Fraction(1, 2)
      >>> Fraction(1.1).limit_denominator()
      Fraction(11, 10)

fractions.gcd(a, b)

   整数 *a* と *b* の最大公約数を返します。*a* も *b* もゼロでないとす
   ると、"gcd(a, b)" の絶対値は *a* と *b* の両方を割り切る最も大きな
   整数です。"gcd(a, b)" は *b* がゼロでなければ *b* と同じ符号になり
   ます。そうでなければ *a* の符号を取ります。"gcd(0, 0)" は "0" を返
   します。

参考:

  "numbers" モジュール
     数値の塔を作り上げる抽象基底クラス。
