I'm getting strage output with the sqrt() function in a quadratic formula program. I'll also add that I had the same problem using pow(x,.5). It's definitely the function that's causing the problem, I've made sure of it.
Output:Code:#include <iostream> #include <iomanip> #include <cmath> using namespace std; void quad(double, double, double, double&, double&); int main() { double a, b, c, x1, x2; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << "Enter the values of A, B, and C for the quadratic equation." << "\n>"; cin >> a >> b >> c; cin.ignore(3,'\n'); quad(a,b,c,x1,x2); cout << setprecision(2) << "X = " << x1 << " or " << x2 << "."; cin.get(); return 0; } void quad(double a, double b, double c, double& xPos, double& xNeg) { if (2*a != 0 && (b*b - 4*a*c) < 0) { xPos = (-b + sqrt(b*b - 4*a*c))/(2*a); xNeg = (-b - sqrt(b*b - 4*a*c))/(2*a); } else { xPos = 0; xNeg = 0; } return; }
Code:Enter the values of A, B, and C for the quadratic equation. >3 4 5 X = -1.#J or -1.#J.



LinkBack URL
About LinkBacks


