This repository has been archived on 2023-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
VSMechatronics/C14/quadratic_solver_function.c

24 lines
641 B
C
Raw Normal View History

2022-04-08 15:12:38 +00:00
int SolveQuadraticEquation(float a, float b, float c, float *x1, float *x2)
{
float d; // For storing b^2 - 4*a*c
if ( a == 0) // Not a quadratie
{
return -1;
}
// calculate and store b*b-4*a*c for testing ans use later (if OK)
d = b*b - 4*a*c;
if ( d < 0 )
{
return -1; // Complex
}
// If we have got to here, we can calculate x1 and x2
*x1 = ( -b + sqrt (d)) / (2 * a); // Note the use of the * before x1 & x2
*x2 = ( -b - sqrt (d)) / (2 * a); // to write to the relevant memory locations
// As we got here OK, return 0 to indicate all is OK
return 0;
}