我正在尝试编写代码来计算一个过程所需的功率,该过程的划分越多,效率就越高。



我认为这应该可以起作用,但是我的IDE总是在很长之后无法运行 延迟,引用内存问题。 我想从该代码中想要的关键是能够获得所需的总能量 在带来1.2至5的每个切片之间的间隙宽度上。 FF(a,b)是唯一寻找问题的地方,其余的按预期工作, 或至少足够好

ip = 1.2
fp = 5
#The process is more efficient the smaller b -a is hence the following function
def efficiency(a, b):
    eff = a/b
    return eff
#The energy difference between input and output is directly related to b-a
def power(a,b):
    pwr = b-a
    return pwr
#This gives the actual energy needed for a change from a to b
def workneeded(a, b):
    wrk = ((efficiency(a, b))**(-1)) * power(a, b)
    return wrk
def ff(a, b):
#I had trouble with other options so I decided to simply start with any empty list
    lis = [0,0]
#This is what I think is a way to make the function work until a reaches b through += it
#it stands for iteration
    while b != a:
        it = .38
        a += it
        tot = workneeded(a-it, a)
        lis.append(tot)
#I think this makes the function finish by the point that a = b
    if a == b:
#What I'm really after is the next line
        print(sum(lis))
ff(ip, fp)

而不是 while a != b:,尝试使用 while a - b > toltol是一个很小的数字。

ff(a, b)中,您的b永远不会等于a,并且循环永无止境。

但是为什么?

那是因为所有数字均以二进制存储在内存中。

如果您进行一些计算以获取十进制的二进制,那么在大多数情况下,它将成为重复的小数。

不可能存储所有数字,因此数字现在与您的意图略有不同。

在空闲中尝试一下:

>>> import ctypes
>>> bin(ctypes.c_int.from_buffer(ctypes.c_float(0.38)).value)
>>> bin(5)

根据需要更换浮动号码。您会发现3.8 1.2不等于5。

最新更新