本文整理汇总了Python中fractions.Fraction.limit_denominator方法的典型用法代码示例。如果您正苦于以下问题:Python Fraction.limit_denominator方法的具体用法?Python Fraction.limit_denominator怎么用?Python Fraction.limit_denominator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fractions.Fraction
的用法示例。
在下文中一共展示了Fraction.limit_denominator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: solve
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def solve(n, m):
total_cases = nCr(n+m, n)
failed_cases = fail(n, m)
from fractions import Fraction
f = Fraction(total_cases - failed_cases, total_cases)
f.limit_denominator()
return float(f)
示例2: output_dynamics
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def output_dynamics(message):
i = struct.unpack("!dd", message)
acc_fr = Fraction(i[0])
acc_fr = acc_fr.limit_denominator(8)
brk_fr = Fraction(i[1])
brk_fr = brk_fr.limit_denominator(8)
return struct.pack("<hhhh", int(acc_fr.numerator), int(acc_fr.denominator),
int(brk_fr.numerator), int(brk_fr.denominator))
示例3: print_file
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def print_file(A):
file = open("matrix_show.txt", "a")
for i in range(len(A)):
for j in range(len(A[i])):
file.write("%6s " % Fraction.limit_denominator(A[i][j]))
file.write("\n")
file.write("\n\n")
示例4: __init__
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def __init__(self, infreq, outfreq):
self.clkin = Signal()
self.clkout = Signal()
ratio = Fraction(outfreq)/Fraction(infreq)
appr = ratio.limit_denominator(32)
m = appr.numerator
if m < 2 or m > 32:
raise OverflowError
d = appr.denominator
in_period = float(Fraction(1000000000)/Fraction(infreq))
self._inst = Instance("DCM_SP",
[("CLKFX", self.clkout)],
[("CLKIN", self.clkin),
("PSEN", BV(1)),
("RST", BV(1))],
[("CLKDV_DIVIDE", 2.0),
("CLKFX_DIVIDE", d),
("CLKFX_MULTIPLY", m),
("CLKIN_DIVIDE_BY_2", "FALSE"),
("CLKIN_PERIOD", in_period),
("CLKOUT_PHASE_SHIFT", "NONE"),
("CLK_FEEDBACK", "NONE"),
("DESKEW_ADJUST", "SYSTEM_SYNCHRONOUS"),
("DUTY_CYCLE_CORRECTION", "TRUE"),
("PHASE_SHIFT", 0),
("STARTUP_WAIT", "TRUE")]
)
示例5: rational
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def rational(arg, max_denominator=1000000):
# return nominator and denominator from float or two integers
try:
f = Fraction.from_float(arg)
except TypeError:
f = Fraction(arg[0], arg[1])
f = f.limit_denominator(max_denominator)
return f.numerator, f.denominator
示例6: to_frac
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def to_frac(v):
v = Fraction(v)
v = v.limit_denominator(1000)
v = str(v)
if "/" in v:
v = v.split("/")
v = "\\frac{%s}{%s}" % (v[0], v[1])
return v
示例7: fraction_cheating
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def fraction_cheating(n=3,d=7,limit = 1000000):
'''
find the reduced proper fraction directly
to the left of the given target
problem 71
'''
real = Fraction(n,d)
approx = Fraction(n/d)
while approx.limit_denominator(limit) == real: #Fraction(7720456504063707, 18014398509481984)
approx = Fraction(approx.numerator-1,approx.denominator)
return approx #Fraction(3595117, 8388608)???
示例8: data_averaging_coeffs
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def data_averaging_coeffs(fh1, fh2):
"""
return the time-and-frequency averaging parameters
which help get the data on the same grid(s)
"""
## read the two filestreams in chunks of equal time
sr = Fraction(fh1.dtsample.to(u.s).value * fh1.blocksize
/ fh1.recordsize)
sr /= Fraction(fh2.dtsample.to(u.s).value * fh2.blocksize
/ fh2.recordsize)
sr = sr.limit_denominator(1000)
nf1 = sr.denominator
nf2 = sr.numerator
## used for re-sizing hdf5 x-corr output
raw1_nrows = int(fh1.blocksize * nf1 / fh1.recordsize)
raw2_nrows = int(fh2.blocksize * nf2 / fh2.recordsize)
## time averaging params
Tavg = Fraction(raw1_nrows, raw2_nrows).limit_denominator(1000)
Tden = Tavg.denominator
Tnum = Tavg.numerator
## channel averaging params
f1info = (fh1.freq.max(), fh1.freq.min(), len(fh1.freq),
np.sign(np.diff(fh1.freq).mean()))
f2info = (fh2.freq.max(), fh2.freq.min(), len(fh2.freq),
np.sign(np.diff(fh2.freq).mean()))
f1keep = (fh1.freq > max(f1info[1], f2info[1])) \
& (fh1.freq < min(f1info[0], f2info[0]))
f2keep = (fh2.freq > max(f1info[1], f2info[1])) \
& (fh2.freq < min(f1info[0], f2info[0]))
Favg = abs(Fraction(np.diff(fh1.freq.value).mean()
/ np.diff(fh2.freq.value).mean()))
Favg = Favg.limit_denominator(200)
Fden = Favg.denominator
Fnum = Favg.numerator
# the frequencies we keep
freq1 = fh1.freq[f1keep]
freq1 = freq1.reshape(freq1.size / Fden, Fden).mean(axis=-1)
freq2 = fh2.freq[f2keep]
freq2 = freq2.reshape(freq2.size / Fnum, Fnum).mean(axis=-1)
# sort low freq to high freq
if f1info[3] < 0:
freq1 = freq1[::-1]
if f2info[3] < 0:
freq2 = freq2[::-1]
return ((nf1, nf2), (Tnum, Tden), (Fden, Fnum), (f1keep, f2keep),
(freq1, freq2), (raw1_nrows, raw2_nrows))
示例9: simplify
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def simplify(numer, denom, approximate=False):
"""
Simplifies the fraction, returning a tuple. If ``approximate`` is true,
then the resulting fraction is only an approximation of the input number
by limiting the denominator of the fraction to ``10``.
:param numer: The numerator
:param denom: The denominator
:param approximate: Whether to approximate or not.
"""
fract = Fraction(numer, denom)
if approximate:
tmp = fract.limit_denominator(10)
if fract != tmp:
LOG.debug("{0} reduced to {1}".format(fract, tmp))
fract = tmp
return fract.numerator, fract.denominator
示例10: fmt_number
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def fmt_number(p):
"""Format a number.
It will be printed as a fraction if the denominator isn't too big and as a
decimal otherwise.
"""
formatted = '{:n}'.format(p)
if not config.PRINT_FRACTIONS:
return formatted
fraction = Fraction(p)
nice = fraction.limit_denominator(128)
return (
str(nice) if (abs(fraction - nice) < constants.EPSILON and
nice.denominator in NICE_DENOMINATORS)
else formatted
)
示例11: Fraction
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author Greedywolf
# 分数运算
from fractions import Fraction
a = Fraction(5, 4)
print(a)
b = Fraction(7, 16)
print(b)
print(a + b)
print(a * b)
c = a * b
# 分子
tmp = c.numerator
print(tmp)
# 分母
tmp = c.denominator
print(tmp)
tmp = float(c)
print(tmp)
print(c)
print(c.limit_denominator(8))
print(a.limit_denominator(4))
print("--------------------")
x = 3.75
y = Fraction(*x.as_integer_ratio())
print(y)
示例12: setFrequency
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
def setFrequency(self, freq):
"""Sets the registers to the desired frequency"""
ref = 50e6
if verbose:
print("Calculating registers for: " + str(freq) + " Hz")
'''Calculate NOUT'''
'''VCO runs from 1.5 GHz up to 3 GHz, NOUT sets the VCO output divider'''
NOUT = 2 * np.floor(3e9 / (2 * freq))
'''Calculate the reference divider'''
'''PFD needs to run below 70 MHz'''
RDIV = np.ceil(ref / 70e6)
FPFD = ref / RDIV
'''Calculating NDIV'''
ratio = freq / ref
if (NOUT == 0):
NDIV = ratio * RDIV
else:
NDIV = ratio * RDIV * NOUT
NINT = np.floor(NDIV)
NFRAC = np.ceil((2 ** 24) * (NDIV - NINT))
'''Calculate the exact frequency mode register'''
fraction = Fraction(float(NDIV) - float(NINT))
EXACT = 1 / (fraction.limit_denominator())
'''Calculate charge pump settings'''
if (NOUT == 0):
FVCO = freq
else:
FVCO = freq * NOUT
ICP = 1.1e-3 + (2.3e-3 - 1.1e-3) * (FVCO - 1.5e9) / (3e9 - 1.5e9)
ICPOffset = ((2.5e-9) + 4 * (1 / FVCO)) * (ICP / (1 / (FPFD)))
'''Lock detection window settings'''
LDWindow = (((ICPOffset) / (FPFD * ICP)) + (2.66e-9) + (1 / FPFD)) / 2
'''Print out settings'''
if verbose and False:
print("-------PLL Configuration-------")
print("Freq : " + str(freq * 1e-9) + " GHz")
print("Ref : " + str(ref * 1e-6) + " MHz")
print("Ratio : " + str(ratio))
print("NOUT : " + str(NOUT))
print("RDIV : " + str(RDIV))
print("FPFD : " + str(FPFD * 1e-6) + " MHz")
print("NDIV : " + str(NDIV))
print("NINT : " + str(NINT))
print("NFRAC : " + str(NFRAC))
print("DEN : " + str(EXACT))
print("FVCO : " + str(FVCO * 1e-9) + " GHz")
print("ICP : " + str(ICP * 1e6) + " uA")
print("ICPOff : " + str(ICPOffset * 1e6) + " uA")
print("LD Wind: " + str(LDWindow * 1e9) + " ns")
print("------------------------------")
'''Check minimum divide ratio (20 for fractional mode)'''
if (NINT < 20):
print("ERROR: Minimum divide ratio not met")
return
if verbose:
print("Configuring device..."),
'''Configure reference divider'''
RDIV = int(RDIV)
self.writeReg(0x02, RDIV)
'''Configure delta-sigma modulator'''
DCM = 2
DCM = DCM | 2 << 2
DCM = DCM | 4 << 4
DCM = DCM | 0 << 7
DCM = DCM | 1 << 8
DCM = DCM | 1 << 9
DCM = DCM | 1 << 10
DCM = DCM | 1 << 11
self.writeReg(0x06, DCM)
'''Configure autocal'''
AC = 0x2046
self.writeReg(0x0A, AC)
'''Configure charge pump current'''
'''Calculate register'''
# CPC = 0x30E5CB
CPC = 1 << 21 # Enable up offset
CP = int(np.round(ICP / 20e-6))
CPC = CPC | CP << 0
CPC = CPC | CP << 7
CPO = int(np.round(ICPOffset / 5e-6))
CPC = CPC | CPO << 14
self.writeReg(0x09, CPC)
'''Configure lock detect'''
# LD = 0x94D
LD = 5 << 0
LD = LD | 1 << 3
LD = LD | 1 << 6
divide, speed = self.LDLookup(LDWindow)
#.........这里部分代码省略.........
示例13: print
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
import decimal
decimal.getcontext().prec = 2
x = decimal.Decimal(1)/decimal.Decimal(3)
y = decimal.Decimal(str(1/3)) + decimal.Decimal(str(2/3))
#
x = (2.5).as_integer_ratio()
print(type(x))
f = 2.5
z = Fraction(*f.as_integer_ratio())
y = float(z)
type(y)
Fraction.from_float(1.75)
Fraction(*(1.75).as_integer_ratio())
#
x = Fraction(*(0.5).as_integer_ratio())
y = x + 2 # = Fraction
y = x + 1/3 # = float
y = x + 2.8 # = float
#
a = Fraction(*(4/3).as_integer_ratio())
a = Fraction(*(4.0/3).as_integer_ratio())
b = a.limit_denominator()
示例14: Fraction
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
p2 = Fraction(0,1) # probability of 2 defects in this chip
s1_add0 = Fraction(chips-1, chips) # probability of a new defect adding 0 defects in this chip
s1_add1 = Fraction(1, chips) # probability of a new defect adding 1 defects in this chip
s1_add2 = Fraction(0, chips) # probability of a new defect adding 2 defects in this chip
for n in range(0,10):
p2 = (p2 * s1_add0) + (p1 * s1_add1) + (p0 * s1_add2)
p1 = (p1 * s1_add0) + (p0 * s1_add1)
p0 = (p0 * s1_add0)
print "10 steps: p0 = {0}, p1 = {1}, p2 = {2}".format(float(p0), float(p1), float(p2))
p_012 = (p0 + p1 + p2)
print "p_012 =", float(p_012), p_012
#print "time =", time.clock() - start
p2 = p2.limit_denominator(2**1024)
p1 = p1.limit_denominator(2**1024)
p0 = p0.limit_denominator(2**1024)
s10_add2 = p2
s10_add1 = p1
s10_add0 = p0
for n in range(1,10):
p2 = (p2 * s10_add0) + (p1 * s10_add1) + (p0 * s10_add2)
p1 = (p1 * s10_add0) + (p0 * s10_add1)
p0 = (p0 * s10_add0)
print "100 steps: p0 = {0}, p1 = {1}, p2 = {2}".format(float(p0), float(p1), float(p2))
p_012 = (p0 + p1 + p2)
print "p_012 =", float(p_012)
示例15: Fraction
# 需要导入模块: from fractions import Fraction [as 别名]
# 或者: from fractions.Fraction import limit_denominator [as 别名]
# Módulo fractions
from fractions import Fraction, gcd
a = Fraction(5, 4)
b = Fraction(7, 16)
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a.numerator)
print(a.denominator)
print(float(a + b))
print(gcd(10, 6))
pi = Fraction('3.1415926535897932')
print(pi.limit_denominator(50))
# Otros constructores
a = Fraction('7e-6')
print(a)
a = Fraction(10.5)
print(a)
a = Fraction('-9/2')
print(a)