本文整理汇总了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))
示例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
示例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)
示例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