|
#include<math.h> |
|
|
|
#include "quadratic.h" |
|
|
|
int getRoots(double a, double b, double c, double *root1, double *root2) { |
|
|
|
//what kind of errors could face here? |
|
//segmentation fault: if root1 or root2 were NULL, then we would have a problem |
|
//if a == 0 we only have a single root, division by zero |
|
//discriminant is complex then we have NaN |
|
//or... NO ERRORS! |
|
|
|
if(root1 == NULL || root2 == NULL) { |
|
return NULL_POINTER_ERROR; |
|
} else if(a == 0) { |
|
return DIVISION_BY_ZERO_ERROR; |
|
} else if(b*b - 4*a*c < 0) { |
|
return COMPLEX_NUMBER_ERROR; |
|
} |
|
|
|
*root1 = (-b + discriminant(a, b, c)) / (2*a); |
|
*root2 = (-b - discriminant(a, b, c)) / (2*a); |
|
return NO_ERROR; |
|
} |
|
|
|
double discriminant(double a, double b, double c) { |
|
//principle: we should NOT handle errors in our functions, instead |
|
//if an error occurs, we should COMMUNICATE the error to the calling |
|
// function, then IT can decide what to do |
|
return sqrt(b*b - 4*a*c); |
|
} |
|
|
|
double root01(double a, double b, double c) { |
|
return (-b + discriminant(a, b, c)) / (2*a); |
|
} |
|
|
|
double root02(double a, double b, double c) { |
|
return (-b - discriminant(a, b, c)) / (2*a); |
|
} |