fractions
— 有理数
¶
源代码: Lib/fractions.py
The
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 )
-
第 1 个版本要求 numerator and denominator 是实例化的
numbers.Rational并返回新的Fraction实例采用值numerator/denominator。若 denominator is0,引发ZeroDivisionError。第 2 个版本要求 other_fraction 是实例化的numbers.Rational并返回Fractioninstance with the same value. The next two versions accept either afloat或decimal.Decimalinstance, and return aFractioninstance with exactly the same value. Note that due to the usual issues with binary floating point (see 浮点算术:问题和局限性 ), the argument toFraction(1.1)is not exactly equal to 11/10, and soFraction(1.1)does not returnFraction(11, 10)as one might expect. (But see the documentation for thelimit_denominator()method below.) The last version of the constructor expects a string or unicode instance. The usual form for this instance is:[sign] numerator ['/' denominator]