Infinity

From Rosetta Code
Revision as of 17:46, 20 January 2008 by Ce (talk | contribs) (New task, C++ code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Infinity
You are encouraged to solve this task according to the task description, using any language you may know.

Write a function which tests if infinity is supported for floating point numbers (this step should be omitted for languages where the language specification already demands the existence of infinity, e.g. by demanding IEEE numbers), and if so, returns positive infinity. Otherwise, return the largest possible number.

For languages with several floating point types, use the type of the literal constant 1.0 as floating point type.

Template:C++

#include <limits>

double inf()
{
  if (std::numeric_limits<double>::has_infinity)
    return std::numeric_limits<double>::infinity();
  else
    return std::numeric_limits<double>::max();
}