0

Unexpected values being printed

The attached code is supposed to take three inputs (like 1, -3, -306), make a quadratic equation using them(like 1x² - 3x - 306 = 0), and output the roots of the quadratic equation if Discriminant is greater than or equal to 0. However, it does not print the answers, the roots are displayed 0 and the Discriminant has a very high value no matter what the input is https://sololearn.com/compiler-playground/cgNDf4cpPecQ/?ref=app

23rd Jul 2025, 9:40 AM
Vaibhav
Vaibhav - avatar
3 Respostas
+ 3
In line 34, you compare 2 doubles with ==. This likely is an issue because of floating point precision. I haven't tested yet if this solves your issue.
23rd Jul 2025, 10:15 AM
Lisa
Lisa - avatar
0
Lisa, that is not a problem at all. The value of D will exactly be equal to 0.0 if roots are equal. Also, it should still print some value of the roots because the else statement will execute if the value is not 0.0
23rd Jul 2025, 11:57 AM
Vaibhav
Vaibhav - avatar
0
Even if it doesn't solve your main problem, I think it might still be a not-so-clean solution? I think part of the problem is how you pass the arguments to the solvequadeq() function: You pass them directly but then change them inside of the functions. Perhaps you need to use local variables or pass by reference. Does "D" get an initial value somewhere?
23rd Jul 2025, 12:17 PM
Lisa
Lisa - avatar