Skip to content

Instantly share code, notes, and snippets.

@TNick
Created March 17, 2013 18:58
Show Gist options
  • Save TNick/5183058 to your computer and use it in GitHub Desktop.
Save TNick/5183058 to your computer and use it in GitHub Desktop.
LibreCAD math
std::vector<double> RS_Math::quadraticSolver(const std::vector<double>& ce)
//quadratic solver for
// x^2 + ce[0] x + ce[1] =0
{
std::vector<double> ans(0,0.);
if(ce.size() != 2) return ans;
double discriminant=0.25*ce[0]*ce[0]-ce[1];
/* the discriminant may be very small and negative */
if ( qAbs(discriminant) < 0.0000000001 ){
ans.push_back(-0.5*ce[0] );
ans.push_back(-ce[0] - ans[0]);
}
else if (discriminant >= 0.){
ans.push_back(-0.5*ce[0] + sqrt(discriminant));
ans.push_back(-ce[0] - ans[0]);
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment