Jump to content

Ada Programming/Libraries/Ada.Numerics.Big Numbers.Big Reals

From Wikibooks, open books for an open world

This language feature has been introduced in Ada 2022.

Ada.Numerics.Big Numbers.Big Reals is a unit of the Predefined Language Environment since Ada 2022.

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

   type Big_Real is private
      with Real_Literal => From_Universal_Image,
           Put_Image => Put_Image;

   function Is_Valid (Arg : Big_Real) return Boolean
      with Convention => Intrinsic;

   subtype Valid_Big_Real is Big_Real
      with Dynamic_Predicate => Is_Valid (Valid_Big_Real),
           Predicate_Failure => raise Program_Error;

   function "/" (Num, Den : Big_Integers.Valid_Big_Integer)
      return Valid_Big_Real
      with Pre => Den /= 0
                   or else raise Constraint_Error;

   function Numerator
      (Arg : Valid_Big_Real) return Big_Integers.Valid_Big_Integer
     with Post => (if Arg = 0.0 then Numerator'Result = 0);

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

   function To_Big_Real (Arg : Big_Integers.Valid_Big_Integer)
      return Valid_Big_Real is (Arg / 1);

   function To_Real (Arg : Integer) return Valid_Big_Real is
      (Big_Integers.To_Big_Integer (Arg) / 1);

   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;

   function In_Range (Arg, Low, High : Valid_Big_Real) return Boolean is
      (Low <= Arg and Arg <= High);

   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;

   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;

   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;

   function From_String (Arg   : String) return Valid_Big_Real;

   function From_Universal_Image (Arg : String) return Valid_Big_Real
      renames From_String;

   function From_Universal_Image (Num, Den : String)
      return Valid_Big_Real is
         (Big_Integers.From_Universal_Image (Num) /
          Big_Integers.From_Universal_Image (Den));

   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;

   procedure Put_Image
     (Buffer : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class;
      Arg    : in Valid_Big_Real);

   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;

private
   ... -- not specified by the language
end Ada.Numerics.Big_Numbers.Big_Reals;