我在 Swift 中的 while 循环不会终止,我错过了什么?

My while-loop in Swift won't terminate, what am i missing?

我有一个非常详细的 while 循环,并且一直非常抗拒不得不再次寻求帮助,但我不知所措!对于这个 while 循环代码,我真的很抱歉,发生了很多事情,但我不想遗漏任何东西,以防观众需要看到它。

func retRhoCurrent() -> Double {
        while abs(pTarget - pCalc) > 1 {

            //
            // SET rhoCurrent
            //

            if (pTarget > pCalc) {
                rhoLow = rhoCurrent
                if(rhoCurrent >= rhoHigh) {
                    rhoCurrent += RHO_C
                    rhoHigh = rhoCurrent
                } else {
                    rhoCurrent += ((rhoHigh - rhoCurrent)/2)
                }
            } else {
                rhoHigh = rhoCurrent
                rhoCurrent += ((rhoLow - rhoCurrent)/2)
            }

            //
            // SET rhoR
            //

            rhoR = rhoCurrent / RHO_C

            //
            // SET DIFFERENTIAL
            //
            diffAggregate = 0
            for var kIndex = 0; kIndex < NK_VALUES.count; ++kIndex {
                diffSegment = NK_VALUES[kIndex]
                diffSegment *= pow(rhoR, IK_VALUES[kIndex])
                diffSegment *= pow(tempR, JK_VALUES[kIndex])
                iFactor = 0
                if LK_VALUES[kIndex] > 0 {
                    diffSegment *= exp(-1 * pow(rhoR, LK_VALUES[kIndex]))
                    iFactor = LK_VALUES[kIndex] * pow(rhoR, LK_VALUES[kIndex])
                }
                if PK_VALUES[kIndex] > 0 {
                    diffSegment *= exp(-1 * PK_VALUES[kIndex] * pow(rhoR, 2) - BK_VALUES[kIndex] * pow(tempR - UK_VALUES[kIndex], 2))
                    iFactor = 2 * rhoR * PK_VALUES[kIndex] * (rhoR - 1)
                }
                diffAggregate += (diffSegment * (IK_VALUES[kIndex] - iFactor))
            }

            //
            // SET pCalc
            //
            zDiff + 1 + diffAggregate
            pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent
        }
        return rhoCurrent
    }

我想我还有一个问题是我想从这个 while 循环中得到的主要值是 rhoCurrent(因为我将使用这个最后的 # 来计算其他东西)。执行 "return rhoCurrent" 会起作用,对吗?

提前致谢!!

我认为您打算将这一行赋值,但它没有:

zDiff + 1 + diffAggregate

因为它没有赋值,所以 diffAggregate 没有用在 while 循环体中。 rhoR 也因此没有被使用。您的函数可以这样简化:

func retRhoCurrent() -> Double {
        while abs(pTarget - pCalc) > 1 {

            //
            // SET rhoCurrent
            //

            if (pTarget > pCalc) {
                rhoLow = rhoCurrent
                if(rhoCurrent >= rhoHigh) {
                    rhoCurrent += RHO_C
                    rhoHigh = rhoCurrent
                } else {
                    rhoCurrent += ((rhoHigh - rhoCurrent)/2)
                }
            } else {
                rhoHigh = rhoCurrent
                rhoCurrent += ((rhoLow - rhoCurrent)/2)
            }

            //
            // SET pCalc
            //
            pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent
        }
        return rhoCurrent
    }

我真的很怀疑你在循环中使用但没有在函数内初始化的变量。 rhoLowrhoHighrhoCurrenttempKzDiffpTargetpCalc的初始值是多少?

这个方法很乱,依赖很多魔法。它正在修改不属于它的值,这意味着您可能会在应用程序的其他区域遇到意想不到的事情。它绝对不是线程安全的(尽管这可能不是您关心的问题)。