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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算