[C++] coding a quadratic root finder with if statements

Click For Summary

Discussion Overview

The discussion revolves around coding a quadratic root finder in C++ using if statements. Participants are addressing issues related to the implementation of the program, particularly focusing on the handling of coefficients and the conditions for determining the number of real roots. The scope includes debugging, coding practices, and mathematical reasoning related to quadratic equations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that comparing floating-point numbers directly to zero can lead to inaccuracies due to machine precision.
  • Another suggests using a small threshold (e.g., abs(a-0.0)<0.00001) for comparisons instead of direct equality.
  • There is a recommendation to include debug statements after variable assignments to trace the program's execution and identify errors.
  • A participant rewrites the initial if statements to incorporate the suggested improvements but encounters issues with complex roots when specific coefficients are inputted.
  • Concerns are raised about ensuring that coefficients are not zero or too close to zero, which could lead to incorrect calculations or undefined behavior.

Areas of Agreement / Disagreement

Participants express differing views on the best practices for comparing floating-point numbers and handling edge cases in the code. There is no consensus on the final implementation or the specific conditions that should be checked.

Contextual Notes

Participants highlight limitations related to the precision of floating-point arithmetic and the potential for complex roots when the discriminant is negative. The discussion does not resolve these issues, leaving them open for further exploration.

maiad
Messages
101
Reaction score
0
[C++] coding a quadratic root finder with "if statements"

//This program solves the qudratic equation
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
//Declaring Varibles
double coefA;
double coefB;
double coefC;
double x1;
double x2;
double x3;

//Initialize varibles
x1=0.0;
x2=0.0;
x3=0.0;
coefA=0.0;
coefB=0.0;
coefC=0.0;

//Input of values

count<<"Enter the value of the coefficient of the second order (x squared) term:";
cin>>coefB;

count<<"Enter the value of the coefficient of the first order term:";
cin>>coefA;

count<<"Enter the value of the constant term:";
cin>>coefC;

count<<"\n\n";

count<<"The coefficents of the quadratic equation are:\n";

count<<" coefficient of the second order term is "<<coefB<<",\n";

count<<" coefficient of the first order term is "<<coefA<<",\n";

count<<" coefficient of the constant term is "<<coefC<<"\n";

count<<"\n\n\n";

//Equation body
x1=(-coefB+sqrt(coefB*coefB-4*coefA*coefC))/(2*coefA);

x2=(-coefB-sqrt(coefB*coefB-4*coefA*coefC))/(2*coefA);

x3=-coefC/coefB;

//"If" staements that returns the appropriate number of roots

if(coefA==0&&coefB==0)
{
if(coefC==0&&coefB==0)
{
count<<"There are infinite number of possible solutions";
}
else
{
count<<"There are no real roots.";
}
}


else if (fabs(coefA-0)<0.005)
{
count<<"There is one real root = "<<fixed<<setprecision(2)<<x3;
}

else if((coefB*coefB-4*coefA*coefC)>0.0)
{
count<<"There are two real roots, root 1 = "<<fixed<<setprecision(2)<<x1<<" root2 = "<<x2;
}

else if(fabs(x1-x2)<0.005)
{
count<<"There is one double real root, root1 = "<<fixed<<setprecision(2)<<x1;
}

else
{
count<<"There are no real roots.";
}


return 0;
}
Above is my code so far, I'm not sure why when i enter any value at all, the out put will always be the same giving me "There are no real roots".

I'm assuming there is something wrong with the first "if" statements since before i put that in, it gave me the correct roots. can someone give me some guidance?
 
Last edited:
Technology news on Phys.org


1) its bad to compare a==0 if a is a float or double as the compiler interprets the 0 as an integer value

2) its bad to compare a=0.0 if a is a float or double due to machine precision

3) better to use: abs(a-0.0)<0.00001 or some equivalent math function that does this foryou

4) its good to put in count statements each time a variable is assigned a value so you can see where your pgm went wrong as in: count <<"x="<<x;

so rewrite these lines with the code improvements mentioned above:

if((coefA==0&&coefB==0)<0.0) // what are you trying to do here ?
{
if(coefC==0&&coefB==0)
 


i think that was just a unfinished code that i did before i saved. i reworte it as:
if((fabs(coefA-0.0)<0.00001)&&(abs(coefB-0.0)<0.00001))
{
if((fabs(coefA-0.0)<0.00001)&&(abs(coefB-0.0)<0.00001))
{
count<<"There are infinite number of possible solutions";
}
else
{
count<<"There are no real roots.";
}
}
and to seems so far.

But now, when i put in the inputs :coefB=3, coefA=5,coefC=1, it gives me root1= -1#j, root2=-1,#j so I'm asumming something is wrong with :else if(fabs((coefB*coefB-4*coefA*coefC)-0.0)>0.00001)
{
count<<"There are two real roots, root 1 = "<<fixed<<setprecision(2)<<x1<<" root2 = "<<x2;
}

but dosent the the value it computes not satsify:
fabs((coefB*coefB-4*coefA*coefC)-0.0)>0.00001?
 


Just as a side note, you will probably want to guarantee that the coeffecients of the quadratic are not zero or close enough to zero to cause problems, and you can do this by having a simple loop in the input stage that checks this condition and asks you to enter it in again if it doesn't meet the criteria.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K