2010-03-16 84 views
1

我试图让这个表达式起作用,我很确定它不是括号,因为我把它们都算在内。也许有什么我做错了,涉及参数pow(x,y)。C++关于pow函数的问题

double calculatePeriodicPayment() 
{ 
periodicPaymentcalc = (loan * ((interestRate/yearlyPayment)))/(1-((pow ((1+(interestRate/yearlyPayment)))),(-(yearlyPayment * numOfYearLoan)))); 

return periodicPaymentcalc; 
} 
+12

如果你把计算分成更小的步骤,它可能会更具可读性吗? – 2010-03-16 20:48:17

+1

请提供您收到的错误/不当行为。 – 2010-03-16 20:48:36

+1

它以什么方式不起作用?编译错误?运行时错误的结果?运行时错误?如果这是一个错误,请发布错误。另外,我们可能需要知道所有这些变量的类型。 – 2010-03-16 20:49:03

回答

9

请注意,如果您将每一步分解为多个部分,可以更轻松地弄清楚函数的功能: (如果您的变量与源材料匹配,我发现它更容易,所以我将在我的变量的那些维基百科使用。)

// amortization calculator 
// uses annuity formula (http://en.wikipedia.org/wiki/Amortization_calculator) 
// A = (P x i)/(1 - pow(1 + i,-n)) 
// Where: 
// A = periodic payment amount 
// P = amount of principal 
// i = periodic interest rate 
// n = total number of payments 
double calculatePeriodicPayment() 
{ 
    const double P = loan; 
    const double i = interestRate/yearlyPayment; 
    const double n = yearlyPayment * numOfYearLoan; 

    const double A = (P * i)/(1 - pow(1.0 + i, -n)); 

    return A; 
} 

它更容易,以确认此功能的逻辑做的事情应该这样。

如果你好奇,在我的替代变量名,你parenthises问题如下:

const double A = (P * i)/(1 - pow(1 + i)), -n; // <- this is how you have it 
    const double A = (P * i)/(1 - pow(1 + i, -n)); // <- this is how it should be 

有了这个分组,你只传递一个参数pow,这就是为什么说编译no overloaded function takes 1 arguments

编辑:你提到我使用了更多的变量。但是,你的编译器会像我一样使用临时变量。您复杂的语句将被分解成块,并可能是这个样子:

double calculatePeriodicPayment() 
{ 
    const double temp1 = interestRate/yearlyPayment; 
    const double temp2 = loan * temp1; 
    const double temp3 = interestRate/yearlyPayment; 
    const double temp4 = 1.0 + temp3; 
    const double temp5 = yearlyPayment * numOfYearLoan; 
    const double temp6 = -temp5; 
    const double temp7 = pow(temp4, temp5); 
    const double temp8 = 1 - temp7; 
    const double temp9 = temp2/temp8; 

    periodicPaymentcalc = temp9; 
    return periodicPaymentcalc; 
} 

矿山也将被打散了,看起来就像:

double calculatePeriodicPayment() 
{ 
    const double P = loan; 
    const double i = interestRate/yearlyPayment; 
    const double n = yearlyPayment * numOfYearLoan; 

    const double temp1 = P * i; 
    const double temp2 = 1.0 + i; 
    const double temp3 = -n; 
    const double temp4 = pow(temp2, temp3); 
    const double temp5 = 1 - temp4; 
    const double temp6 = temp1/temp5; 
    const double A = temp6; 

    return A; 
} 

或许有一些优化的编译器会使用它,比如注意到它在你的函数中使用了两次,并且在两个地方使用了相同的临时文件,但是这不会发生。请注意,我们在两个函数中都使用了几乎相同数量的变量。我只使用了更多的命名变量,以及更少的未命名的临时变量。

+1

+1:很好的答案,很好的指导如何编写可读代码 – 2010-03-16 22:08:48

+0

非常感谢。我无法让它在一条线上运行,所以我分成了更小的步骤,但有更多的变量。 – Sagistic 2010-03-18 19:35:23

+0

@Sagistic:很高兴能帮到你!尽管我没有使用比你更多的变量,但意识到这一点很重要。你的只是编译器用于计算机中间结果的未命名变量。我会编辑我的帖子来详细说明。 – Bill 2010-03-18 19:42:35

2

有一个错位的括号。这里有一个固定的版本:

periodicPaymentcalc = (loan * ((interestRate/yearlyPayment)))/(1 - ((pow ((1+(interestRate/yearlyPayment)),(-(yearlyPayment * numOfYearLoan)))))); 

使用,突出括号匹配,以避免这种错误的编辑。或者简单地创建临时变量来保存中间值。

+0

仍然存在相同的错误 – Sagistic 2010-03-16 20:51:31

+0

或者在出于某种原因临时变量不是选项的情况下使用多行代码写出计算。 – Brian 2010-03-16 20:52:12

+0

我想我只需要使用临时值并简化它 – Sagistic 2010-03-16 20:52:52

1
periodicPaymentcalc = (loan * interestRate/yearlyPayment)/
    (1.0 - pow (1.0 + interestRate/yearlyPayment, -yearlyPayment * numOfYearLoan)); 

试试看。我也删除了所有冗余括号,并将所有文字更改为双精度,只是为了更好的衡量。