duanhe0817825 2018-06-08 13:18 采纳率: 100%
浏览 54
已采纳

复杂的财务计算导致意外结果

I'm using PHP 7.2. I'm in the process of converting an Excel spreadsheet into our financial analysis application. For one of the calculations, I'm literally copying the text in the cell, pasting it into my PHP application, then substituting the cell references with variables. There are no built-in Excel functions being used--it's just a straight formula. And I'm not physically changing the structure of the formula--it's a literal copy/paste of what is in the spreadsheet. This is the formula in Excel:

=(A38*(1-(1+(G38/U38))^-N38)/(G38/U38))/((1+(G38/U38))^(((AB38-$I$5)/(365/U38))-1))

This is how it's converted in my application:

$x = ($payment_amount * (1 - (1 + ($discount_rate / 
     $payments_per_year))^-$payments_in_series) / 
     ($discount_rate / $payments_per_year)) / 
     ((1 + ($discount_rate / $payments_per_year))^((($days_between_dates) / 
     (365 / $payments_per_year)) - 1));

There is one minor difference between mine and the one in Excel and that's (AB38-$I$5). In Excel, this calculates the number of days between two dates. In PHP, I used DateTime to find the difference in days for the two dates and just substituted it with $days_between_dates. I have verified that PHP matches what Excel calculates.

Let's assume these values:

$payment_amount = 30000;
$discount_rate = .08;
$payments_in_series = 20;
$payments_per_year = 1;
$days_between_dates = 398;

In Excel, I get the following value for this calculation: 292502.06

In PHP, I get the following value for this calculation: -7500000. When I use var_dump to examine the result, it identifies it as a float.

I plugged this function into WolframAlpha and get the same result from Excel.

I tried this same calculation in Ideone and get the same result as the PHP calculation.

Where I'm messing up? I'm all out of ideas and don't even know where else to troubleshoot. Thanks!

  • 写回答

1条回答 默认 最新

  • douzhantanju1849 2018-06-08 13:22
    关注

    In PHP "^" is not the exponentiation operator but "bitwise Xor". "**" is the equivalent operator.

    Also make sure operator precedence is not different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分