Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

A simple program to solve quadratic equations with

, 16 Nov 2010
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)
#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:
 
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)

About the Author

Alain Rist

France France
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Nov 2010
Article Copyright 2010 by Alain Rist
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid