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


Python Fraction.from_decimal方法代码示例

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


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

示例1: exact_ratio

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def exact_ratio(x):
    """Convert Real number x exactly to (numerator, denominator) pair.

    x is expected to be an int, Fraction, Decimal or float.
    """
    try:
        try:
            # int, Fraction
            return x.numerator, x.denominator
        except AttributeError:
            # float
            try:
                return x.as_integer_ratio()
            except AttributeError:
                # Decimal
                try:
                    return decimal_to_ratio(x)
                except AttributeError:
                    msg = "can't convert type '{}' to numerator/denominator"
                    raise TypeError(msg.format(type(x).__name__))
    except (OverflowError, ValueError):
        # INF or NAN
        return (x, None)


# FIXME This is faster than Fraction.from_decimal, but still too slow. 
开发者ID:avalente,项目名称:appmetrics,代码行数:28,代码来源:statistics.py

示例2: _exact_ratio

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def _exact_ratio(x):
    """Convert Real number x exactly to (numerator, denominator) pair.

    >>> _exact_ratio(0.25)
    (1, 4)

    x is expected to be an int, Fraction, Decimal or float.
    """
    try:
        try:
            # int, Fraction
            return (x.numerator, x.denominator)
        except AttributeError:
            # float
            try:
                return x.as_integer_ratio()
            except AttributeError:
                # Decimal
                try:
                    return _decimal_to_ratio(x)
                except AttributeError:
                    msg = "can't convert type '{}' to numerator/denominator"
                    raise TypeError(msg.format(type(x).__name__)) from None
    except (OverflowError, ValueError):
        # INF or NAN
        if __debug__:
            # Decimal signalling NANs cannot be converted to float :-(
            if isinstance(x, Decimal):
                assert not x.is_finite()
            else:
                assert not math.isfinite(x)
        return (x, None)


# FIXME This is faster than Fraction.from_decimal, but still too slow. 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:37,代码来源:statistics.py

示例3: _exact_ratio

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_decimal [as 别名]
def _exact_ratio(x):
    """Return Real number x to exact (numerator, denominator) pair.

    >>> _exact_ratio(0.25)
    (1, 4)

    x is expected to be an int, Fraction, Decimal or float.
    """
    try:
        # Optimise the common case of floats. We expect that the most often
        # used numeric type will be builtin floats, so try to make this as
        # fast as possible.
        if type(x) is float:
            return x.as_integer_ratio()
        try:
            # x may be an int, Fraction, or Integral ABC.
            return (x.numerator, x.denominator)
        except AttributeError:
            try:
                # x may be a float subclass.
                return x.as_integer_ratio()
            except AttributeError:
                try:
                    # x may be a Decimal.
                    return _decimal_to_ratio(x)
                except AttributeError:
                    # Just give up?
                    pass
    except (OverflowError, ValueError):
        # float NAN or INF.
        assert not math.isfinite(x)
        return (x, None)
    msg = "can't convert type '{}' to numerator/denominator"
    raise TypeError(msg.format(type(x).__name__))


# FIXME This is faster than Fraction.from_decimal, but still too slow. 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:39,代码来源:statistics.py


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