Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
 
Annotated Ada Reference Manual (Ada 2022 Draft 35)Legal Information
Contents   Index   References   Search   Previous   Next 

A.5.6 Big Integers

Static Semantics

1/5
{AI12-0208-1} {AI12-0366-1} The library package Numerics.Big_Numbers.Big_Integers has the following declaration:
2/5
with Ada.Strings.Text_Buffers;
package Ada.Numerics.Big_Numbers.Big_Integers
   with Preelaborate, Nonblocking, Global => in out synchronized is
3/5
{AI12-0208-1} {AI12-0366-1} {AI12-0407-1}    type Big_Integer is private
     with Integer_Literal => From_Universal_Image,
          Put_Image => Put_Image;
4/5
   function Is_Valid (Arg : Big_Integer) 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_Integer is unspecified, analogous to the value of a Valid attribute_reference applied to a default-initialized object of an integer type (see 13.9.2). The language-provided functions in the Big_Integers package only return values for which Is_Valid is certain to be True. 
5/5
   subtype Valid_Big_Integer is Big_Integer
      with Dynamic_Predicate => Is_Valid (Valid_Big_Integer),
           Predicate_Failure => (raise Program_Error);
6/5
   function "=" (L, R : Valid_Big_Integer) return Boolean;
   function "<" (L, R : Valid_Big_Integer) return Boolean;
   function "<=" (L, R : Valid_Big_Integer) return Boolean;
   function ">" (L, R : Valid_Big_Integer) return Boolean;
   function ">=" (L, R : Valid_Big_Integer) return Boolean;
7/5
   function To_Big_Integer (Arg : Integer) return Valid_Big_Integer;
8/5
   subtype Big_Positive is Big_Integer
      with Dynamic_Predicate => (if Is_Valid (Big_Positive)
                                 then Big_Positive > 0),
           Predicate_Failure => (raise Constraint_Error);
9/5
   subtype Big_Natural is Big_Integer
      with Dynamic_Predicate => (if Is_Valid (Big_Natural)
                                 then Big_Natural >= 0),
           Predicate_Failure => (raise Constraint_Error);
10/5
   function In_Range (Arg, Low, High : Valid_Big_Integer) return Boolean is
     (Low <= Arg and Arg <= High);
11/5
   function To_Integer (Arg : Valid_Big_Integer) return Integer
      with Pre => In_Range (Arg,
                            Low  => To_Big_Integer (Integer'First),
                            High => To_Big_Integer (Integer'Last))
                   or else raise Constraint_Error;
12/5
   generic
      type Int is range <>;
   package Signed_Conversions is
      function To_Big_Integer (Arg : Int) return Valid_Big_Integer;
      function From_Big_Integer (Arg : Valid_Big_Integer) return Int
         with Pre => In_Range (Arg,
                               Low  => To_Big_Integer (Int'First),
                               High => To_Big_Integer (Int'Last))
                      or else raise Constraint_Error;
   end Signed_Conversions;
13/5
   generic
      type Int is mod <>;
   package Unsigned_Conversions is
      function To_Big_Integer (Arg : Int) return Valid_Big_Integer;
      function From_Big_Integer (Arg : Valid_Big_Integer) return Int
         with Pre => In_Range (Arg,
                               Low  => To_Big_Integer (Int'First),
                               High => To_Big_Integer (Int'Last))
                      or else raise Constraint_Error;
   end Unsigned_Conversions;
14/5
   function To_String (Arg : Valid_Big_Integer;
                       Width : Field := 0;
                       Base  : Number_Base := 10) return String
      with Post => To_String'Result'First = 1;
15/5
   function From_String (Arg : String) return Valid_Big_Integer;
16/5
{AI12-0407-1}    function From_Universal_Image (Arg : String) return Valid_Big_Integer
      renames From_String;
17/5
   procedure Put_Image
     (Buffer : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class;
      Arg    : in Valid_Big_Integer);
18/5
   function "+" (L : Valid_Big_Integer) return Valid_Big_Integer;
   function "-" (L : Valid_Big_Integer) return Valid_Big_Integer;
   function "abs" (L : Valid_Big_Integer) return Valid_Big_Integer;
   function "+" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "-" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "*" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "/" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "mod" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "rem" (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function "**" (L : Valid_Big_Integer; R : Natural)
      return Valid_Big_Integer;
   function Min (L, R : Valid_Big_Integer) return Valid_Big_Integer;
   function Max (L, R : Valid_Big_Integer) return Valid_Big_Integer;
19/5
   function Greatest_Common_Divisor
     (L, R : Valid_Big_Integer) return Big_Positive
     with Pre => (L /= 0 and R /= 0) or else raise Constraint_Error;
20/5
private
   ... -- not specified by the language
end Ada.Numerics.Big_Numbers.Big_Integers;
21/5
{AI12-0208-1} {AI12-0366-1} To_String and From_String behave analogously to the Put and Get procedures defined in Text_IO.Integer_IO (in particular, with respect to the interpretation of the Width and Base parameters) except that Constraint_Error, not Data_Error, is propagated in error cases and the result of a call to To_String with a Width parameter of 0 and a nonnegative Arg parameter does not include a leading blank. Put_Image calls To_String (passing in the default values for the Width and Base parameters), prepends a leading blank if the argument is nonnegative, and writes the resulting value to the buffer using Text_Buffers.Put.
22/5
{AI12-0208-1} The other functions have their usual mathematical meanings.
23/5
{AI12-0208-1} {AI12-0366-1} The type Big_Integer needs finalization (see 7.6). 

Dynamic Semantics

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

Implementation Requirements

25/5
{AI12-0208-1} {AI12-0366-1} No storage associated with a Big_Integer object shall be lost upon assignment or scope exit.
25.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

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

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