这个等式 returns NaN 只有当某些值被传递时
This equation returns NaN only when certain values are passed
因此对于我的 C# 编程 class 我必须计算在给定余额、每月付款和 APR 的情况下还清信用卡余额所需的月数。
这是我们要解释的公式:
这是我制作的 C# 版本:
months = (double)decimal.Divide(-1, 30) * (
Math.Log(1 + (double)(balance / payment) * (1 - Math.Pow((1 + (apr / 365)), 30)))
/ Math.Log(1 + (apr / 365))
);
它适用于我尝试过的每个示例,除了一个,它在我需要得到答案的问题中指定:
How many months will it take Alice to pay off a ,500.00 balance if the
APR value is 0.22 and she pays 5.00 per month?
输出如下:
Enter the credit card balance (example: 10000.00)
7500
Enter the monthly payment amount (example: 250.00)
125
Enter the annual percentage rate (example: 0.15)
0.22
It will take NaN months to pay off the balance.
Goodbye! Press any key to exit.
在我的调查中,我将公式插入到 WolframAlpha 中,它 returns 两个不同的值 (http://bit.ly/1LQSsEx) 也许这与它有关,但我是 C# 的新手并且已经达到我的知识结束了。
这里发生了什么,我该如何解决?
以下代码的结果产生负数。
1 + (double)(7500 / 125) * (1 - Math.Pow((1 + (0.22 / 365)), 30))
Math.Log
由于显而易见的原因只适用于数字 > 0
这就是给你 NaN
的原因。
因此对于我的 C# 编程 class 我必须计算在给定余额、每月付款和 APR 的情况下还清信用卡余额所需的月数。
这是我们要解释的公式:
这是我制作的 C# 版本:
months = (double)decimal.Divide(-1, 30) * (
Math.Log(1 + (double)(balance / payment) * (1 - Math.Pow((1 + (apr / 365)), 30)))
/ Math.Log(1 + (apr / 365))
);
它适用于我尝试过的每个示例,除了一个,它在我需要得到答案的问题中指定:
How many months will it take Alice to pay off a ,500.00 balance if the APR value is 0.22 and she pays 5.00 per month?
输出如下:
Enter the credit card balance (example: 10000.00)
7500
Enter the monthly payment amount (example: 250.00)
125
Enter the annual percentage rate (example: 0.15)
0.22
It will take NaN months to pay off the balance.
Goodbye! Press any key to exit.
在我的调查中,我将公式插入到 WolframAlpha 中,它 returns 两个不同的值 (http://bit.ly/1LQSsEx) 也许这与它有关,但我是 C# 的新手并且已经达到我的知识结束了。
这里发生了什么,我该如何解决?
以下代码的结果产生负数。
1 + (double)(7500 / 125) * (1 - Math.Pow((1 + (0.22 / 365)), 30))
Math.Log
由于显而易见的原因只适用于数字 > 0
这就是给你 NaN
的原因。