4.5 Numeric Types

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

Numeric types specify sets of numerical values and include integer types (signed and modular), floating-point types, and fixed-point types (regular and decimal).

Numeric Type Declarations - General Forms

Integer Types
  (signed)
type My_Integer_Type is range Low .. High;
  -- where Low and High are integers
  -- (negative, zero or positive)
Integer Types
  (modular aka
    unsigned)
type My_Modular_Type is mod M;
  -- where M (modulus) is a positive integer
  -- example: type Hours is mod 24;
  -- example: type Minutes is mod 60;
Floating-Point Types
type My_Float_Type is digits D range Low .. High;
  -- where D is a positive integer, 
  -- Low and High are real numbers, and 
  -- the range constraint is optional
Fixed-Point Types
  (regular)
type My_Fixed_Type is delta P range Low .. High;
  -- P, Low and High are real numbers and
  -- P (precision) is positive
Decimal Types
(available only if  Annex F conformance is claimed)
type My_Decimal_Type is delta P digits D range L .. H;
  -- where D is a positive integer, 
  -- P, L and H are real numbers, and 
  -- P is a power of ten (such as 0.01)

The scale of a floating-point or decimal type is specified by the number of digits, D, used to represent values of the type. A fixed-point type is a set of equally-spaced values where the spacing or precision is represented by the delta value, P. For decimal fixed-point types P must be a power of ten.

Attributes of Numeric Types

Generally speaking, numeric types have the same attributes as enumeration types ( T'First, T'Last, T'Range, T'Pred, T'Succ, T'Min, T'Max, T'Image, T'Wide_Image, T'Value, T'Wide_Value, T'Pos, and T'Val). However, floating-point and fixed-point types do not have the T'Pos and T'Val attributes. Furthermore, fixed-point types have the attributes T'Delta, T'Small, D'Scale, D'Digits and D'Round(X) where T is the (fixed or fixed-decimal) subtype name, D is a decimal subtype name and X is a value of the type. Also, floating-point types have the following additional attributes: T'Digits, T'Floor(X), T'Ceiling(X), T'Truncation(X), T'Rounding(X), T'Unbiased_Rounding(X), and T'Remainder(X,Y), where T is the subtype name and X and Y are values of the type. Finally, modular types have the attribute T'Modulus. See [ARM95] for full details.

Universal Types and Implicit  Conversions

Evaluation of expressions containing numeric quantities involve values of universal integer type and/or values of universal real type and the implicit conversion to named types. Both of these universal types can be considered built-in anonymous types that cannot be named in a program, so the programmer can generally ignore their existence.

Example Program Illustrating User-defined Numeric Types

The package Sluggers exports a user-defined floating-point type, a user-defined fixed-point type and a function called ABPHR (At Bats Per Home Run). The test procedure declares instances of the generic packages, Float_IO and Fixed_IO, exported by Ada.Text_IO. It then uses Put procedures from these instances, as well as other Put procedures from Ada.Text_IO to display some data and results of computations.

Im4-6.gif (5292 bytes)

The following program was constructed so as to illustrate the declaration and use of both floating-point and fixed-point types. We could have written a simpler program, using one or the other, if the only objective was to compute and display at bats per home run.

Source Code Listing

------------------------------------------------------------
------------------------- Sluggers -------------------------
--  This package declares floating- and fixed-point types 
--  representing ABPHR (at bats per home run) and exports  
--  and implements a function for computing ABPHR.
------------------------------------------------------------
package Sluggers is
    
  type ABPHR_Float_Type is digits 5; -- At Bats per Home Run
  type ABPHR_Fixed_Type is delta 0.1 range 1.0 .. 99.0;
    
  function ABPHR(At_Bats, Home_Runs : Float)
           return ABPHR_Float_Type;
            
end Sluggers;
------------------------------------------------------------
package body Sluggers is
    
  function ABPHR(At_Bats, Home_Runs : Float)
           return ABPHR_Float_Type is
  begin
    return ABPHR_Float_Type(At_Bats/Home_Runs); -- type conversion
  end ABPHR;

end Sluggers;
------------------------------------------------------------
----------------------- Test_Sluggers ----------------------
--  This test procedure declares floating-point and fixed-
--  point ABPHR variables, calls the ABPHR function, performs
--  a type conversion, and displays results for one baseball 
--  player in one year (Mark McGuire in 1998)
------------------------------------------------------------
with Sluggers;
with Ada.Text_IO; use Ada.Text_IO; 
procedure Test_Sluggers is
  
  ABPHR_FL : Sluggers.ABPHR_Float_Type;  -- At Bats Per Home Run
  ABPHR_FX : Sluggers.ABPHR_Fixed_Type;  
  Name_1   : String  := "Mark McGuire";
  B2       : constant String  := "  ";
  Year     : Integer := 1998; 
  package FLIO is new Float_IO(Sluggers.ABPHR_Float_Type);
  package FXIO is new Fixed_IO(Sluggers.ABPHR_Fixed_Type);
  
begin
    
  Put_Line("    Name       Year   AB    HR    AB/HR       AB/HR");
  Put_Line("                                 (Float)     (Fixed)");
  New_Line; 
  ABPHR_FL := Sluggers.ABPHR(509.0, 70.0 );        -- function call
  ABPHR_FX := Sluggers.ABPHR_Fixed_Type(ABPHR_FL); -- type conversion
  
  Put(Name_1 & B2 & Integer'Image(Year) & B2 &
      Integer'Image(509) & B2 &Integer'Image(70) & B2); -- attribute
  FLIO.Put(ABPHR_FL);
  Put(B2);
  FXIO.Put(ABPHR_FX);
  New_Line;
  
end Test_Sluggers;
-----------------------------------------------------------

The above program produces the following output:

         Name       Year   AB    HR    AB/HR       AB/HR
                                      (Float)     (Fixed)
     Mark McGuire   1998   509   70   7.2714E00     7.3

Related Topics

4.1 Type System Overview A.2 List of Attributes

[ Back to top of pagePrev ] Next ]