问题是不理解为什么Math.Pow()将小数点放在错误的位置。

huangapple go评论71阅读模式
英文:

Problem understanding why Math.Pow() puts decimal place in the wrong spot

问题

我正在参加Codecademy的课程,其中一个复习问题是创建一个计算复利的程序。目前我正在学习方法,所以我正在浏览这个网站,我找到了一个用于计算复利的方程式。我甚至使用了相同的数值。我得到了正确的答案,但是当我使用 Math.Pow() 时,小数点放错了位置。我已经查找了答案好几天,但是我卡在这里。我不想继续学下去,直到我理解为什么会发生这种情况,以及如何修复它。

注意:在课程中我们还没有讲解到if/else语句,只是在处理变量。

double amount = 1000.00;

double intRate = 2.0;

int numYears = 5;

double numTimesCompound = 2.0;

double compTotal1 = amount * (1 + intRate / numTimesCompound);
Console.WriteLine(compTotal1);

double compTotal2 = numTimesCompound * numYears;
Console.WriteLine(compTotal2);

double totIntrest = Math.Pow(compTotal1, compTotal2);

Console.WriteLine(totIntrest);

这会输出以下结果:

2000
10
1.024E+33

我尝试了不同的方法来解决这个问题,比如 Math.Floor()Math.Abs()。我还尝试将变量更改为decimal,而不是double(这应该是处理货币的正确方式,对吗?)也许这就是我的问题。此外,在某个时候,我只是为了答案而来到这里,以便我能理解我做错了什么。我浏览了一些答案,Math.Round() 似乎有我想要的东西,但 E+33 是什么意思?

英文:

I am taking a codecademy course and one of the review questions is to make a program that calculates compound interest. I'm being taught methods at the moment, so I'm all over this site and I found an equation for compound interest calculation. I even used the same values. I have the right answer but when I use Math.Pow() it puts the decimal in the wrong spot. I've been looking at answers for days and I'm so stuck. I do not want move on until I understand why this happens, and how I can fix it.

Note: we haven't gotten to if/else statements in the course. Just working with variables.

double amount = 1000.00;

double intRate = 2.0;

int numYears = 5;

double numTimesCompound = 2.0;

double compTotal1 = amount * (1 + intRate / numTimesCompound);
Console.WriteLine(compTotal1);

double compTotal2 = numTimesCompound * numYears;
Console.WriteLine(compTotal2);

double totIntrest = Math.Pow(compTotal1, compTotal2);

Console.WriteLine(totIntrest);

This gives the following output:

2000
10
1.024E+33

I've tried different methods to resolve the problem. such as Math.Floor() and Math.Abs(). I've tried changing the variables to decimal, instead of double (which should be correct with money, yes?) and maybe that's my problem. Also at a certain point, I just came here for the answer so I can understand what I'm doing wrong. I've looked through some of the answers and Math.Round() seems to have something I'm looking for but what's with the E+33?

答案1

得分: 1

每年,你之前的 amount 会增加 intRate 百分比,即不是初始的 100%,而是 100% + intRate

newAmount = prevAmount * (100 + intRate) / 100 = prevAmount * (1 + intRate / 100)

如果我们想要多年的话,会有一个望远镜式的乘法:

amount_1 = amount_0 * (1 + intRate / 100)
amount_2 = amount_1 * (1 + intRate / 100)
amount_3 = amount_2 * (1 + intRate / 100)
...
amount_n = amount_n_1 * (1 + intRate / 100)

到目前为止还好:

amount_n = amount_0 * (1 + intRate / 100) * ... * (1 + intRate / 100) = 
                      ^                                             ^
                      |---------------    n     ---------------|  

         = amount_0 * Pow((1 + intRate / 100), n)                 

让我们编写代码:

double amount  = 1000.0;
double intRate = 2.0;
int numYears   = 5;

double finalAmount = amount * Math.Pow(1 + intRate / 100.0, numYears);
double interest = finalAmount - amount;

// f2 - 让我们格式化(四舍五入)到分
Console.WriteLine($"经过 {numYears} 年: {finalAmount:f2}");
Console.WriteLine($"利息 {interest:f2}");

结果:

经过 5 : 1104.08
利息 104.08

这些结果相当现实(投资 1000$5 年内以 2% 的利率,肯定不能买下整个银河系)。

英文:

Each year your prevoius amount is icreased by intRate percent, i.e. instead of initial 100% you have 100% + intRate:

newAmount = prevAmount * (100 + intRate) / 100 = prevAmount * (1 + intRate / 100)

If we want several years we have a telecsopic multiplication:

amount_1 = amount_0 * (1 + intRate / 100)
amount_2 = amount_1 * (1 + intRate / 100)
amount_3 = amount_2 * (1 + intRate / 100)
...
amount_n = amount_n_1 * (1 + intRate / 100)

So far so good

amount_n = amount_0 * (1 + intRate / 100) * ... * (1 + intRate / 100) = 
                      ^                                             ^
                      |---------------    n times    ---------------|  

         = amount_0 * Pow((1 + intRate / 100), n)                 

Let's code:

double amount  = 1000.0;
double intRate = 2.0;
int numYears   = 5;

double finalAmount = amount * Math.Pow(1 + intRate / 100.0, numYears);
double interest = finalAmount - amount;

// f2 - let's format (round) up to cents
Console.WriteLine($"After {numYears} years: {finalAmount:f2}");
Console.WriteLine($"Interest {interest:f2}");

Figures:

After 5 years: 1104.08
Interest 104.08

Which are quite realistic (you definitely can't buy the entire Galaxy having invested 1000$ for 5 years for 2%)

答案2

得分: 0

复利的公式是 P*(1+ r/n)^nt,其中 P 是初始金额,r 是利率,n 是每个时间段复利的次数,t 是时间段的数量。在你的代码中,你对指数应用得不正确,因为你计算的是 (P*(1 + r/n))^nt,这包括了你的初始金额在指数部分,这会使最终结果比应该的大得多。

另外,正如评论中指出的那样,利率应该表示为比率而不是百分比,所以2%应该是0.02,而不是2。

英文:

There are two main things that you are missing here.

The formula for compound interest is P*(1+ r/n)^nt where P is the starting amount, r is the interest rate, n is the number of times compounded per time period, and t is the number of time periods. In your code you are applying the exponent incorrectly because what you are calculating is (P*(1 + r/n))^nt which is including your starting amount in the exponential component which is making your final result much larger than it should be.

Also, as pointed out in the comments, the interest rate should be expressed as a ratio rather than a percentage, so 2% would be 0.02, not 2

huangapple
  • 本文由 发表于 2023年7月7日 02:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定