Infinity: Difference between revisions

From Rosetta Code
Content added Content deleted
(Ada example)
Line 48: Line 48:
=={{header|Java}}==
=={{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.
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; //defined as 1.0/0.0
double infinity = Double.POSITIVE_INFINITY; //defined as 1.0/0.0
Double.isInfinite(infinity); //true
Double.isInfinite(infinity); //true
As a function:
As a function:
Line 55: Line 55:
}
}
The largest possible number in Java is also in the <tt>Double</tt> class.
The largest possible number in Java is also in the <tt>Double</tt> class.
Double biggestNumber = Double.MAX_VALUE;
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.
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 05:31, 3 February 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.

Ada

compiler: GNAT GPL 2007

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;

procedure Infinities is
   F : Float := Float'Last;
   I : Float := F * 10.0; 
   Ff : Float := Float'First;
   II : Float := FF * 10.0;  
begin
   Put(Item => F);
   New_Line;
   Put(Item => I);
   New_Line;
   Put(Item => Ff);
   New_Line;
   Put(Item => Ii);
end Infinities;

Output:

 3.40282E+38
+Inf********
-3.40282E+38
-Inf********

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; //defined as 1.0/0.0
Double.isInfinite(infinity); //true

As a function:

public static double getInf(){
   return Double.POSITIVE_INFINITY;
}

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.