0

I want to make the quadratic equation in C++ using only a,b, and c as the variables, using mostly parenthesis, but i'm not getting the correct answers.

root1 = ((-1*b) + (std::sqrt((b*b) - (4*a*c)))) / 2*a;
root2 = ((-1*b) - (std::sqrt((b*b) - (4*a*c)))) / 2*a;
user4581301
  • 33,082
  • 7
  • 33
  • 54

1 Answers1

0

In

root1 = ((-1*b) + (std::sqrt((b*b) - (4*a*c)))) / 2*a;

the 2*a winds up dividing everything by 2 and then multiplying by a. Some brackets will fix that.

root1 = ((-1*b) + (std::sqrt((b*b) - (4*a*c)))) / (2*a);
user4581301
  • 33,082
  • 7
  • 33
  • 54