English 中文(简体)
计算产出=南
原标题:Calculation output = nan?
  • 时间:2012-01-12 17:30:59
  •  标签:
  • c++

首先是集体任务,这样一来,人们会欣赏帮助,而只是想学习的背后。 我不得不按照我守则中所示的利率等计算每月付款,但与我计算不符。 我的产出为<代码>nan,一则认为是而不是编号。 我一直在试图指出,在什么地方,我会错失,就如何纠正这一问题提出任何建议? 提前感谢。

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    //collect payment info
    double loanAmount = 0; 
    double anualRate = 0;
    double monthlyIntRate = 0; 
    int numOfPayment = 0; 
    double monthlyPayment = 0; 
    double amountPaidBack = 0; 
    double interestPaid = 0;

    cout << "Enter loan amount: ";
    cin >> loanAmount;
    cout << "Enter Anual Interest Rate: ";
    cin >> anualRate;
    cout << "Enter Payments made: ";
    cin >> numOfPayment;

    //calculate montly payment
    monthlyPayment = (loanAmount * pow(monthlyIntRate + 1, numOfPayment) * monthlyIntRate) / ( pow(monthlyIntRate + 1, numOfPayment) - 1);                                                    

    //calculate amount paid back
    amountPaidBack = monthlyPayment * numOfPayment;

    //calculate interest paid
    monthlyIntRate = anualRate / 12;
    interestPaid = monthlyIntRate * numOfPayment;

    //split input from calculated output
    cout << "-----------------------------
" << endl;

    //Display the calulated data
    cout << fixed;
    cout << setprecision(2);

    cout << "Loan Amount: " << setw(15) << "$ "<< right << loanAmount << endl;

    cout << "Monthly Interest Rate: " << setw(14) << monthlyIntRate << "%" << endl;

    cout << "Number of Payments: " << setw(17) << numOfPayment << endl;

    cout << "Montly Payment: " << setw(19) << "$ " << monthlyPayment << endl;

    cout << "Amount Paid Back: " << setw(17) << "$ " << amountPaidBack << endl;

    cout << "Interest Paid: " << setw(18) << "$ " << interestPaid << endl;

    return 0;

产出:

Loan Amount:              $ 100000.00
Monthly Interest Rate:           1.00%
Number of Payments:                36
Montly Payment:                  $ nan
Amount Paid Back:                $ nan
Interest Paid:                 $ 36.00
最佳回答

页: 1 这段话

monthlyPayment = (loanAmount * pow(monthlyIntRate + 1, numOfPayment) * monthlyIntRate) / ( pow(monthlyIntRate + 1, numOfPayment) - 1);

储存在<代码>月度日上的数值为零,因此<代码>pow(月度IntRate + 1, numOfPayment) eqauls to (0 + 1) ^ numOfPayment,即1。 因此,pow(月度IntRate + 1, numOfPayment) - 1)

问题回答

As @FlopCoder mentioned, by the time monthlyPayment is calculated, monthlyIntRate == 0. So the divisor of the fraction

monthlyPayment = (loanAmount * pow(monthlyIntRate + 1, numOfPayment) * monthlyIntRate) / ( pow(monthlyIntRate + 1, numOfPayment) - 1);

页: 1

But it is not enough to get nan. If dividend is not 0 also, you would get inf. But it is 0 in your case, too. Then, you get nan.

各种权力导致数量众多。 我只是通过使用algerbra:

monthlyPayment = (loanAmount * pow(monthlyIntRate + 1, numOfPayment) * monthlyIntRate) / ( pow(monthlyIntRate + 1, numOfPayment) - 1);                                                     

页: 1 因此,每月使用的利率为0.0,而你因零而分立,导致N产出。 如果你先搬走,那么所有工作都会奏效。

请注意,你在功能顶端申报所有变量的风格是C-esque,而不是C++。 如果你总是宣布并初步确定<> > /m>,那么这些差错在尽可能接近使用点时变得更容易发现。 当你能够宣布和开始在同一时间变数时,你可以意外地加以使用,然后才能获得实际价值。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?