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


Python Fraction.from_float方法代码示例

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


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

示例1: test_binary_floats

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_float [as 别名]
def test_binary_floats(self):
        # check that floats hash equal to corresponding Fractions and Decimals

        # floats that are distinct but numerically equal should hash the same
        self.check_equal_hash(0.0, -0.0)

        # zeros
        self.check_equal_hash(0.0, D(0))
        self.check_equal_hash(-0.0, D(0))
        self.check_equal_hash(-0.0, D('-0.0'))
        self.check_equal_hash(0.0, F(0))

        # infinities and nans
        self.check_equal_hash(float('inf'), D('inf'))
        self.check_equal_hash(float('-inf'), D('-inf'))

        for _ in range(1000):
            x = random.random() * math.exp(random.random()*200.0 - 100.0)
            self.check_equal_hash(x, D.from_float(x))
            self.check_equal_hash(x, F.from_float(x)) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:22,代码来源:test_numeric_tower.py

示例2: rationalize

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_float [as 别名]
def rationalize(flt: float, denominators: Set[int] = None) -> Fraction:
    """Convert a floating point number to a Fraction with a small
    denominator.

    Args:
        flt:            A floating point number
        denominators:   Collection of standard denominators. Default is
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 16, 32, 64, 128, 256, 512,
            1024, 2048, 4096, 8192

    Raises:
        ValueError:     If cannot rationalize float
    """
    if denominators is None:
        denominators = _DENOMINATORS
    frac = Fraction.from_float(flt).limit_denominator()
    if frac.denominator not in denominators:
        raise ValueError('Cannot rationalize')
    return frac 
开发者ID:rigetti,项目名称:quantumflow,代码行数:21,代码来源:utils.py

示例3: _calcNumeratorDenominator

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_float [as 别名]
def _calcNumeratorDenominator(self, scalingFactor, forceFloat = False):

        if forceFloat:
           (numerator, denominator) = (float(scalingFactor), 1)
        else:
            f=Fraction.from_float(scalingFactor)
            if f.denominator > 10000: #use the float version in case its not a rational number
                (numerator, denominator) = (float(scalingFactor), 1)
            else:
                (numerator, denominator) = (f.numerator, f.denominator)
        return (numerator, denominator) 
开发者ID:cogu,项目名称:autosar,代码行数:13,代码来源:package.py

示例4: _msec_to_numden

# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import from_float [as 别名]
def _msec_to_numden(delay):
    """
    delay is the time delay in milliseconds.

    Return value is the tuple (delay_num, delay_den) representing
    the delay in seconds as the fraction delay_num/delay_den.
    Each value in the tuple is an integer less than 65536.
    """
    if delay == 0:
        return (0, 1)
    # Convert delay to seconds.
    delay_sec = delay/1000.0
    if delay_sec > 1:
        f = _Fraction.from_float(1.0/delay_sec).limit_denominator(65535)
        num = f.denominator
        den = f.numerator
    else:
        f = _Fraction.from_float(delay_sec).limit_denominator(65535)
        num = f.numerator
        den = f.denominator
    if (num, den) == (1, 0):
        raise ValueError("delay=%r is too large to convert to "
                         "delay_num/delay_den" % (delay,))
    if (num, den) == (0, 1):
        raise ValueError("delay=%r is too small to convert to "
                         "delay_num/delay_den" % (delay,))
    return num, den 
开发者ID:WarrenWeckesser,项目名称:numpngw,代码行数:29,代码来源:numpngw.py


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