1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <string>
#include <cmath>
double quadraticRoots(double a, double b, double c, double& bigRoot, double& smallRoot);
bool pathological(double a, double b, double c, double& root);
double radical(double a, double b, double c);
int main()
{
double x=0;
double y=0;
std::cout<<quadraticRoots(1,5,6,x,y);
return 0;
}
double quadraticRoots(double a, double b, double c, double& bigRoot, double& smallRoot){
if (pathological(a,b,c,bigRoot))
smallRoot = bigRoot;
else {
double rad = radical(a,b,c);
if (rad >= 0){
if (a > 0){
bigRoot = (-b + rad) / (2*a);
smallRoot = (-b - rad) /(2*a);
} else {
bigRoot = (-b - rad) / (2*a);
smallRoot = (-b + rad) /(2*a);
return smallRoot;
}
}
}
}
bool pathological(double a, double b, double c, double& root){
bool isPath = false; // not pathological is default
if (a == 0.0) {
isPath = true; // not actually a quadratic
if (b!=0.0)
root = -c/b;
}
return isPath;
}
double radical(double a, double b, double c){
double rad = b*b - 4 * a * c;
if (rad >= 0.0) return sqrt(rad);
return -1.0;
}
|