Infinity: Difference between revisions

2,060 bytes added ,  15 years ago
→‎{{header|Ada}}: Solution is fixed to work on any machine, Ways to avoid IEEE semantics elaborated
(javascript, python)
(→‎{{header|Ada}}: Solution is fixed to work on any machine, Ways to avoid IEEE semantics elaborated)
Line 4:
 
=={{header|Ada}}==
<ada>
{{works with|GNAT|GPL 2007}}
with Ada.Text_IoText_IO; use Ada.Text_IoText_IO;
 
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
procedure Infinities is
function Sup return Float is -- Only for predefined types
procedure Infinities is
F Result : Float := Float'Last;
begin
I : Float := F * 10.0;
Ff : Floatif :=not Float'First;Machine_Overflows then
II : Float Result := FFFloat'Succ * 10.0(Result);
end if;
begin
Put(Item => F)return Result;
end New_LineSup;
 
Put(Item => I);
function Inf return Float is -- Only for predefined types
New_Line;
I Result : Float := F * 10.0Float'First;
Put(Item => Ff);
New_Line;begin
if not Float'Machine_Overflows then
Put(Item => Ii);
Result := Float'Pred (Result);
end Infinities;
end if;
Output:
return Result;
3.40282E+38
end Inf;
+Inf********
begin
-3.40282E+38
Put_Line ("Supremum" & Float'Image (Sup));
-Inf********
Put_Line ("Infimum " & Float'Image (Inf));
end Infinities;
</ada>
The language-defined attribute Machine_Overflows is defined for each floating-point type. It is true when an overflow or divide-by-zero results in Constraint_Error exception propagation. When the underlying machine type is incapable to implement this semantics the attribute is false. It is to expect that on the machines with [[IEEE]] 754 hardware Machine_Overflows is true. The language-defined attributes Succ and Pred yield the value next or previous to the argument, correspondingly.
 
Sample output on a machine where Float is [[IEEE]] 754:
<pre>
Supremum +Inf********
Infimum -Inf********
</pre>
Note that the code above does not work for user-defined types, which may have range of values narrower than one of the underlying hardware type. This case represents one of the reasons why [[Ada]] programmers are advised not to use predefined floating-point types. There is a danger that the implementation of might be [[IEEE]] 754, and so the program semantics could be broken.
 
Here is the code that should work for any type on any machine:
<ada>
with Ada.Float_Text_IoText_IO; use Ada.Float_Text_IoText_IO;
 
procedure Infinities is
type Real is digits 5 range -10.0..10.0;
function Sup return Real is
Result : Real := Real'Last;
begin
return Real'Succ (Result);
exception
when Constraint_Error =>
return Result;
end Sup;
 
function Inf return Real is
Result : Real := Real'First;
begin
return Real'Pred (Result);
exception
when Constraint_Error =>
return Result;
end Inf;
begin
Put_Line ("Supremum" & Real'Image (Sup));
Put_Line ("Infimum " & Real'Image (Inf));
end Infinities;
</ada>
Sample output. Note that the compiler is required to generate Constraint_Error even if the hardware is [[IEEE]] 754. So the upper and lower bounds are 10.0 and -10.0:
<pre>
Supremum 1.0000E+01
Infimum -1.0000E+01
</pre>
 
=={{header|C++}}==