对不同大小的浮点值执行算术运算

Performing arithmetic on floating point values of different sizes

如果一个浮点变量有 5 个数字,其他有 4 个数字,并且对它们执行算术运算,我的代码没有 运行 我期望的那样。例如:

def capacity(self):

    mmax = self.mmax.get()
    current = self.current.get()
    mmin = self.mmin.get()

    flag = False

    if flag is False:
        if mmax.isdigit() and mmin.isdigit() and current.isdigit():
            capacity = float(100 * ((float(current) - float(mmin)) / (float(mmax) - float(mmin))))
            if mmin <= current <= mmax and 0 <= capacity <= 100:
                flag = True
            else:
                self.result = str("Please ensure the values entered correspond to the correct value.")
        else:
            self.result = str("Please enter only positive integers.")

    if flag is True:
        self.result = "Capacity: %2.2f" % capacity + '%'

如果mmax = 10000, current = 5000, and mmin = 1000,self.result = str("Please ensure...").
mmax = 9999, current = 5000, and mmin = 1000,self.result = str("Capacity: 44.45%).

这是怎么回事/我该如何解决这个问题?

我猜 mmincurrent 以及 mmax 是字符串。在这种情况下,这个表达式:

if mmin <= current <= mmax and 0 <= capacity <= 100:

... 正在对值进行字典顺序比较。这与数值比较不同:例如,"5000" < "10000" 计算结果为 False,因为“5”大于“1”。

在对它们进行比较之前将您的值转换为数字。

if mmax.isdigit() and mmin.isdigit() and current.isdigit():
    mmax = float(mmax)
    current = float(current)
    mmin = float(mmin)

    capacity = float(100 * ... #etc

if float(mmin) <= float(current) <= float(mmax) and 0 <= capacity <= 100: