Ada Programming/Attributes/'Compose


      Ada Lovelace 1838.jpg


      Description

      X'Compose(Fraction : X, Exponent : Integer) is an Ada attribute where X is any floating point type.

      Floating point types are represented as:

      sign \times mantissa \times radix^{exponent}

      where

      sign is 1 or -1
      mantissa is a fraction in base radix
      radix is the hardware radix (usually 2)
      exponent is an integer

      'Compose(Fraction, Exponent) returns the floating point number Fraction with the exponent replaced with Exponent.

      Example

      with Ada.Text_IO;
      
      procedure Compose is
      
         package T_IO renames Ada.Text_IO;
         package F_IO is new  Ada.Text_IO.Float_IO (Float);
         
         X : Float := 1.0;
      begin
         T_IO.Put ("                   X = ");
         F_IO.Put(Item => X, Exp => 0);
         T_IO.New_Line;
         
         for Exp in -2..2 loop
            T_IO.Put ("Float'Compose(X, " & Integer'Image(Exp) & ") = ");
            F_IO.Put(Item => Float'Compose(X, Exp), Exp => 0);
            T_IO.New_Line;
         end loop;
         
      end Compose;
      

      The output with GNAT 4.6 on the x86-64 architecture is:

                         X =  1.00000
      Float'Compose(X, -2) =  0.12500
      Float'Compose(X, -1) =  0.25000
      Float'Compose(X,  0) =  0.50000
      Float'Compose(X,  1) =  1.00000
      Float'Compose(X,  2) =  2.00000
      

      See also

      Wikibook

      Ada Reference Manual

      Last modified on 2 December 2012, at 00:42