当我使用while循环时,该函数返回一个正确的值,但是当我使函数递归时,它返回一个nan。出于调试目的,我在返回它之前输入了值(x)并给出了正确的答案,但是在将值返回给调用函数之后,它是一个nan。另一件事,程序对x的系数不取0。任何尝试都会导致nan。下面是我的代码(所有这些只是为了确保我没有提供足够的信息):
// This is my first useful program
// to calculate the root (Solution) of exponential
// functions up to the fourth degree using
// Newton-Raphson method and applying a recursive function
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
// Below is the function fGetRoot's prototype declaration
// c4, c3, etc are the coefficients of x in the 4th power, 3rd power
// etc, while c is the constant
float fGetRoot (float c4, float c3, float c2, float c1, float c);
float fPowerFunc(float fPowered, int iPower);
float x; // declaring this as global variables so they can be initialized in the calling function
int i=10; // and be used in the recursive function without being reinitialized during each recursion
int main()
{
float c4, c3, c2, c1, c;
float fRoot;
cout << "Hello, I am a genie for calculating the root of your problems\
up to the fourth power" << endl;
cout << "Please enter the values of the coefficients of x to the power 4\
, 3, 2, 1 and the constant respectively" << endl;
cout << "x^4:" << endl;
cin >> c4;
cout << "\n x^3:" << endl;
cin >> c3;
cout << "x^2:" << endl;
cin >> c2;
cout << "\n x:" << endl;
cin >> c1;
cout << "Constant, C:" << endl;
cin >> c;
cout << "\nEnter the initial iteration. Any figure is fine. The closer to the answer, the better" << endl;
cin >> x;
i=10; // gets the number of times to iterate. the larger, the more accurate the answer
fRoot = fGetRoot(c4, c3, c3, c1, c);
cout <<"\nAnd the root is: " << fRoot << "\n";
return 0;
}
// The fGetRoot function
float fGetRoot(float c4, float c3, float c2, float c1, float c)
{
float fTemp1, fTemp2, fTemp3, fTemp4;
// the series of lines below are equivalent to the one line below but clearer
fTemp1 = c4*fPowerFunc(x,4);
cout << "This is the value of c4*x^4: "<< fTemp1 << endl; // for debugging purposes
fTemp2 = c3*fPowerFunc(x,3);
fTemp3 = fTemp2 + fTemp1;
fTemp1 = c2*fPowerFunc(x,2);
cout << "This is the value of c2*x^2: "<< fTemp1 << endl; //for debugging purposes
fTemp2 = c1*fPowerFunc(x,1);
fTemp4 = fTemp1 + fTemp2 + c;
fTemp1 = fTemp3 + fTemp4;
fTemp2 = 4*c4*fPowerFunc(x,3);
fTemp3 = 3*c3*fPowerFunc(x,2);
fTemp4 = fTemp2 + fTemp3 + 2*c2*x;
fTemp2 = fTemp4;
fTemp3 = fTemp1/fTemp2;
fTemp1 = fTemp3;
fTemp2 = x - fTemp1;
x = fTemp2;
i--;
// The line below is equivalent to the "fTemp" series of lines... just to be sure
//x=x-(c4*fPowerFunc(x,4)+c3*fPowerFunc(x,3)+c2*fPowerFunc(x,2)+c1*fPowerFunc(x,1)+c)/(4*c4*fPowerFunc(x,3)+3*c3*fPowerFunc(x,2)+2*c2*x);
cout << "\nThis is x: " << x << endl;
i--;
if(i==0)
return x;
x=fGetRoot(c4, c3, c2, c1, c); // Let the recursion begin...
}
// A simpler approach to the fPowerFunc(). This gets two numbers and powers the left one by the right one. It works right
float fPowerFunc(float fPowered, int iPower)
{
float fConstant=fPowered;
while(iPower>0)
{
fPowered *=fConstant;
iPower--;
}
return fPowered;
}
答案 0 :(得分:4)
你在功能结束时忘了return x
。编译器可以在寄存器中保存临时值,使其保持返回值并使其看起来有效。
您应该编译并启用更多警告,以便编译器可以告诉您类似的事情。
答案 1 :(得分:2)
此声明中还有另一个问题:if(i==0)
,应由if(i<=0)
替换。
在浮动领域,严格来说没有平等的概念。范围中的数字是更正确的抽象。例如,要测试f
是否大约为零,您可以这样做:
if(f >= -epsilon && f <= epsilon) ...
其中epsilon
是标准FLT_EPSILON或其他特定于应用的精度常量。