信用卡等式(C#中)

Hib*_*ion 1 c# math

我有一个功课问题,我需要进行一些验证.我得到了问题第一部分的正确答案.然而,问题的第二部分并没有那么顺利.我不确定我的计算是否已关闭或测试数据是否错误.(今年已经发生了几次).

我们假设设计一个控制台应用程序,它将解决偿还贷款需要多少个月的时间.

我不能使用任何我还没有学过的代码; 只是基本代码.(没有循环,数组等)

公式为:N = - (1/30)*ln(1 + b/p(1-(1 + i)^ 30))/ ln(1 + i)

n =几个月

ln =日志功能

b =信用卡余额

p =每月付款

i =每日利率(年利率/ 365)

问题1的测试数据:

信用卡余额:5000美元

每月付款:200美元

年率:0.28

我的回答:38个月

问题2的测试数据:

信用卡余额:7500美元

每月付款:125美元

年费率:0.22

无法得到答案

我的解决方案

static void Main(string[] args)
{
    decimal creditcardbalance, monthlypayment;
    double calculation, months, annualpercentrate;

    Console.WriteLine("Enter the credit card balance in dollars and cents: ");
    creditcardbalance = decimal.Parse(Console.ReadLine());

    Console.WriteLine("Enter the monthly payment amount in dollars and cents: ");
    monthlypayment = decimal.Parse(Console.ReadLine());

    Console.WriteLine("Enter the annual rate percentage as a decimal: ");
    annualpercentrate = double.Parse(Console.ReadLine());

    calculation = (Math.Log((1 + (double)(creditcardbalance / monthlypayment) * (1 - (Math.Pow(1 + (annualpercentrate / 365), 30)))))) / (Math.Log((1 + (annualpercentrate / 365))));
    months = (-0.033333333333333333) * calculation;


    Console.WriteLine("\nIt will take {0:F0} months to pay off the loan.", months);
    Console.WriteLine("Goodbye!");
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

小智 7

你的问题是你试图取一个负数的对数.这就是为什么你得到"NaN"(不是数字)的结果.

但是为什么第二个例子中1 + b/p(1-(1 + i)^ 30)为负?嗯,这很简单.因为您的每月利息大于您的每月付款!

$ 7500*0.22/12 = 137.5 $

由于您的每月付款仅为125美元,您将花费无限的月份(说话:从不)来支付您的债务.

NaN是编程语言表示不可计算结果的方式.

所以,你没有编程问题,你有债务问题.也许这是的问题.stackexchange.com ;-)

  • NaN是Inf的一个独特概念.负数的记录是复杂的而不是无限的.所以需要一个假想的月数,而不是无限的 (3认同)