Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++
Alternative
Tip/Trick

A simple program to solve quadratic equations with

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
16 Nov 2010CPOL 11.1K   3   3
I prefer this :) #include #include #include #include static const double bad_double = std::numeric_limits::quiet_NaN();class QuadSolver{ static bool IsZero(double val) { return (val == 0) || (fabs(val) <...
I prefer this :)
C++
#include <utility>
#include <limits>
#include <iostream>
#include <cmath>
static const double bad_double = std::numeric_limits<double>::quiet_NaN();

class QuadSolver
{
    static bool IsZero(double val)
    {
        return (val == 0) || (fabs(val) < std::numeric_limits<double>::epsilon());
    }
    enum {state_unknown, state_invalid, state_no_solution, state_single_solution, state_equal_solutions, state_double_solution} state;
    std::pair<double, double> solution;
    double a, b, c;

public:
    QuadSolver(double a_, double b_, double c_) : a(a_), b(b_), c(c_), state(state_unknown), solution(bad_double, bad_double)

    {
        if (IsZero(a))
            if(IsZero(b))
                state = state_invalid;
            else
            {
                state = state_single_solution;
                solution.first = -c / b;
            }
        else
        {
            double disc = b * b - 4 * a * c;
            if (IsZero(disc))
            {
                state = state_equal_solutions;
                solution.first = solution.second = -b / (2 * a);
            }
            else if (disc > 0)
            {
                state = state_double_solution;

                // accurately handle b*b much_greater_than 4*a*c
                const int sign = (b == fabs(b) ? 1 : -1);
                const double q = -(b + sign * sqrt(disc)) / 2;

                solution = std::make_pair(q / a, c / q);
            }
            else
                state = state_no_solution;
        }
    }

    std::ostream& Out(std::ostream& out) const
    {
        if (state == state_unknown)
            return out;

        out << "Equation " << a << "*x*x + "<< b << "*x + " << c << " = 0";
        switch (state)
        {
        case state_invalid:
            out << " is ill formed";
            break;
        case state_no_solution:
            out << " has no solution";
            break;
        case state_single_solution:
            out << " has a single solution: x = " << solution.first;
            break;
        case state_equal_solutions:
            out << " has two equal solutions: x1 = x2 = " << solution.first;
            break;
        case state_double_solution:
            out << " has two solutions: x1 = " << solution.first << " and x2 = " << solution.second;
            break;
        }

        return out << '.' << std::endl;
    }
};

// Global ostream extractor
inline std::ostream& operator << (std::ostream& out, const QuadSolver& qs)
{
    return qs.Out(out);
}

Test program showing usage:

C++
int main(void)
{
    std::cout << "Quadratic equation a*x*x + b*x + c = 0" << std::endl;
    while (std::cin.good())
    {
        double a, b, c; // no need to initialize
        std::cout << "Enter a  b  c (q to quit)";
        std::cin >> a >> b >> c;
        if (std::cin.good())
            std::cout << QuadSolver(a, b, c);
    }

    return 0;
}

cheer,
AR

Edited to address Federico's objection and (slightly) improve the code.

License

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


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthe solving method is not correct for large values of b, tha... Pin
federico.strati15-Nov-10 21:45
federico.strati15-Nov-10 21:45 
GeneralIt is not requested by the OP, and I am lazy :) Thanks and c... Pin
Alain Rist11-Nov-10 19:52
Alain Rist11-Nov-10 19:52 
GeneralReason for my vote of 5 Well done! Just an improvement: you ... Pin
Sauro Viti11-Nov-10 10:33
professionalSauro Viti11-Nov-10 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.