C++ C++;:您如何确定解决方案是微不足道的还是不重要的';二次函数不存在吗?
我正在做一个程序,它应该计算一个二次函数的根并输出它的根。然而,并非所有情况下的输出都应该如此。当它被认为没有解决方案或很琐碎时,它输出为C++ C++;:您如何确定解决方案是微不足道的还是不重要的';二次函数不存在吗?,c++,quadratic-programming,C++,Quadratic Programming,我正在做一个程序,它应该计算一个二次函数的根并输出它的根。然而,并非所有情况下的输出都应该如此。当它被认为没有解决方案或很琐碎时,它输出为-nan(ind)。当它应该有一个解决方案时,它输出x1=-nan(ind)和x2=-inf。我不太清楚为什么会发生这种情况,我真的需要一些帮助。这是我的密码: #include <iostream> #include <cmath> #include <iomanip> using namespace std; int
-nan(ind)
。当它应该有一个解决方案时,它输出x1=-nan(ind)
和x2=-inf
。我不太清楚为什么会发生这种情况,我真的需要一些帮助。这是我的密码:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
// Initialize and define the variables:
// a = the variable that stores the value for 'a' in the quadratic
// b = the variable that stores the value for 'b' in the quadratic
// c = the variable that stores the value for 'c' in the quadratic
// d = the variable that stores the determinant of the quadratic function to find the nature of the roots (b^2-4ac)
// root1 = the variable that stores the first possible root of a quadratic function
// root2 = the variable that stores the second possible root of a quadratic function
// realNum = the variable that stores the real portion of the complex roots
// imaginaryNum = the variable that stores the imaginary portion of the complex roots
double a, b, c, d, root1, root2, realNum, imaginaryNum;
// Ask the user to input a value for variable 'a' of the quadratic
// NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
// appear after the entered digit
cout << "Please input a: " << setprecision(4) << fixed;
cin >> a; /// Store the value in variable 'a'
// Ask the user to input a value for variable 'b' of the quadratic
// NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
// appear after the entered digit
cout << "Please input b: " << setprecision(4) << fixed;;
cin >> b; /// Store the value in variable 'b'
// Ask the user to input a value for variable 'c' of the quadratic
// NOTE: 'setprecision' specifies the minimum precision, 'fixed' states a fixed number of decimals will
// appear after the entered digit
cout << "Please input c: " << setprecision(4) << fixed;;
cin >> c; /// Store the value in variable 'c'
// Calculate the determinant of the quadratic (b^2 - 2ac)
d = ((pow(b, 2.0)) - (4 * a * c));
// Check to see if the determinant is greater than 0
if (d >= 0) {
// Calculate each of the two possible roots for the quadratic
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
// Display to the user that a solution does exist for the following quadratic
cout << "Your equation has real roots: " << root1 << " and " << root2 << "." << endl;
}
// Check to see if the determinant is greater than 0
else if (d < 0) {
// Calculate the real portion of the complex roots for the quadratic
realNum = (-b) / (2 * a);
// Calculate the imaginary portion of the complex roots for the quadratic
imaginaryNum = (sqrt(-d)) / (2 * a);
// Combine the two portions of the complex roots and display the calculated complex roots to the user
cout << "Your equation has complex roots: " << realNum << " + " << imaginaryNum << "i and "
<< realNum << " - " << imaginaryNum << "i." << endl;
}
// Indicate that the program ended successfully
return 0;
} // End of function main
#包括
#包括
#包括
使用名称空间std;
int main(){
//初始化并定义变量:
//a=在二次曲线中存储“a”值的变量
//b=在二次曲线中存储“b”值的变量
//c=在二次曲线中存储“c”值的变量
//d=存储二次函数行列式以确定根的性质的变量(b^2-4ac)
//root1=存储二次函数第一个可能根的变量
//root2=存储二次函数第二个可能根的变量
//realNum=存储复数根的实部的变量
//imaginaryNum=存储复数根的虚部的变量
双a,b,c,d,root1,root2,realNum,imaginaryNum;
//要求用户为二次曲线的变量“a”输入一个值
//注:“setprecision”指定最小精度,“fixed”表示将使用固定数量的小数
//显示在输入的数字之后
cout问题是这里你要除以0:
imaginaryNum = (sqrt(-d)) / (2 * a);
浮标的行为是由标准定义的,它表示如果将一个数除以0,则数字将表示为<强>无穷大,此外,除以0是在C++中。
这就是您希望检查用户输入的原因。您可能不会输入无效的数字,但用户可能会很好地输入。此代码将是一个临时解决方案。请注意,这只是问题的临时解决方案,因为像“12a”这样的字符串仍将被接受:
#include <iostream>
#include <exception>
int main() {
double input;
bool is_valid = false;
while (is_valid != true) {
try {
cin >> input; //
if (!cin) {
cin.clear(); // reset failbit
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::bad_exception();
}
else {
is_valid = true;
}
}
catch (...) {
std::cout << "Input a number!\n";
}
}
// ... Do this for all your inputs and proceed.
}
#包括
#包括
int main(){
双输入;
bool是有效的=错误的;
while(是否有效!=true){
试一试{
cin>>输入//
如果(!cin){
cin.clear();//重置故障位
cin.ignore(std::numeric_limits::max(),'\n');
抛出std::bad_异常();
}
否则{
is_valid=true;
}
}
捕获(…){
首先,如果a很小,我们可以说平方项消失了,所以它变成一条有一个根的直线(除非平行于x轴)
如果行列式是负数,方程就不会穿过x轴,所以没有根,或者,如果你愿意,根是虚的
为了提高数值稳定性,使用此函数。编写此函数是为了避免使用高中公式可能出现的接近0/0项
int quadratic_roots(double a, double b, double c, double *out)
{
double rootb2minus4ac;
double b2minus4ac;
if(fabs(a) < FLT_EPSILON * 0.01)
{
if(fabs(b) > FLT_EPSILON * 0.01)
{
out[0] = c/-b;
return 1;
}
return 0;
}
b2minus4ac = b*b - 4*a*c;
if(b2minus4ac > 0)
{
rootb2minus4ac = sqrt(b*b - 4*a*c);
if(b >= 0)
{
out[0] = (-b -rootb2minus4ac)/(2*a);
out[1] = (2*c)/ (-b -rootb2minus4ac);
}
else
{
out[0] = (2*c)/(-b + rootb2minus4ac);
out[1] = (-b + rootb2minus4ac)/(2*a);
}
if(a < 0)
{
double temp = out[0];
out[0] = out[1];
out[1] = temp;
}
return 2;
}
else if(b2minus4ac == 0.0)
{
out[0] = (2*c)/-b;
return 1;
}
return 0;
}
int平方根(双a、双b、双c、双*出)
{
双rootb24ac;
双B24AC;
if(fabs(a)FLT_ε*0.01)
{
out[0]=c/-b;
返回1;
}
返回0;
}
B24ac=b*b-4*a*c;
如果(B24AC>0)
{
rootb24ac=sqrt(b*b-4*a*c);
如果(b>=0)
{
out[0]=(-b-4ac)/(2*a);
out[1]=(2*c)/(-b-4ac);
}
其他的
{
out[0]=(2*c)/(-b+4ac);
out[1]=(-b+4ac)/(2*a);
}
if(a<0)
{
双温=输出[0];
out[0]=out[1];
out[1]=温度;
}
返回2;
}
否则如果(B24AC==0.0)
{
out[0]=(2*c)/-b;
返回1;
}
返回0;
}
以下是代码中有问题的部分:
// Calculate each of the two possible roots for the quadratic
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
//....
// Calculate the real portion of the complex roots for the quadratic
realNum = (-b) / (2 * a);
// Calculate the imaginary portion of the complex roots for the quadratic
imaginaryNum = (sqrt(-d)) / (2 * a);
如可以清楚地看到,变量<强> a <强>在分母中。变量<强> a <强>可以具有<强>任何< /强>值,包括零,除以零,可以导致C++。
浮点数的行为由定义。它声明
如果将一个数字除以0,则该数字将表示为
无限
我的建议是检查a是否等于零。如果a等于零,则抛出一个错误
cout << "Please input a: " << setprecision(4) << fixed;
cin >> a;
if (a == 0){
std::cerr << "The variable a should not be equal with 0";
return 1;
}
cout-nan表示非整数,是否除以零?继续之前检查a、b、c的值。如果a
为零,则方程式不再是二次方程式。请在其中断时提供测试用例(例如,精确的输入和输出)。选择一个退化的测试用例。在执行的每一步写下所有变量的预期值。如果发现异常,请停止并思考,否则启动调试器并检查自己。与其他注释相反,除以零在IEEE浮点中给出无穷大,而不是NaN。调用sqrt()
带负参数给出一个NaN,并且(几乎)任何涉及NaN的表达式都会继续给出一个NaN。因此,在计算sqrt(d)
之前,检查d
是否为非负。[这是二次方程没有实根的情况]。