Click here to Skip to main content
12,241,419 members (55,382 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.9K views
2 bookmarked
Posted

A simple program to solve quadratic equations with

, 9 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
// Real solutions of the quadratic equation A x^2 + B x + C = 0// Returns two roots (possibly identical) in increasing order, or nonebool Quadratic(double A, double B, double C, double R[2]){ if (A == 0) { // Linear, impossible or degenerate, cannot find two roots ...
// Real solutions of the quadratic equation A x^2 + B x + C = 0
// Returns two roots (possibly identical) in increasing order, or none
bool Quadratic(double A, double B, double C, double R[2])
{
    if (A == 0)
    {
        // Linear, impossible or degenerate, cannot find two roots
        return false;
    }
 
    // Truly quadratic, reduce the coefficients and discuss
    A= 1 / A;
    B*= 0.5 * A;
    C*= A;
    double D= B * B - C;
    if (D < 0)
    {
        // No real root
        return false;
    }
 
    // Compute and sort the root(s), avoiding cancellation and division by zero
    double Q= - B + (B > 0 ? - sqrt(D) : + sqrt(D));
    R[0]= Q > 0 ? C / Q : Q;
    R[1]= Q < 0 ? C / Q : Q;
 
    return true;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

YvesDaoust
CEO VISION fOr VISION
Belgium Belgium
I fell into applied algorithmics at the age of 16 or so. This eventually brought me to develop machine vision software as a professional. This is Dreamland for algorithm lovers.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 9 Nov 2010
Article Copyright 2010 by YvesDaoust
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid