Ada 95 Quality and Style Guide Chapter 7

Chapter 7: Portability - TOC - 7.2 NUMERIC TYPES AND EXPRESSIONS

7.2.8 Decimal Types and the Information Systems Annex

guideline

  • In information systems, declare different numeric decimal types to correspond to different scales (Brosgol, Eachus, and Emery 1994).
  • Create objects of different decimal types to reflect different units of measure (Brosgol, Eachus, and Emery 1994).
  • Declare subtypes of the appropriately scaled decimal type to provide appropriate range constraints for application-specific types.
  • Encapsulate each measure category in a package (Brosgol, Eachus, and Emery 1994).
  • Declare as few decimal types as possible for unitless data (Brosgol, Eachus, and Emery 1994).
  • For decimal calculations, determine whether the result should be truncated toward 0 or rounded.
  • Avoid decimal types and arithmetic on compilers that do not support the Information Systems Annex (Ada Reference Manual 1995, Annex F) in full.
  • example

    -- The salary cap today is $500,000; however this can be expanded to $99,999,999.99.
    type Executive_Salary is delta 0.01 digits 10 range 0 .. 500_000.00;
    
    ------------------------------------------------------------------------------
    package Currency is
    
       type Dollars is delta 0.01 digits 12;
    
       type Marks   is delta 0.01 digits 12;
    
       type Yen     is delta 0.01 digits 12;
    
       function To_Dollars (M : Marks) return Dollars;
       function To_Dollars (Y : Yen)   return Dollars;
    
       function To_Marks (D : Dollars) return Marks;
       function To_Marks (Y : Yen)     return Marks;
    
       function To_Yen (D : Dollars) return Yen;
       function To_Yen (M : Marks)   return Yen;
    
    end Currency;
    
    

    rationale

    The Ada language does not provide any predefined decimal types. Therefore, you need to declare decimal types for the different scales you will need to use. Differences in scale and precision must be considered in deciding whether or not a common type will suffice (Brosgol, Eachus, and Emery 1994).

    You need different types for objects measured in different units. This allows the compiler to detect mismatched values in expressions. If you declare all decimal objects to be of a single type, you forego the benefits of strong typing. For example, in an application that involves several currencies, each currency should be declared as a separate type. You should provide appropriate conversions between different currencies.

    You should map data with no particular unit of measure to a small set of types or a single type to avoid the explosion of conversions between numeric types.

    Separate the range requirement on a decimal type from its precision, i.e., the number of significant digits required. From the point of view of planning for change and ease of maintenance, you can use the digit's value to accommodate future growth in the values to be stored in objects of the type. For example, you may want to anticipate growth for database values and report formats. You can constrain the values of the type through a range constraint that matches current needs. It is easier to modify the range and avoid redefining databases and reports.

    Ada automatically truncates toward 0. If your requirements are to round the decimal result, you must explicitly do so using the 'Round attribute.

    The core language defines the basic syntax of and operations on decimal types. It does not specify, however, the minimum number of significant digits that must be supported. Nor does the core language require the compiler to support values of Small other than powers of 2, thus enabling the compiler effectively to reject a decimal declaration (Ada Reference Manual 1995, 3.5.9). The Information Systems Annex provides additional support for decimal types. It requires a minimum of 18 significant digits. It also specifies a Text_IO.Editing package that provides support analogous to the COBOL picture approach.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography