Annotated Ada Reference Manual (Ada 202y Draft 1)Legal Information
Contents   Index   References   Search   Previous   Next 

A.5.7 Big Reals

Static Semantics

1/5
{AI12-0208-1} {AI12-0366-1} The library package Numerics.Big_Numbers.Big_Reals has the following declaration:
2/5
with Ada.Numerics.Big_Numbers.Big_Integers;
   use all type Big_Integers.Big_Integer;
with Ada.Strings.Text_Buffers;
package Ada.Numerics.Big_Numbers.Big_Reals
   with Preelaborate, Nonblocking, Global => in out synchronized is
3/5
{AI12-0208-1} {AI12-0366-1} {AI12-0407-1}    type Big_Real is private
      with Real_Literal => From_Universal_Image,
           Put_Image => Put_Image;
4/5
   function Is_Valid (Arg : Big_Real) return Boolean
      with Convention => Intrinsic;
4.a/5
Discussion: {AI12-0005-1} The result of Is_Valid on a default-initialized object of type Big_Real is unspecified, analogous to the value of a Valid attribute_reference applied to a default-initialized object of a real type (see 13.9.2). The language-provided functions in the Big_Reals package only return values for which Is_Valid is certain to be True. 
5/5
   subtype Valid_Big_Real is Big_Real
      with Dynamic_Predicate => Is_Valid (Valid_Big_Real),
           Predicate_Failure => raise Program_Error;
6/5
   function "/" (Num, Den : Big_Integers.Valid_Big_Integer)
      return Valid_Big_Real
      with Pre => Den /= 0
                   or else raise Constraint_Error;
7/5
   function Numerator
      (Arg : Valid_Big_Real) return Big_Integers.Valid_Big_Integer
     with Post => (if Arg = 0.0 then Numerator'Result = 0);
7.a/5
Reason: The postcondition of Numerator cannot be complete as it cannot mention Denominator. Since the postcondition of Denominator uses Numerator, we would get an infinite mutual recursion if both postconditions are enabled. The postcondition of Denominator serves as the postcondition for Numerator as well unless Arg = 0.0. 
8/5
   function Denominator (Arg : Valid_Big_Real)
      return Big_Integers.Big_Positive
      with Post =>
        (if Arg = 0.0 then Denominator'Result = 1
         else Big_Integers.Greatest_Common_Divisor
                (Numerator (Arg), Denominator'Result) = 1);
9/5
   function To_Big_Real (Arg : Big_Integers.Valid_Big_Integer)
      return Valid_Big_Real is (Arg / 1);
10/5
   function To_Real (Arg : Integer) return Valid_Big_Real is
      (Big_Integers.To_Big_Integer (Arg) / 1);
11/5
   function "=" (L, R : Valid_Big_Real) return Boolean;
   function "<" (L, R : Valid_Big_Real) return Boolean;
   function "<=" (L, R : Valid_Big_Real) return Boolean;
   function ">" (L, R : Valid_Big_Real) return Boolean;
   function ">=" (L, R : Valid_Big_Real) return Boolean;
12/5
   function In_Range (Arg, Low, High : Valid_Big_Real) return Boolean is
      (Low <= Arg and Arg <= High);
13/5
   generic
      type Num is digits <>;
   package Float_Conversions is
      function To_Big_Real (Arg : Num) return Valid_Big_Real;
      function From_Big_Real (Arg : Valid_Big_Real) return Num
         with Pre => In_Range (Arg,
                               Low  => To_Big_Real (Num'First),
                               High => To_Big_Real (Num'Last))
                     or else (raise Constraint_Error);
   end Float_Conversions;
14/5
   generic
      type Num is delta <>;
   package Fixed_Conversions is
      function To_Big_Real (Arg : Num) return Valid_Big_Real;
      function From_Big_Real (Arg : Valid_Big_Real) return Num
         with Pre => In_Range (Arg,
                               Low  => To_Big_Real (Num'First),
                               High => To_Big_Real (Num'Last))
                     or else (raise Constraint_Error);
   end Fixed_Conversions;
15/5
   function To_String (Arg  : Valid_Big_Real;
                       Fore : Field := 2;
                       Aft  : Field := 3;
                       Exp  : Field := 0) return String
      with Post => To_String'Result'First = 1;
16/5
   function From_String (Arg   : String) return Valid_Big_Real;
17/5
{AI12-0407-1}    function From_Universal_Image (Arg : String) return Valid_Big_Real
      renames From_String;
18/5
{AI12-0407-1}    function From_Universal_Image (Num, Den : String)
      return Valid_Big_Real is
         (Big_Integers.From_Universal_Image (Num) /
          Big_Integers.From_Universal_Image (Den));
19/5
   function To_Quotient_String (Arg : Valid_Big_Real) return String is
      (To_String (Numerator (Arg)) & " / " & To_String (Denominator (Arg)));
   function From_Quotient_String (Arg : String) return Valid_Big_Real;
20/5
   procedure Put_Image
     (Buffer : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class;
      Arg    : in Valid_Big_Real);
21/5
   function "+" (L : Valid_Big_Real) return Valid_Big_Real;
   function "-" (L : Valid_Big_Real) return Valid_Big_Real;
   function "abs" (L : Valid_Big_Real) return Valid_Big_Real;
   function "+" (L, R : Valid_Big_Real) return Valid_Big_Real;
   function "-" (L, R : Valid_Big_Real) return Valid_Big_Real;
   function "*" (L, R : Valid_Big_Real) return Valid_Big_Real;
   function "/" (L, R : Valid_Big_Real) return Valid_Big_Real;
   function "**" (L : Valid_Big_Real; R : Integer)
      return Valid_Big_Real;
   function Min (L, R : Valid_Big_Real) return Valid_Big_Real;
   function Max (L, R : Valid_Big_Real) return Valid_Big_Real;
22/5
private
   ... -- not specified by the language
end Ada.Numerics.Big_Numbers.Big_Reals;
23/5
{AI12-0208-1} {AI12-0366-1} To_String and From_String behave analogously to the Put and Get procedures defined in Text_IO.Float_IO (in particular, with respect to the interpretation of the Fore, Aft, and Exp parameters), except that Constraint_Error (not Data_Error) is propagated in error cases. From_Quotient_String implements the inverse function of To_Quotient_String; Constraint_Error is propagated in error cases. Put_Image calls To_String, and writes the resulting value to the buffer using Text_Buffers.Put.
24/5
{AI12-0208-1} For an instance of Float_Conversions or Fixed_Conversions, To_Big_Real is exact (that is, the result represents exactly the same mathematical value as the argument) and From_Big_Real is subject to the same precision rules as a type conversion of a value of type T to the target type Num, where T is a hypothetical floating point type whose model numbers include all of the model numbers of Num as well as the exact mathematical value of the argument.
25/5
{AI12-0208-1} The other functions have their usual mathematical meanings.
26/5
{AI12-0208-1} {AI12-0366-1} The type Big_Real needs finalization (see 7.6).

Dynamic Semantics

27/5
{AI12-0208-1} {AI12-0366-1} For purposes of determining whether predicate checks are performed as part of default initialization, the type Big_Real is considered to have a subcomponent that has a default_expression.
27.a/5
Ramification: {AI12-0005-1} {AI12-0208-1} This means that the elaboration of 
27.b/5
Default_Initialized_Object : Valid_Big_Real;
27.c/5
either produces a value for which Is_Valid is True. or it propagates Program_Error. 

Implementation Requirements

28/5
{AI12-0208-1} {AI12-0366-1} No storage associated with a Big_Real object shall be lost upon assignment or scope exit.
28.a/5
Implementation Note: {AI12-0208-1} The “No storage ... shall be lost” requirement does not preclude implementation techniques such as caching or unique number tables. 

Extensions to Ada 2012

28.b/5
{AI12-0208-1} {AI12-0366-1} The package Numerics.Big_Numbers.Big_Reals is new.

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe