VBA中的迭代 – PHP中的类比方法

使用我在电子表格中find的VBA代码来适应在线PHP应用程序。 该代码使用math中的二等分方法来为计算选项所需的计算find最优值。

Upper = estimate_upper Lower = estimate_lower UUpper = container Start_Iteration: IterationCountE = 0.000000001 While (Upper - Lower) > IterationCountE Mid = (Upper + Lower) / 2 c1 = calculations1... c2 = calculations2... If (c2 - c1) > 0 Then Lower = Mid Else Upper = MId End If Wend 'Ends the while loop If (Round(Mid, 4) = Round(UUpper, 4)) Then Upper = 2 * UUpper UUpper = Upper GoTo Start_Iteration End If Function = Mid 

大多数情况下,我理解迭代的机制。 我尝试的PHP转换如下:

 $IterationCountE = 0.00000000001; while ( ($Upper - $Lower) > $IterationCountE ) { $Mid = ($Upper + $Lower) / 2; $c1 = calculation1(); $c2 = calculation2(); if ( ($c2 - $c1) > 0 ) { $Lower = $Mid; } else { $Upper = $Mid; } if (round($Mid, 4) == round($UUpper, 4)) { $Upper = 2 * $UUpper; $UUpper = $Upper; } } return $Mid; 

这是在PHP中进行类似迭代的最佳方式吗? 将迭代包装在一个函数中并像在VBA代码中一样引用它会更好吗?

将PHP输出与macros的值比较时,我得不到相同的值结果。