当前位置: 首页>>代码示例>>Python>>正文


Python numbers.Rational方法代码示例

本文整理汇总了Python中numbers.Rational方法的典型用法代码示例。如果您正苦于以下问题:Python numbers.Rational方法的具体用法?Python numbers.Rational怎么用?Python numbers.Rational使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在numbers的用法示例。


在下文中一共展示了numbers.Rational方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __pow__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
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, numbers.Rational):
            if b.denominator == 1:
                power = b.numerator
                if power >= 0:
                    return Fraction(a._numerator ** power,
                                    a._denominator ** power)
                else:
                    return Fraction(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 
开发者ID:war-and-code,项目名称:jawfish,代码行数:25,代码来源:fractions.py

示例2: __eq__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def __eq__(a, b):
        """a == b"""
        if isinstance(b, numbers.Rational):
            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):
            if math.isnan(b) or math.isinf(b):
                # comparisons with an infinity or nan should behave in
                # the same way for any finite a, so treat a as zero.
                return 0.0 == b
            else:
                return a == a.from_float(b)
        else:
            # Since a doesn't know how to compare with b, let's give b
            # a chance to compare itself with a.
            return NotImplemented 
开发者ID:war-and-code,项目名称:jawfish,代码行数:20,代码来源:fractions.py

示例3: _richcmp

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def _richcmp(self, other, op):
        """Helper for comparison operators, for internal use only.

        Implement comparison between a Rational instance `self`, and
        either another Rational instance or a float `other`.  If
        `other` is not a Rational instance or a float, return
        NotImplemented. `op` should be one of the six standard
        comparison operators.

        """
        # convert other to a Rational instance where reasonable.
        if isinstance(other, numbers.Rational):
            return op(self._numerator * other.denominator,
                      self._denominator * other.numerator)
        if isinstance(other, float):
            if math.isnan(other) or math.isinf(other):
                return op(0.0, other)
            else:
                return op(self, self.from_float(other))
        else:
            return NotImplemented 
开发者ID:war-and-code,项目名称:jawfish,代码行数:23,代码来源:fractions.py

示例4: __pow__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
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, Rational):
            if b.denominator == 1:
                power = b.numerator
                if power >= 0:
                    return Fraction(a._numerator ** power,
                                    a._denominator ** power)
                else:
                    return Fraction(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 
开发者ID:glmcdona,项目名称:meddle,代码行数:25,代码来源:fractions.py

示例5: __eq__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def __eq__(a, b):
        """a == b"""
        if isinstance(b, Rational):
            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):
            if math.isnan(b) or math.isinf(b):
                # comparisons with an infinity or nan should behave in
                # the same way for any finite a, so treat a as zero.
                return 0.0 == b
            else:
                return a == a.from_float(b)
        else:
            # Since a doesn't know how to compare with b, let's give b
            # a chance to compare itself with a.
            return NotImplemented 
开发者ID:glmcdona,项目名称:meddle,代码行数:20,代码来源:fractions.py

示例6: __pow__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
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, numbers.Rational):
            if b.denominator == 1:
                power = b.numerator
                if power >= 0:
                    return Fraction(a._numerator ** power,
                                    a._denominator ** power,
                                    _normalize=False)
                else:
                    return Fraction(a._denominator ** -power,
                                    a._numerator ** -power,
                                    _normalize=False)
            else:
                # A fractional power will generally produce an
                # irrational number.
                return float(a) ** float(b)
        else:
            return float(a) ** b 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:27,代码来源:fractions.py

示例7: __eq__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def __eq__(a, b):
        """a == b"""
        if type(b) is int:
            return a._numerator == b and a._denominator == 1
        if isinstance(b, numbers.Rational):
            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):
            if math.isnan(b) or math.isinf(b):
                # comparisons with an infinity or nan should behave in
                # the same way for any finite a, so treat a as zero.
                return 0.0 == b
            else:
                return a == a.from_float(b)
        else:
            # Since a doesn't know how to compare with b, let's give b
            # a chance to compare itself with a.
            return NotImplemented 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:22,代码来源:fractions.py

示例8: __rpow__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
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, numbers.Rational):
            return Fraction(a.numerator, a.denominator) ** b

        if b._denominator == 1:
            return a ** b._numerator

        return a ** float(b) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:15,代码来源:fractions.py

示例9: test_floats

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def test_floats(self):
        for t in sctypes['float']:
            assert_(isinstance(t(), numbers.Real),
                    "{0} is not instance of Real".format(t.__name__))
            assert_(issubclass(t, numbers.Real),
                    "{0} is not subclass of Real".format(t.__name__))
            assert_(not isinstance(t(), numbers.Rational),
                    "{0} is instance of Rational".format(t.__name__))
            assert_(not issubclass(t, numbers.Rational),
                    "{0} is subclass of Rational".format(t.__name__)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:12,代码来源:test_abc.py

示例10: __floordiv__

# 需要导入模块: import numbers [as 别名]
# 或者: from numbers import Rational [as 别名]
def __floordiv__(a, b):
        """a // b"""
        # Will be math.floor(a / b) in 3.0.
        div = a / b
        if isinstance(div, Rational):
            # 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) 
开发者ID:glmcdona,项目名称:meddle,代码行数:13,代码来源:fractions.py


注:本文中的numbers.Rational方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。