未来价值计算未返回预期结果
有以下 FV 函数
private static function fv($r,$n,$p,$pv=0)
{
$sum = $pv;
for ( $i=0;$i<$n;$i++ ) {
$sum += ($sum * $r) + $p;
}
return $sum;
}
这些是我传入的值:
0.0067 第444章 1834.58
这是我期望从桌面检查得到的值:4983453.613 这就是我从我的方法中得到的结果:5036155.12
让我发疯。我在网上尝试了很多其他功能;他们都不起作用!
have the following FV function
private static function fv($r,$n,$p,$pv=0)
{
$sum = $pv;
for ( $i=0;$i<$n;$i++ ) {
$sum += ($sum * $r) + $p;
}
return $sum;
}
These are the values I'm passing in:
0.0067
444
1834.58
This is the value I'm expecting from my deskcheck: 4983453.613
This is what I'm getting from my method: 5036155.12
Driving me nuts. I've tried a bunch of other functions on-line; none of them work!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个舍入误差,但仅限于
0.0067
不够精确。对于
$r = 0.0067
我得到的结果与你得到的结果相同。对于
$r = 0.006666666666666666666666
我得到了你想要的结果。我建议您传入
$r = 1 / 150
以获得最佳精度。This is a rounding error, but only in so far as
0.0067
is not precise enough.For
$r = 0.0067
I get the same result as your are getting.For
$r = 0.00666666666666666666666
I get the result you want.I suggest you pass in
$r = 1 / 150
for the best possible precision.我刚刚通过 Excel 电子表格运行此操作(将单元格 A1 设置为 0,将单元格 A2 设置为
=A1 + (A1*0.0067) + 1834.58
,向下复制到 A445(表示从 0 开始的 444 次迭代)。正如您所说,最终迭代得出 5036155.12您如何计算 4983453.613 。你说你在期待吗?我在任何阶段都没有得到这个结果:
最后三个迭代
是:
I just ran this through an Excel spreadsheet (set cell A1 to 0, set cell A2 to
=A1 + (A1*0.0067) + 1834.58
, copy down to A445 (representing 444 iterations starting from 0). The final iteration comes up with 5036155.12 as you say your function does.How are you calculating the 4983453.613 you say you are expecting? Have you double checked your method there? I don't get that at any stage.
The first three iterations come out as:
and the final three as:
稍微整理一下,上面的内容应该可以解决问题。$p 没有在任何时候设置,因为你的范围,你可以在函数中重新调整 $pv 的用途,而不必定义 $sum。
A little tidying up and the above should do the trick.. $p was not set at any time, because of your scoping, you can repurpose $pv within the function without having to define $sum.