gpt4 book ai didi

asp.net - Microsoft.VisualBasic.Financial.Rate 错误, "Cannot calculate rate using the arguments provided"

转载 作者:行者123 更新时间:2023-12-02 14:22:45 28 4
gpt4 key购买 nike

因此,我们的 ASP.NET4 Web 应用程序中有一个利率计算器类,它使用 Microsoft.VisualBasic.Financial.Rate 来计算名义利率(基于输入参数)。

我们注意到,对于较高的 NPer 值(付款期总数,例如 50 年 x 每月付款 = 600),该函数会抛出异常:无法使用提供的参数计算费率。

到处搜索没有找到任何解决方案,所以我在这里发布解决方案。我们的要求是维护一个尽可能实现与上述相同算法的函数,因为我们需要产生完全相同的输出。

最佳答案

回答我自己的问题,对于任何遇到此问题的 future 编码人员 - 我们使用 dotPeek 反编译该模块,产生以下内容:

public static double Rate(double NPer, double Pmt, double PV, double FV = 0.0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
{
if (NPer <= 0.0)
throw new ArgumentException(Utils.GetResourceString("Rate_NPerMustBeGTZero"));
double Rate1 = Guess;
double num1 = Financial.LEvalRate(Rate1, NPer, Pmt, PV, FV, Due);
double Rate2 = num1 <= 0.0 ? Rate1 * 2.0 : Rate1 / 2.0;
double num2 = Financial.LEvalRate(Rate2, NPer, Pmt, PV, FV, Due);
int num3 = 0;
do
{
if (num2 == num1)
{
if (Rate2 > Rate1)
Rate1 -= 1E-05;
else
Rate1 -= -1E-05;
num1 = Financial.LEvalRate(Rate1, NPer, Pmt, PV, FV, Due);
if (num2 == num1)
throw new ArgumentException(Utils.GetResourceString("Financial_CalcDivByZero"));
}
double Rate3 = Rate2 - (Rate2 - Rate1) * num2 / (num2 - num1);
double num4 = Financial.LEvalRate(Rate3, NPer, Pmt, PV, FV, Due);
if (Math.Abs(num4) < 1E-07)
return Rate3;
double num5 = num4;
num1 = num2;
num2 = num5;
double num6 = Rate3;
Rate1 = Rate2;
Rate2 = num6;
checked { ++num3; }
}
while (num3 <= 39);
throw new ArgumentException(Utils.GetResourceString("Financial_CannotCalculateRate"));
}

private static double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
{
if (Rate == 0.0)
return PV + Pmt * NPer + dFv;
double num1 = Math.Pow(Rate + 1.0, NPer);
double num2 = Due == DueDate.EndOfPeriod ? 1.0 : 1.0 + Rate;
return PV * num1 + Pmt * num2 * (num1 - 1.0) / Rate + dFv;
}

我们可以看到,如果超过 num3,就会抛出错误,因为它的硬限制为 39。我们稍微整理了一下代码,并将限制增加到 100:

private static double CalculateUpfrontNominalRate(double numberOfPeriods, double payment, double presentValue, double futureValue = 0.0, DueDate Due = DueDate.EndOfPeriod, double Guess = 0.1)
{
if (numberOfPeriods <= 0.0)
{
throw new ArgumentException("CalculateUpfrontNominalRate: Number of periods must be greater than zero");
}

var rateUpperBoundary = Guess;
var lEvalRate1 = LEvalRate(rateUpperBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);
var rateLowerBoundary = lEvalRate1 <= 0.0 ? rateUpperBoundary * 2.0 : rateUpperBoundary / 2.0;
var lEvalRate2 = LEvalRate(rateLowerBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);

for (var i = 0; i < 100; i++)
{
if (lEvalRate2 == lEvalRate1)
{
if (rateLowerBoundary > rateUpperBoundary)
rateUpperBoundary -= 1E-05;
else
rateUpperBoundary -= -1E-05;

lEvalRate1 = LEvalRate(rateUpperBoundary, numberOfPeriods, payment, presentValue, futureValue, Due);
if (lEvalRate2 == lEvalRate1)
{
throw new ArgumentException("CalculateUpfrontNominalRate: Inputs will cause a divsion by zero");
}
}

double temporaryRate = rateLowerBoundary - (rateLowerBoundary - rateUpperBoundary) * lEvalRate2 / (lEvalRate2 - lEvalRate1);
double lEvalRate3 = LEvalRate(temporaryRate, numberOfPeriods, payment, presentValue, futureValue, Due);

if (Math.Abs(lEvalRate3) < 1E-07)
{
return temporaryRate;
}

lEvalRate1 = lEvalRate2;
lEvalRate2 = lEvalRate3;
rateUpperBoundary = rateLowerBoundary;
rateLowerBoundary = temporaryRate;
}

throw new ArgumentException("CalculateUpfrontNominalRate: The maximum number of iterations has been exceeded, unable to calculate rate");
}

private static double LEvalRate(double Rate, double NPer, double Pmt, double PV, double dFv, DueDate Due)
{
if (Rate == 0.0)
return PV + Pmt * NPer + dFv;
double num1 = Math.Pow(Rate + 1.0, NPer);
double num2 = Due == DueDate.EndOfPeriod ? 1.0 : 1.0 + Rate;
return PV * num1 + Pmt * num2 * (num1 - 1.0) / Rate + dFv;
}

关于asp.net - Microsoft.VisualBasic.Financial.Rate 错误, "Cannot calculate rate using the arguments provided",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14576138/

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