Infinity: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added solution in J.)
(Added Java example)
Line 20: Line 20:
Positive infinity is produced by the primary constant function<code> _: </code>
Positive infinity is produced by the primary constant function<code> _: </code>
<br>It is also represented directly as a numeric value by an underscore, used alone.
<br>It is also represented directly as a numeric value by an underscore, used alone.

=={{header|Java}}==
Java does not have a test for the existence of infinity, but it does have the concept in the <tt>Double</tt> class.
Double infinity = Double.POSITIVE_INFINITY;
Double.isInfinite(infinity); //true
The largest possible number in Java is also in the Double class.
Double biggestNumber = Double.MAX_VALUE;
Its value is (2-2<sup>-52</sup>)*2<sup>1023</sup> or 1.7976931348623157*10<sup>308</sup> (a.k.a. "big"). Other number classes (<tt>Integer</tt>, <tt>Long</tt>, <tt>Float</tt>, <tt>Byte</tt>, and <tt>Short</tt>) have maximum values that can be accessed in the same way.

Revision as of 20:03, 20 January 2008

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.

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();
}

J

Positive infinity is produced by the primary constant function _:
It is also represented directly as a numeric value by an underscore, used alone.

Java

Java does not have a test for the existence of infinity, but it does have the concept in the Double class.

Double infinity = Double.POSITIVE_INFINITY;
Double.isInfinite(infinity); //true

The largest possible number in Java is also in the Double class.

Double biggestNumber = Double.MAX_VALUE;

Its value is (2-2-52)*21023 or 1.7976931348623157*10308 (a.k.a. "big"). Other number classes (Integer, Long, Float, Byte, and Short) have maximum values that can be accessed in the same way.