gpt4 book ai didi

php - 将 ErlangB 公式从 VBA 翻译成 PHP

转载 作者:搜寻专家 更新时间:2023-10-31 21:29:03 25 4
gpt4 key购买 nike

我在 VBA 中有以下功能可以完美运行:

Private Function MinMax(Val As Single, Min As Single, Max As Single) As Single
'Apply minimum and maximum bounds to a value
MinMax = Val
If Val < Min Then MinMax = Min
If Val > Max Then MinMax = Max
End Function

'-----------------------------------------------------------------------
Public Function ErlangB(Servers As Single, Intensity As Single) As Single
'The Erlang B formula calculates the percentage likelyhood of the call
' being blocked, that is that all the trunks are in use and the caller
' will receive a busy signal.
' Servers = Number of telephone lines
' Intensity = Arrival rate of calls / Completion rate of calls
' Arrival rate = the number of calls arriving per hour
' Completion rate = the number of calls completed per hour
Dim Val As Single, Last As Single, B As Single
Dim Count As Long, MaxIterate As Long
On Error GoTo ErlangBError
If (Servers < 0) Or (Intensity < 0) Then
ErlangB = 0
Exit Function
End If
MaxIterate = Fix(Servers)
Val = Intensity
Last = 1 ' for server = 0
For Count = 1 To MaxIterate
B = (Val * Last) / (Count + (Val * Last))
Last = B
Next Count
ErlangBExit:
ErlangB = MinMax(B, 0, 1)
Exit Function

ErlangBError:
B = 0
Resume ErlangBExit
End Function

我正在尝试使用以下代码在 PHP 中重新创建它:

function minmax($val, $min, $max)
{
//Apply minimum and maximum bounds to a value
$minmax = $val;
if($val < $min){ $minmax = $min;}
if($val > $max) { $minmax = $max;}

Return $minmax;
}

function erlangb($servers, $intensity)
{
//'The Erlang B formula calculates the percentage likelyhood of the call
//' being blocked, that is that all the trunks are in use and the caller
//' will receive a busy signal.
//' Servers = Number of telephone lines
//' Intensity = Arrival rate of calls / Completion rate of calls
//' Arrival rate = the number of calls arriving per hour
//' Completion rate = the number of calls completed per hour
if($servers < 0 || $intensity < 0) {
$erlangb = 0;
}
Else{
$maxiterate = floor($servers);
$val = $intensity;
$last = 1;
$count = 1;
while($count < $maxiterate){
$b = ($val * $last) / ($count + ($val * $last));
$last = $b;
$count ++;
}
}
return minmax($b, 0, 1);
}

但是我并不总是得到相同的结果。例如这样的请求:

erlangb(10, 5)

在 VBA 中生成的结果为 0.01838457,但在 PHP 中生成的值为 0.037457785974194。

我在过渡过程中遗漏了什么导致了截然不同的值(value)观?

最佳答案

改变这个

while($count < $maxiterate){

while($count <= $maxiterate){

关于php - 将 ErlangB 公式从 VBA 翻译成 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com