计算输出= nan?

时间:2012-01-12 17:30:59

标签: c++

首先这是一个课程作业,所以我会很感激帮助,但只是提示,因为我想学习。我必须根据我的代码中的利率等计算每月付款,但我的计算结果不合适。我的输出显示为nan,我认为是not a number。我一直试图弄清楚我哪里出错无济于事,有关如何纠正这个问题的任何建议?提前谢谢。

#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 << "-----------------------------\n" << 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

4 个答案:

答案 0 :(得分:6)

你除以零。它发生在这一行:

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

monthlyIntRate中存储的值为零,因此pow(monthlyIntRate + 1, numOfPayment) eqauls为(0 + 1) ^ numOfPayment,即为1.因此,pow(monthlyIntRate + 1, numOfPayment) - 1)为0。

答案 1 :(得分:5)

正如@FlopCoder所提到的那样,在计算monthlyPayment时,monthlyIntRate == 0。 所以分数的除数

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

0

仅获得nan是不够的。如果被分红也不是0,您将获得inf。但是你的情况 0。然后,你得到nan

答案 2 :(得分:0)

各种权力导致数量非常大。我只想使用algerbra这个等式:

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

答案 3 :(得分:0)

您执行此分配:{/ 1}} 之后,您在每月付款计算中使用它。因此,使用的月利率为0.0,并且您得到除以零,从而产生NaN输出。如果你之前移动它就可以了。

请注意,在函数顶部声明所有变量的样式非常C-esque,在C ++中不受欢迎。如果您始终将声明并初始化尽可能接近使用点,则会更容易发现这些错误。任何时候你可以在同一点声明和初始化一个变量,在你获得一个真正的价值之前,你可能会意外地使用它。