How do I check for infinite (1.#INF) and indefinite (1.#IND) numbers?

hkBattousai picture hkBattousai · Dec 23, 2011 · Viewed 10.1k times · Source
template <class T>
MyClass
{
    public:
        // ...

        void MyMethod(T dbNumber)
        {
            // ...

            T dbResult = do_some_operation_on_dbnumber(dbNumber);

            if (IsInfinite(dbResult))
            {
                // ...
            }
            else if (IsIndefinite(dbResult))
            {
                // ...
            }
            else
            {
                // ...
            }

            // ...
        }

        static bool IsInfinite(T dbNumber)
        {
            // How do I implement this?
        }

        static bool IsIndefinite(T dbNumber)
        {
            // How do I implement this?
        }

        // ...
};

There is a mathematical operation in my code which sometimes return infinite and indefinite results in a template variable. I want to catch these kind of indefinite results. How do I do that?

Answer

Steve C picture Steve C · Dec 23, 2011
   #include <limits>

   using namespace std;

   double d = 1.0 / 0.0;
   if (d == numeric_limits<double>::infinity( ))
        cout << "Its infinite, all right" << endl;
   else
        cout << "Not in my book" << endl;

This works.