Ada Programming/Attributes/'Compose

From Wikibooks, open books for an open world
Jump to: navigation, search

Ada Lovelace 1838.jpg

Description[edit]

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[edit]

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[edit]

Wikibook[edit]

Ada Reference Manual[edit]